Ejemplo n.º 1
0
        public virtual bool OnGuildCreate(DiscordPacket Packet)
        {
            Guild Guild = null;

            if (long.TryParse(Packet.d["id"].GetString(), out long Id))
            {
                if (Guilds.TryGet(Id, out Guild))
                {
                    Console.WriteLine($"Found guild obj for {Id}, patching");
                    try
                    {
                        Guild.Patch(Packet.d);
                    }catch (Exception Ex)
                    {
                        Console.WriteLine($"Exception during patch: {Ex.Message}\n{Ex.StackTrace}");
                    }
                }
                else
                {
                    Console.WriteLine($"Creating new guild for {Id}");
                    Guilds.CreateEntry(Packet.d);
                }
            }

            Console.WriteLine(Guild);
            GuildCreate?.Invoke(this, Guild);
            return(true);
        }
Ejemplo n.º 2
0
 private void ResolveDiscordEvent(DiscordPacket Packet)
 {
     Console.WriteLine($"==== New event! ====\n{Packet.t}");
     Events.TryGetValue(Packet.t, out Func <DiscordPacket, bool> Func);
     if (Func is null)
     {
         Console.WriteLine("Func is null");
         return;
     }
     _ = Func(Packet);
 }
Ejemplo n.º 3
0
        public void ResolvePacketType(DiscordPacket Packet)
        {
            switch (Packet.op)
            {
            case (int)PacketType.Dispatch:
                ResolveDiscordEvent(Packet);
                break;

            case (int)PacketType.Heartbeat:
                // TODO: HeartBeatRequested(Packet); //Discord requested heartbeat
                break;

            case (int)PacketType.Identify:
                // Sent to discord, shouldn't need to do anything here
                break;

            case (int)PacketType.PresenceUpdate:
                // Sent to discord, shouldn't need to do anything here
                break;

            case (int)PacketType.VoiceStateUpdate:
                // Sent to discord, shouldn't need to do anything here
                break;

            case (int)PacketType.Resume:
                // Sent to discord, shouldn't need to do anything here
                break;

            case (int)PacketType.Reconnect:
                // TODO: Reconnect(Packet);
                break;

            case (int)PacketType.RequestGuildMembers:
                // Sent to discord, shouldn't need to do anything here
                break;

            case (int)PacketType.InvalidSession:
                // TODO: Reconnect(Packet);
                break;

            case (int)PacketType.Hello:
                PacketHello(Packet);
                break;

            case (int)PacketType.HeartbeatAck:
                HeartbeatAck(Packet);
                break;
            }
        }
Ejemplo n.º 4
0
        internal void OnSocketEvent(object Sender, string Message)
        {
            Console.WriteLine("Event came through to client:");
            DiscordPacket Packet = JsonSerializer.Deserialize <DiscordPacket>(Message);

            Packet.Raw = Message;
            Console.WriteLine(Packet);
            if (Packet.s.HasValue)
            {
                SequenceNum = (int)Packet.s;
            }
            Console.WriteLine($"SequenceNum: {SequenceNum}");

            ResolvePacketType(Packet);
        }
Ejemplo n.º 5
0
        private async void PacketHello(DiscordPacket Packet)
        {
            // WS should identify to the gateway after receiving this
            Console.WriteLine("Received hello packet");
            Console.WriteLine(Packet.d["heartbeat_interval"]);
            HeartbeatInterval = Packet.d["heartbeat_interval"].GetInt32();

            if (HeartbeatCTS != null)
            {
                HeartbeatCTS.Dispose();
            }
            HeartbeatCTS = new CancellationTokenSource();

            Console.WriteLine("Starting heartbeat");
            await Task.Factory.StartNew(this.Heartbeat, HeartbeatCTS.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).ConfigureAwait(false);

            await Identify();
        }
Ejemplo n.º 6
0
        public virtual bool OnReady(DiscordPacket Packet)
        {
            //TODO: Initialise guilds
            Dictionary <string, JsonElement> Data = Packet.d;

            User = new ClientUser(this, Data["user"]);

            ArrayEnumerator Guilds = Data["guilds"].EnumerateArray();

            foreach (JsonElement RawGuild in Guilds)
            {
                this.Guilds.CreateEntry(RawGuild);
            }

            SessionId = Data["session_id"].GetString();

            IsReady = true;
            Ready?.Invoke(this, null);
            return(true);
        }
Ejemplo n.º 7
0
 public virtual bool OnMessage(DiscordPacket Packet)
 {
     MessageCreate?.Invoke(this, new Message(this, Packet.d));
     return(true);
 }
Ejemplo n.º 8
0
 private void HeartbeatAck(DiscordPacket Packet)
 {
     MissedHeartbeats--;
     Console.WriteLine("Heartbeat acknowledged");
 }