diff --git a/Maki/Discord.cs b/Maki/Discord.cs index 9e09c46..c0d896e 100644 --- a/Maki/Discord.cs +++ b/Maki/Discord.cs @@ -271,11 +271,6 @@ namespace Maki ShardClient.OnResumed += ShardManager_OnResumed; ShardClient.OnUserUpdate += ShardManager_OnUserUpdate; - - ShardClient.OnSocketOpen += ShardManager_OnSocketOpen; - ShardClient.OnSocketClose += ShardManager_OnSocketClose; - ShardClient.OnSocketError += ShardManager_OnSocketError; - ShardClient.OnSocketMessage += ShardManager_OnSocketMessage; #endregion } @@ -855,22 +850,10 @@ namespace Maki OnUserUpdate?.Invoke(user); } - private void ShardManager_OnSocketOpen(GatewayShard shard) - { - } - - private void ShardManager_OnSocketClose(GatewayShard shard, bool wasClean, ushort code, string reason) - { - } - private void ShardManager_OnSocketError(GatewayShard shard, Exception ex) { } - private void ShardManager_OnSocketMessage(GatewayShard shard, string text) - { - } - #endregion #region IDisposable diff --git a/Maki/Gateway/GatewayEvent.cs b/Maki/Gateway/GatewayEvent.cs deleted file mode 100644 index c225d55..0000000 --- a/Maki/Gateway/GatewayEvent.cs +++ /dev/null @@ -1,192 +0,0 @@ -namespace Maki.Gateway -{ - /// - /// Gateway Dispatch Events - /// - enum GatewayEvent - { - #region Voice Events - CALL_CREATE, - CALL_DELETE, - CALL_UPDATE, - - /// - /// Sent when a guild's voice server is updated. This is sent when initially connecting to voice, and when the current voice instance fails over to a new server. - /// - VOICE_SERVER_UPDATE, - - /// - /// Sent when someone joins/leaves/moves voice channels. Inner payload is a voice state object. - /// - VOICE_STATE_UPDATE, - #endregion - - #region Channel Events - /// - /// Sent when a new channel is created, relevant to the current user. The inner payload is a DM or Guild channel object. - /// - CHANNEL_CREATE, - - /// - /// Sent when a channel relevant to the current user is deleted. The inner payload is a DM or Guild channel object. - /// - CHANNEL_DELETE, - CHANNEL_PINS_ACK, - CHANNEL_PINS_UPDATE, - CHANNEL_RECIPIENT_ADD, - CHANNEL_RECIPIENT_REMOVE, - - /// - /// Sent when a channel is updated. The inner payload is a guild channel object. - /// - CHANNEL_UPDATE, - #endregion - - #region Guild Events - /// - /// Sent when a user is banned from a guild. The inner payload is a user object, with an extra guild_id key. - /// - GUILD_BAN_ADD, - - /// - /// Sent when a user is unbanned from a guild. The inner payload is a user object, with an extra guild_id key. - /// - GUILD_BAN_REMOVE, - - /// - /// This event can be sent in three different scenarios: - /// 1. When a user is initially connecting, to lazily load and backfill information for all unavailable guilds sent in the ready event. - /// 2. When a Guild becomes available again to the client. - /// 3. When the current user joins a new Guild. - /// The inner payload is a guild object, with all the extra fields specified. - /// - GUILD_CREATE, - - /// - /// Sent when a guild becomes unavailable during a guild outage, or when the user leaves or is removed from a guild. See GUILD_CREATE for more information about how to handle this event. - /// - GUILD_DELETE, - - /// - /// Sent when a guild's emojis have been updated. - /// - GUILD_EMOJIS_UPDATE, - - /// - /// Sent when a guild integration is updated. - /// - GUILD_INTEGRATIONS_UPDATE, - - /// - /// Sent when a new user joins a guild. The inner payload is a guild member objec, with an extra guild_id key. - /// - GUILD_MEMBER_ADD, - - /// - /// Sent when a guild member is updated. - /// - GUILD_MEMBER_UPDATE, - - /// - /// Sent when a user is removed from a guild (leave/kick/ban). - /// - GUILD_MEMBER_REMOVE, - - /// - /// Sent in response to Gateway Request Guild Members. - /// - GUILD_MEMBERS_CHUNK, - - /// - /// Sent when a guild role is created. - /// - GUILD_ROLE_CREATE, - - /// - /// Sent when a guild role is updated. - /// - GUILD_ROLE_UPDATE, - - /// - /// Sent when a guild role is deleted. - /// - GUILD_ROLE_DELETE, - - /// - /// Sent when a guild is updated. The inner payload is a guild object. - /// - GUILD_UPDATE, - #endregion - - #region Message Events - MESSAGE_ACK, - - /// - /// Sent when a message is created. The inner payload is a message object. - /// - MESSAGE_CREATE, - - /// - /// Sent when a message is deleted. - /// - MESSAGE_DELETE, - - /// - /// Sent when multiple messages are deleted at once. - /// - MESSAGE_DELETE_BULK, - MESSAGE_REACTION_ADD, - MESSAGE_REACTION_REMOVE, - MESSAGE_REACTIONS_REMOVE_ALL, - - /// - /// Sent when a message is updated. The inner payload is a message object. - /// - MESSAGE_UPDATE, - #endregion - - #region Presence Events - /// - /// Sent when a user starts typing in a channel. - /// - TYPING_START, - - /// - /// A user's presence is their current state on a guild. This event is sent when a user's presence is updated for a guild. - /// - PRESENCE_UPDATE, - PRESENCES_REPLACE, - #endregion - - #region Relation Events - FRIEND_SUGGESTION_DELETE, - RELATIONSHIP_ADD, - RELATIONSHIP_REMOVE, - #endregion - - #region State Events - /// - /// The ready event is dispatched when a client has completed the initial handshake with the gateway (for new sessions). The ready event can be the largest and most complex event the gateway will send, as it contains all the state required for a client to begin interacting with the rest of the platform. - /// - READY, - - /// - /// The resumed event is dispatched when a client has sent a resume payload to the gateway (for resuming existing sessions). - /// - RESUMED, - #endregion - - #region User Events - /// - /// Sent when properties about the user change. Inner payload is a user object. - /// - USER_UPDATE, - USER_NOTE_UPDATE, - - /// - /// Sent when the current user updates their settings. Inner payload is a user settings object. - /// - USER_SETTINGS_UPDATE, - #endregion - } -} diff --git a/Maki/Gateway/GatewayShard.cs b/Maki/Gateway/GatewayShard.cs index a1c7222..868d224 100644 --- a/Maki/Gateway/GatewayShard.cs +++ b/Maki/Gateway/GatewayShard.cs @@ -1,9 +1,4 @@ -using Maki.Structures.Channels; -using Maki.Structures.Gateway; -using Maki.Structures.Guilds; -using Maki.Structures.Messages; -using Maki.Structures.Presences; -using Maki.Structures.Users; +using Maki.Structures.Gateway; using Newtonsoft.Json; using System; using WebSocketSharp; @@ -18,7 +13,7 @@ namespace Maki.Gateway /// /// Session key for continuing a resuming after disconnecting /// - private string session; + private string Session; /// /// Websocket container @@ -43,72 +38,17 @@ namespace Maki.Gateway /// /// Parent DiscordClient instance /// - private readonly Discord client; + private readonly Discord Client; /// /// Heartbeat handler /// - private readonly GatewayHeartbeatManager heartbeatHandler; - - #region Events - public event Action OnCallCreate; - public event Action OnCallDelete; - public event Action OnCallUpdate; - public event Action OnVoiceServerUpdate; - public event Action OnVoiceStateUpdate; - - public event Action OnChannelCreate; - public event Action OnChannelDelete; - public event Action OnChannelPinsAck; - public event Action OnChannelPinsUpdate; - public event Action OnChannelRecipientAdd; - public event Action OnChannelRecipientRemove; - public event Action OnChannelUpdate; - - public event Action OnGuildBanAdd; - public event Action OnGuildBanRemove; - public event Action OnGuildCreate; - public event Action OnGuildDelete; - public event Action OnGuildEmojisUpdate; - public event Action OnGuildIntegrationsUpdate; - public event Action OnGuildMemberAdd; - public event Action OnGuildMemberUpdate; - public event Action OnGuildMemberRemove; - public event Action OnGuildMembersChunk; - public event Action OnGuildRoleCreate; - public event Action OnGuildRoleUpdate; - public event Action OnGuildRoleDelete; - public event Action OnGuildUpdate; - - public event Action OnMessageAck; - public event Action OnMessageCreate; - public event Action OnMessageDelete; - public event Action OnMessageDeleteBulk; - public event Action OnMessageReactionAdd; - public event Action OnMessageReactionRemove; - public event Action OnMessageReactionsRemoveAll; - public event Action OnMessageUpdate; - - public event Action OnTypingStart; - public event Action OnPresenceUpdate; - public event Action OnPresencesReplace; - - public event Action OnFriendSuggestionDelete; - public event Action OnRelationshipAdd; - public event Action OnRelationshipRemove; - - public event Action OnReady; - public event Action OnResumed; - - public event Action OnUserUpdate; - public event Action OnUserNoteUpdate; - public event Action OnUserSettingsUpdate; - - public event Action OnSocketOpen; - public event Action OnSocketClose; - public event Action OnSocketError; - public event Action OnSocketMessage; - #endregion + private readonly GatewayHeartbeatManager HeartbeatHandler; + + /// + /// Fires when a payload is received. + /// + public event Action OnSocketPayload; /// /// Constructor @@ -118,34 +58,19 @@ namespace Maki.Gateway public GatewayShard(int id, Discord c) { Id = id; - client = c; - heartbeatHandler = new GatewayHeartbeatManager(this); + Client = c; + HeartbeatHandler = new GatewayHeartbeatManager(this); Connect(); } - + /// - /// Event handler for WebSocketSharp's OnOpen event, forwards to our OnSocketOpen event - /// - /// Sender object - /// Event arguments - private void WebSocket_OnOpen(object sender, EventArgs e) => OnSocketOpen?.Invoke(this); - - /// - /// Event handler for WebSocketSharp's OnError event, forwards to our OnSocketError event - /// - /// Sender object - /// Event arguments - private void WebSocket_OnError(object sender, ErrorEventArgs e) => OnSocketError?.Invoke(this, e.Exception); - - /// - /// Event handler for WebSocketSharp's OnClose event, stops heartbeats, forwards to our OnSocketClose event and reconnects if the close wasn't clean + /// Event handler for WebSocketSharp's OnClose event, stops heartbeats and reconnects if the close wasn't clean /// /// Sender object /// Event arguments private void WebSocket_OnClose(object sender, CloseEventArgs e) { - heartbeatHandler.Stop(); - OnSocketClose?.Invoke(this, e.WasClean, e.Code, e.Reason); + HeartbeatHandler.Stop(); if (!e.WasClean || e.Code != 1000) Connect(); @@ -162,217 +87,19 @@ namespace Maki.Gateway if (!e.IsText) return; - Console.WriteLine(e.Data.Replace(client.Token, new string('*', client.Token.Length))); - OnSocketMessage?.Invoke(this, e.Data); + Console.WriteLine(e.Data.Replace(Client.Token, new string('*', Client.Token.Length))); + GatewayPayload payload = JsonConvert.DeserializeObject(e.Data); - + OnSocketPayload?.Invoke(this, payload); + switch (payload.OPCode) { case GatewayOPCode.Dispatch: LastSequence = payload.Sequence; - Enum.TryParse(payload.Name, out GatewayEvent evt); - switch (evt) { - #region Call - case GatewayEvent.CALL_CREATE: - OnCallCreate?.Invoke(this); - break; - - case GatewayEvent.CALL_DELETE: - OnCallDelete?.Invoke(this); - break; - - case GatewayEvent.CALL_UPDATE: - OnCallUpdate?.Invoke(this); - break; - - case GatewayEvent.VOICE_SERVER_UPDATE: - OnVoiceServerUpdate?.Invoke(this); - break; - - case GatewayEvent.VOICE_STATE_UPDATE: - OnVoiceStateUpdate?.Invoke(this); - break; - #endregion - - #region Channel - case GatewayEvent.CHANNEL_CREATE: - OnChannelCreate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.CHANNEL_DELETE: - OnChannelDelete?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.CHANNEL_PINS_ACK: - OnChannelPinsAck?.Invoke(this); - break; - - case GatewayEvent.CHANNEL_PINS_UPDATE: - OnChannelPinsUpdate?.Invoke(this); - break; - - case GatewayEvent.CHANNEL_RECIPIENT_ADD: - OnChannelRecipientAdd?.Invoke(this); - break; - - case GatewayEvent.CHANNEL_RECIPIENT_REMOVE: - OnChannelRecipientRemove?.Invoke(this); - break; - - case GatewayEvent.CHANNEL_UPDATE: - OnChannelUpdate?.Invoke(this, payload.DataAs()); - break; - #endregion - - #region Guild - case GatewayEvent.GUILD_BAN_ADD: - OnGuildBanAdd?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_BAN_REMOVE: - OnGuildBanRemove?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_CREATE: - OnGuildCreate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_DELETE: - OnGuildDelete?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_EMOJIS_UPDATE: - OnGuildEmojisUpdate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_INTEGRATIONS_UPDATE: - OnGuildIntegrationsUpdate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_MEMBER_ADD: - OnGuildMemberAdd?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_MEMBER_UPDATE: - OnGuildMemberUpdate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_MEMBER_REMOVE: - OnGuildMemberRemove?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_MEMBERS_CHUNK: - OnGuildMembersChunk?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_ROLE_CREATE: - OnGuildRoleCreate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_ROLE_UPDATE: - OnGuildRoleUpdate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_ROLE_DELETE: - OnGuildRoleDelete?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.GUILD_UPDATE: - OnGuildUpdate?.Invoke(this, payload.DataAs()); - break; - #endregion - - #region Message - case GatewayEvent.MESSAGE_ACK: - OnMessageAck?.Invoke(this); - break; - - case GatewayEvent.MESSAGE_CREATE: - OnMessageCreate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.MESSAGE_DELETE: - OnMessageDelete?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.MESSAGE_DELETE_BULK: - OnMessageDeleteBulk?.Invoke(this); - break; - - case GatewayEvent.MESSAGE_REACTION_ADD: - OnMessageReactionAdd?.Invoke(this); - break; - - case GatewayEvent.MESSAGE_REACTION_REMOVE: - OnMessageReactionRemove?.Invoke(this); - break; - - case GatewayEvent.MESSAGE_REACTIONS_REMOVE_ALL: - OnMessageReactionsRemoveAll?.Invoke(this); - break; - - case GatewayEvent.MESSAGE_UPDATE: - OnMessageUpdate?.Invoke(this, payload.DataAs()); - break; - #endregion - - #region Presence - case GatewayEvent.TYPING_START: - OnTypingStart?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.PRESENCE_UPDATE: - OnPresenceUpdate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.PRESENCES_REPLACE: - OnPresencesReplace?.Invoke(this); - break; - #endregion - - #region Relations - case GatewayEvent.FRIEND_SUGGESTION_DELETE: - OnFriendSuggestionDelete?.Invoke(this); - break; - - case GatewayEvent.RELATIONSHIP_ADD: - OnRelationshipAdd?.Invoke(this); - break; - - case GatewayEvent.RELATIONSHIP_REMOVE: - OnRelationshipRemove?.Invoke(this); - break; - #endregion - - #region State - case GatewayEvent.READY: - GatewayReady ready = payload.DataAs(); - session = ready.Session; - OnReady?.Invoke(this, ready); - break; - - case GatewayEvent.RESUMED: - OnResumed?.Invoke(this); - break; - #endregion - - #region User - case GatewayEvent.USER_UPDATE: - OnUserUpdate?.Invoke(this, payload.DataAs()); - break; - - case GatewayEvent.USER_NOTE_UPDATE: - OnUserNoteUpdate?.Invoke(this); - break; - - case GatewayEvent.USER_SETTINGS_UPDATE: - OnUserSettingsUpdate?.Invoke(this); - break; - #endregion - - default: - Console.WriteLine($"Unknown payload type: {payload.Name}"); - break; + if (payload.Name == "READY") + { + GatewayReady ready = payload.DataAs(); + Session = ready.Session; } break; @@ -380,15 +107,17 @@ namespace Maki.Gateway GatewayHello hello = payload.DataAs(); HeartbeatInterval = TimeSpan.FromMilliseconds(hello.HeartbeatInterval); - heartbeatHandler.Start(); + HeartbeatHandler.Start(); - if (string.IsNullOrEmpty(session)) - Send(GatewayOPCode.Identify, new GatewayIdentification { - Token = client.Token, + if (string.IsNullOrEmpty(Session)) + Send(GatewayOPCode.Identify, new GatewayIdentification + { + Token = Client.Token, Compress = false, LargeThreshold = 250, - Shard = new int[] { Id, client.ShardClient.ShardCount }, - Properties = new GatewayIdentificationProperties { + Shard = new int[] { Id, Client.ShardClient.ShardCount }, + Properties = new GatewayIdentificationProperties + { OperatingSystem = "windows", Browser = "Maki", Device = "Maki", @@ -397,10 +126,11 @@ namespace Maki.Gateway } }); else - Send(GatewayOPCode.Resume, new GatewayResume { + Send(GatewayOPCode.Resume, new GatewayResume + { LastSequence = LastSequence ?? default(int), - Token = client.Token, - Session = session, + Token = Client.Token, + Session = Session, }); break; } @@ -435,14 +165,12 @@ namespace Maki.Gateway /// public void Connect() { - WebSocket = new WebSocket($"{client.Gateway}?v={Discord.GATEWAY_VERSION}&encoding=json"); + WebSocket = new WebSocket($"{Client.Gateway}?v={Discord.GATEWAY_VERSION}&encoding=json"); // make wss not log anything on its own WebSocket.Log.Output = (LogData logData, string path) => { }; - WebSocket.OnOpen += WebSocket_OnOpen; WebSocket.OnClose += WebSocket_OnClose; - WebSocket.OnError += WebSocket_OnError; WebSocket.OnMessage += WebSocket_OnMessage; WebSocket.Connect(); @@ -453,7 +181,7 @@ namespace Maki.Gateway /// public void Disconnect() { - heartbeatHandler.Stop(); + HeartbeatHandler.Stop(); if (WebSocket.ReadyState != WebSocketState.Closed) WebSocket?.Close(CloseStatusCode.Normal); @@ -468,7 +196,7 @@ namespace Maki.Gateway { string json = JsonConvert.SerializeObject(data, typeof(T), new JsonSerializerSettings()); - Console.WriteLine(json.Replace(client.Token, new string('*', client.Token.Length))); + Console.WriteLine(json.Replace(Client.Token, new string('*', Client.Token.Length))); WebSocket.Send(json); } @@ -479,12 +207,10 @@ namespace Maki.Gateway /// Opcode to use /// Data to send public void Send(GatewayOPCode opcode, object data) - { - Send(new GatewayPayload + => Send(new GatewayPayload { OPCode = opcode, Data = data }); - } } } diff --git a/Maki/Gateway/GatewayShardClient.cs b/Maki/Gateway/GatewayShardClient.cs index 53a11aa..63b5595 100644 --- a/Maki/Gateway/GatewayShardClient.cs +++ b/Maki/Gateway/GatewayShardClient.cs @@ -48,7 +48,7 @@ namespace Maki.Gateway public GatewayShard Create(int id) { GatewayShard shard = new GatewayShard(id, Client); - ApplyEvents(shard); + shard.OnSocketPayload += Shard_OnSocketPayload; lock (Lock) Shards.Add(shard); @@ -56,6 +56,219 @@ namespace Maki.Gateway return shard; } + private void Shard_OnSocketPayload(GatewayShard shard, GatewayPayload payload) + { + switch (payload.OPCode) + { + case GatewayOPCode.Dispatch: + switch (payload.Name) + { + #region Call + case "CALL_CREATE": + OnCallCreate?.Invoke(shard); + break; + + case "CALL_DELETE": + OnCallDelete?.Invoke(shard); + break; + + case "CALL_UPDATE": + OnCallUpdate?.Invoke(shard); + break; + + case "VOICE_SERVER_UPDATE": + OnVoiceServerUpdate?.Invoke(shard); + break; + + case "VOICE_STATE_UPDATE": + OnVoiceStateUpdate?.Invoke(shard); + break; + #endregion + + #region Channel + case "CHANNEL_CREATE": + OnChannelCreate?.Invoke(shard, payload.DataAs()); + break; + + case "CHANNEL_DELETE": + OnChannelDelete?.Invoke(shard, payload.DataAs()); + break; + + case "CHANNEL_PINS_ACK": + OnChannelPinsAck?.Invoke(shard); + break; + + case "CHANNEL_PINS_UPDATE": + OnChannelPinsUpdate?.Invoke(shard); + break; + + case "CHANNEL_RECIPIENT_ADD": + OnChannelRecipientAdd?.Invoke(shard); + break; + + case "CHANNEL_RECIPIENT_REMOVE": + OnChannelRecipientRemove?.Invoke(shard); + break; + + case "CHANNEL_UPDATE": + OnChannelUpdate?.Invoke(shard, payload.DataAs()); + break; + #endregion + + #region Guild + case "GUILD_BAN_ADD": + OnGuildBanAdd?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_BAN_REMOVE": + OnGuildBanRemove?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_CREATE": + OnGuildCreate?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_DELETE": + OnGuildDelete?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_EMOJIS_UPDATE": + OnGuildEmojisUpdate?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_INTEGRATIONS_UPDATE": + OnGuildIntegrationsUpdate?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_MEMBER_ADD": + OnGuildMemberAdd?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_MEMBER_UPDATE": + OnGuildMemberUpdate?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_MEMBER_REMOVE": + OnGuildMemberRemove?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_MEMBERS_CHUNK": + OnGuildMembersChunk?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_ROLE_CREATE": + OnGuildRoleCreate?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_ROLE_UPDATE": + OnGuildRoleUpdate?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_ROLE_DELETE": + OnGuildRoleDelete?.Invoke(shard, payload.DataAs()); + break; + + case "GUILD_UPDATE": + OnGuildUpdate?.Invoke(shard, payload.DataAs()); + break; + #endregion + + #region Message + case "MESSAGE_ACK": + OnMessageAck?.Invoke(shard); + break; + + case "MESSAGE_CREATE": + OnMessageCreate?.Invoke(shard, payload.DataAs()); + break; + + case "MESSAGE_DELETE": + OnMessageDelete?.Invoke(shard, payload.DataAs()); + break; + + case "MESSAGE_DELETE_BULK": + OnMessageDeleteBulk?.Invoke(shard); + break; + + case "MESSAGE_REACTION_ADD": + OnMessageReactionAdd?.Invoke(shard); + break; + + case "MESSAGE_REACTION_REMOVE": + OnMessageReactionRemove?.Invoke(shard); + break; + + case "MESSAGE_REACTIONS_REMOVE_ALL": + OnMessageReactionsRemoveAll?.Invoke(shard); + break; + + case "MESSAGE_UPDATE": + OnMessageUpdate?.Invoke(shard, payload.DataAs()); + break; + #endregion + + #region Presence + case "TYPING_START": + OnTypingStart?.Invoke(shard, payload.DataAs()); + break; + + case "PRESENCE_UPDATE": + OnPresenceUpdate?.Invoke(shard, payload.DataAs()); + break; + + case "PRESENCES_REPLACE": + OnPresencesReplace?.Invoke(shard); + break; + #endregion + + #region Relations + case "FRIEND_SUGGESTION_DELETE": + OnFriendSuggestionDelete?.Invoke(shard); + break; + + case "RELATIONSHIP_ADD": + OnRelationshipAdd?.Invoke(shard); + break; + + case "RELATIONSHIP_REMOVE": + OnRelationshipRemove?.Invoke(shard); + break; + #endregion + + #region State + case "READY": + OnReady?.Invoke(shard, payload.DataAs()); + break; + + case "RESUMED": + OnResumed?.Invoke(shard); + break; + #endregion + + #region User + case "USER_UPDATE": + OnUserUpdate?.Invoke(shard, payload.DataAs()); + break; + + case "USER_NOTE_UPDATE": + OnUserNoteUpdate?.Invoke(shard); + break; + + case "USER_SETTINGS_UPDATE": + OnUserSettingsUpdate?.Invoke(shard); + break; + #endregion + +#if DEBUG + default: + Console.WriteLine($"Unknown payload type: {payload.Name}"); + break; +#endif + } + break; + } + } + /// /// Destroys a Gateway Shard /// @@ -67,7 +280,7 @@ namespace Maki.Gateway // Disconnect and Dispose are called separately so OnSocketClose is called properly shard.Disconnect(); - RemoveEvents(shard); + shard.OnSocketPayload -= Shard_OnSocketPayload; shard.Dispose(); } @@ -80,137 +293,8 @@ namespace Maki.Gateway Shards.ToList().ForEach(x => Destroy(x)); } - /// - /// Links all gateway events handlers to the shard - /// - /// Shard to link events to - private void ApplyEvents(GatewayShard shard) - { - shard.OnCallCreate += OnCallCreate; - shard.OnCallDelete += OnCallDelete; - shard.OnCallUpdate += OnCallUpdate; - shard.OnVoiceServerUpdate += OnVoiceServerUpdate; - shard.OnVoiceStateUpdate += OnVoiceStateUpdate; - - shard.OnChannelCreate += OnChannelCreate; - shard.OnChannelDelete += OnChannelDelete; - shard.OnChannelPinsAck += OnChannelPinsAck; - shard.OnChannelPinsUpdate += OnChannelPinsUpdate; - shard.OnChannelRecipientAdd += OnChannelRecipientAdd; - shard.OnChannelRecipientRemove += OnChannelRecipientRemove; - shard.OnChannelUpdate += OnChannelUpdate; - - shard.OnGuildBanAdd += OnGuildBanAdd; - shard.OnGuildBanRemove += OnGuildBanRemove; - shard.OnGuildCreate += OnGuildCreate; - shard.OnGuildDelete += OnGuildDelete; - shard.OnGuildEmojisUpdate += OnGuildEmojisUpdate; - shard.OnGuildIntegrationsUpdate += OnGuildIntegrationsUpdate; - shard.OnGuildMemberAdd += OnGuildMemberAdd; - shard.OnGuildMemberUpdate += OnGuildMemberUpdate; - shard.OnGuildMemberRemove += OnGuildMemberRemove; - shard.OnGuildMembersChunk += OnGuildMembersChunk; - shard.OnGuildRoleCreate += OnGuildRoleCreate; - shard.OnGuildRoleUpdate += OnGuildRoleUpdate; - shard.OnGuildRoleDelete += OnGuildRoleDelete; - shard.OnGuildUpdate += OnGuildUpdate; - - shard.OnMessageAck += OnMessageAck; - shard.OnMessageCreate += OnMessageCreate; - shard.OnMessageDelete += OnMessageDelete; - shard.OnMessageDeleteBulk += OnMessageDeleteBulk; - shard.OnMessageReactionAdd += OnMessageReactionAdd; - shard.OnMessageReactionRemove += OnMessageReactionRemove; - shard.OnMessageReactionsRemoveAll += OnMessageReactionsRemoveAll; - shard.OnMessageUpdate += OnMessageUpdate; - - shard.OnTypingStart += OnTypingStart; - shard.OnPresenceUpdate += OnPresenceUpdate; - shard.OnPresencesReplace += OnPresencesReplace; - - shard.OnFriendSuggestionDelete += OnFriendSuggestionDelete; - shard.OnRelationshipAdd += OnRelationshipAdd; - shard.OnRelationshipRemove += OnRelationshipRemove; - - shard.OnReady += OnReady; - shard.OnResumed += OnResumed; - - shard.OnUserUpdate += OnUserUpdate; - shard.OnUserNoteUpdate += OnUserNoteUpdate; - shard.OnUserSettingsUpdate += OnUserSettingsUpdate; - - shard.OnSocketOpen += OnSocketOpen; - shard.OnSocketClose += OnSocketClose; - shard.OnSocketError += OnSocketError; - shard.OnSocketMessage += OnSocketMessage; - } - - /// - /// Unlinks all gateway events handlers from the shard - /// - /// Shard to unlink events from - public void RemoveEvents(GatewayShard shard) - { - shard.OnCallCreate -= OnCallCreate; - shard.OnCallDelete -= OnCallDelete; - shard.OnCallUpdate -= OnCallUpdate; - shard.OnVoiceServerUpdate -= OnVoiceServerUpdate; - shard.OnVoiceStateUpdate -= OnVoiceStateUpdate; - - shard.OnChannelCreate -= OnChannelCreate; - shard.OnChannelDelete -= OnChannelDelete; - shard.OnChannelPinsAck -= OnChannelPinsAck; - shard.OnChannelPinsUpdate -= OnChannelPinsUpdate; - shard.OnChannelRecipientAdd -= OnChannelRecipientAdd; - shard.OnChannelRecipientRemove -= OnChannelRecipientRemove; - shard.OnChannelUpdate -= OnChannelUpdate; - - shard.OnGuildBanAdd -= OnGuildBanAdd; - shard.OnGuildBanRemove -= OnGuildBanRemove; - shard.OnGuildCreate -= OnGuildCreate; - shard.OnGuildDelete -= OnGuildDelete; - shard.OnGuildEmojisUpdate -= OnGuildEmojisUpdate; - shard.OnGuildIntegrationsUpdate -= OnGuildIntegrationsUpdate; - shard.OnGuildMemberAdd -= OnGuildMemberAdd; - shard.OnGuildMemberUpdate -= OnGuildMemberUpdate; - shard.OnGuildMemberRemove -= OnGuildMemberRemove; - shard.OnGuildMembersChunk -= OnGuildMembersChunk; - shard.OnGuildRoleCreate -= OnGuildRoleCreate; - shard.OnGuildRoleUpdate -= OnGuildRoleUpdate; - shard.OnGuildRoleDelete -= OnGuildRoleDelete; - shard.OnGuildUpdate -= OnGuildUpdate; - - shard.OnMessageAck -= OnMessageAck; - shard.OnMessageCreate -= OnMessageCreate; - shard.OnMessageDelete -= OnMessageDelete; - shard.OnMessageDeleteBulk -= OnMessageDeleteBulk; - shard.OnMessageReactionAdd -= OnMessageReactionAdd; - shard.OnMessageReactionRemove -= OnMessageReactionRemove; - shard.OnMessageReactionsRemoveAll -= OnMessageReactionsRemoveAll; - shard.OnMessageUpdate -= OnMessageUpdate; - - shard.OnTypingStart -= OnTypingStart; - shard.OnPresenceUpdate -= OnPresenceUpdate; - shard.OnPresencesReplace -= OnPresencesReplace; - - shard.OnFriendSuggestionDelete -= OnFriendSuggestionDelete; - shard.OnRelationshipAdd -= OnRelationshipAdd; - shard.OnRelationshipRemove -= OnRelationshipRemove; - - shard.OnReady -= OnReady; - shard.OnResumed -= OnResumed; - - shard.OnUserUpdate -= OnUserUpdate; - shard.OnUserNoteUpdate -= OnUserNoteUpdate; - shard.OnUserSettingsUpdate -= OnUserSettingsUpdate; - - shard.OnSocketOpen -= OnSocketOpen; - shard.OnSocketClose -= OnSocketClose; - shard.OnSocketError -= OnSocketError; - shard.OnSocketMessage -= OnSocketMessage; - } - #region Events + public event Action OnCallCreate; public event Action OnCallDelete; public event Action OnCallUpdate; @@ -264,10 +348,6 @@ namespace Maki.Gateway public event Action OnUserNoteUpdate; public event Action OnUserSettingsUpdate; - public event Action OnSocketOpen; - public event Action OnSocketClose; - public event Action OnSocketError; - public event Action OnSocketMessage; #endregion #region IDisposable diff --git a/Maki/Maki.csproj b/Maki/Maki.csproj index 729d9b8..4530d90 100644 --- a/Maki/Maki.csproj +++ b/Maki/Maki.csproj @@ -64,7 +64,6 @@ -