Ejemplo n.º 1
0
        private void IDENTIFY()
        {
            string data = $"{{\"op\": 2, \"d\": {{ \"token\": \"{token}\", \"properties\": {{\"$os\": \"windows\", \"$browser\": \"carrotcord\", \"$device\": \"carrotcord\"}}, \"large_threshold\": 250, \"presence\": {{ \"game\": {{ \"name\": \"{game}\", \"type\": 0 }}, \"status\": \"online\", \"afk\": false, \"since\": null }} }} }}";

            CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, "[IDENTIFY]");
            socket.Send(data);
        }
Ejemplo n.º 2
0
        private void login(ConnectType type)
        {
            if (socket != null && socket.IsAlive)
            {
                return;
            }
            dynamic d = JsonConvert.DeserializeObject(RestApiClient.GETNOAUTH("gateway").Content);

            gateway          = Convert.ToString(d.url);
            ConnectionStatus = Connection.LOGGING_IN;
            socket           = new WebSocket($"{gateway}/?v={api_version}&encoding={encoding}&client_id=430746509714259989");
            CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, $"[CONNECTING] {gateway}, version: {api_version}, encoding: {encoding}");
            socket.ConnectAsync();
            if (type == ConnectType.NORMAL)
            {
                socket.OnOpen += Socket_OnOpenNormal;
            }
            else if (type == ConnectType.RESUME)
            {
                ConnectionStatus   = Connection.RESUMING;
                retryTimer.Enabled = false;
                socket.OnOpen     += Socket_OnOpenResume;
            }
            socket.OnClose   += Socket_OnClose;
            socket.OnMessage += Socket_OnMessage;
        }
 public void reply(string message)
 {
     if (Bot.current != null)
     {
         CarrotcordLogger.logVerbose(RestApiClient.POSTDiscordMessage($"channels/{channelID}/messages", message).Content);
     }
 }
Ejemplo n.º 4
0
 private void Timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     if (socket.IsAlive)
     {
         socket.Send("{ \"op\": 1, \"d\": null }");
         CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, "[HEARTBEAT]");
     }
 }
Ejemplo n.º 5
0
 internal void SEND_RESUME()
 {
     //CarrotcordLogger.logBork("RESUMING");
     CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, $"RESUMING WITH SESSION ID {session_id} AT SEQUENCE {sequence}");
     socket.SendAsync($"{{ \"op\": {OPCodes.RESUME}, \"d\": {{\"token\": \"{token}\", \"session_id\": \"{session_id}\", \"seq\": {sequence}}}}}", new Action <bool>(completed => {
         //CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, "RESUME SENT");
     }));
 }
Ejemplo n.º 6
0
        public void INVALID_SESSIONS_EVENT(dynamic data)
        {
            //ratelimited = true;
            CarrotcordLogger.logBork("[DISCONNECT] OPCODE 9 INVALID SESSION");

            /**CarrotcordLogger.logBork("[OPCODE 9 DATA]: ---------------------------");
             * CarrotcordLogger.logBork(data);
             * CarrotcordLogger.logBork("--------------------------------------------");*/
            Disconnect();
        }
Ejemplo n.º 7
0
 protected void MESSAGE_DELETED(dynamic data)
 {
     CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, data);
     if (Storage.cachedMessages.TryGetValue(Convert.ToInt64(data.d.id), out Message msg))
     {
         MessageDeletedEvent?.Invoke(this, new MessageDeletedEventArgs(msg));
     }
     else
     {
         MessageDeletedEvent?.Invoke(this, new MessageDeletedEventArgs(Convert.ToInt64(data.d.id), Convert.ToInt64(data.d.channel_id), Convert.ToInt64(data.d.guild_id)));
     }
 }
Ejemplo n.º 8
0
 private void Socket_OnClose(object sender, CloseEventArgs e)
 {
     /**if(retry)
      * {
      *  retryTimer = new Timer(30000);
      *  retryTimer.AutoReset = true;
      *  retryTimer.Elapsed += RetryTimer_Elapsed;
      *  retryTimer.Start();
      * }*/
     timer.Stop();
     CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, "DISCONNECTED");
 }
        public static Message getMessage(long channel, long ID)
        {
            CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, $"channels/{channel}/messages/{ID}");
            dynamic data    = JSONDeserializeAndHandleErrors.DeserializeJSON(RestApiClient.GET($"channels/{channel}/messages/{ID}"));
            Message message = new Message();

            message.ID        = data.id;
            message.channelID = data.channel_id;
            message.author    = User.fromData(data.author);
            message.pinned    = data.pinned;
            message.Guild     = Guild.getGuild(message.guildID);
            message.content   = Convert.ToString(data.content);
            return(message);
        }
Ejemplo n.º 10
0
 private void HELLO(dynamic data)
 {
     CarrotcordLogger.LogServer(CarrotcordLogger.LogSource.WEBSOCKET, "[HELLO]");
     heartbeat_interval = Convert.ToInt32(data.d.heartbeat_interval);
     CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, $"Heartbeat interval set to {heartbeat_interval}.");
     if (timer == null || bootup == false || ConnectionStatus == Connection.RESUMING)
     {
         timer           = new Timer(heartbeat_interval);
         timer.AutoReset = true;
         timer.Elapsed  += Timer_Elapsed;
         timer.Start();
         CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, "HEARTBEAT");
         socket.SendAsync($"{{\"op\":{OPCodes.HEARTBEAT}, \"d\": null}}", new Action <bool>(completed => {
             helloHeartbeat = true;
         }));
     }
 }
Ejemplo n.º 11
0
 public Bot RegisterCommand(Command command)
 {
     if (!command.isValid())
     {
         throw new NullReferenceException("Either the name or result variable has not been initialized!");
     }
     foreach (string cmd in commands.Keys)
     {
         if (cmd == command.name)
         {
             CarrotcordLogger.logBork($"There already is a command registered by the name of \"{command.name}\", skipping.");
             return(this);
         }
     }
     commands.Add(command.name, command);
     return(this);
 }
Ejemplo n.º 12
0
 public GuildUser getMember(object member)
 {
     if (member is Int64)
     {
         //TODO: FIX
         //CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, "long: "+member);
         foreach (GuildUser m in members)
         {
             //CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, "guilduser " + m.username + "("+m.ID+")");
             if (m.ID == (long)member)
             {
                 //CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, "MATCH");
                 return(m);
             }
         }
         return(null);
     }
     if (member is User)
     {
         foreach (GuildUser m in members)
         {
             if (m.user == (User)member)
             {
                 return(m);
             }
         }
         return(null);
     }
     if (member is string)
     {
         CarrotcordLogger.log(CarrotcordLogger.LogSource.BOT, "" + member);
         foreach (GuildUser m in members)
         {
             if (m.username == (string)member)
             {
                 return(m);
             }
         }
         return(null);
     }
     return(null);
 }
Ejemplo n.º 13
0
        private void handleData(dynamic data)
        {
            string t = Convert.ToString(data.t);

            if (data.code != null && data.code != "")
            {
                var code = Convert.ToInt32(data.code);
                CarrotcordLogger.logBork($"Error Code: {code}, {(ErrorCode)code}");
                return;
            }
            if (t != "null" && t != "")
            {
                CarrotcordLogger.LogServer(CarrotcordLogger.LogSource.EVENT, t);
                switch (t)
                {
                case "READY":
                    READY(data);
                    break;

                case "MESSAGE_CREATE":
                    MESSAGE_CREATED(data);
                    break;

                case "MESSAGE_DELETE":
                    MESSAGE_DELETED(data);
                    break;

                case "GUILD_CREATE":
                    GUILD_CREATE(data);
                    break;
                }
            }
            if (data.s != null && Convert.ToInt32(data.s) != null)
            {
                //session_id = Convert.ToInt32(data.s);
                sequence = Convert.ToInt32(data.s);
            }
            if (Convert.ToString(data.op) != null)
            {
                //CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, "OPCODE " + Convert.ToString(data.op));
            }
            switch (Convert.ToString(data.op))
            {
            case "9":
                INVALID_SESSIONS_EVENT(data);
                CarrotcordLogger.logBork("BIG BORK OPCODE 9");
                break;

            case "10":
                HELLO(data);
                break;

            case "11":
                CarrotcordLogger.LogServer(CarrotcordLogger.LogSource.WEBSOCKET, "<- HEARTBEAT RECEIVED");
                if (helloHeartbeat)
                {
                    HELLO_AFTER_HEARTBEAT();
                }
                break;
            }
        }
Ejemplo n.º 14
0
 private void Socket_OnOpenNormal(object sender, EventArgs e)
 {
     CarrotcordLogger.log(CarrotcordLogger.LogSource.WEBSOCKET, $"[CONNECTED] {gateway}/?v={api_version}&encoding={encoding}");
 }