From 010e8e828ff87212ebe8c2539cdc59c3949fa8ca Mon Sep 17 00:00:00 2001 From: CottageDwellingCat <80918250+CottageDwellingCat@users.noreply.github.com> Date: Mon, 1 Aug 2022 10:38:10 -0500 Subject: [PATCH] feat: Support the WEBHOOKS_UPDATED event (#2384) Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> --- .../API/Gateway/WebhooksUpdatedEvent.cs | 13 ++++++ .../BaseSocketClient.Events.cs | 41 +++++++++++++------ .../DiscordShardedClient.cs | 2 + .../DiscordSocketClient.cs | 20 +++++++-- 4 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 src/Discord.Net.WebSocket/API/Gateway/WebhooksUpdatedEvent.cs diff --git a/src/Discord.Net.WebSocket/API/Gateway/WebhooksUpdatedEvent.cs b/src/Discord.Net.WebSocket/API/Gateway/WebhooksUpdatedEvent.cs new file mode 100644 index 000000000..5555dc842 --- /dev/null +++ b/src/Discord.Net.WebSocket/API/Gateway/WebhooksUpdatedEvent.cs @@ -0,0 +1,13 @@ +using Newtonsoft.Json; + +namespace Discord.API.Gateway +{ + internal class WebhooksUpdatedEvent + { + [JsonProperty("guild_id")] + public ulong GuildId { get; set; } + + [JsonProperty("channel_id")] + public ulong ChannelId { get; set; } + } +} diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs index c47591418..21b61c35c 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs @@ -106,7 +106,7 @@ namespace Discord.WebSocket /// /// /// This event is fired when a message is deleted. The event handler must return a - /// and accept a and + /// and accept a and /// as its parameters. /// /// @@ -117,11 +117,11 @@ namespace Discord.WebSocket /// /// If caching is enabled via , the /// entity will contain the deleted message; otherwise, in event - /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the + /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the /// . /// /// - /// The source channel of the removed message will be passed into the + /// The source channel of the removed message will be passed into the /// parameter. /// /// @@ -143,7 +143,7 @@ namespace Discord.WebSocket /// /// /// This event is fired when multiple messages are bulk deleted. The event handler must return a - /// and accept an and + /// and accept an and /// as its parameters. /// /// @@ -154,11 +154,11 @@ namespace Discord.WebSocket /// /// If caching is enabled via , the /// entity will contain the deleted message; otherwise, in event - /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the + /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the /// . /// /// - /// The source channel of the removed message will be passed into the + /// The source channel of the removed message will be passed into the /// parameter. /// /// @@ -178,14 +178,14 @@ namespace Discord.WebSocket /// /// If caching is enabled via , the /// entity will contain the original message; otherwise, in event - /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the + /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the /// . /// /// /// The updated message will be passed into the parameter. /// /// - /// The source channel of the updated message will be passed into the + /// The source channel of the updated message will be passed into the /// parameter. /// /// @@ -199,24 +199,24 @@ namespace Discord.WebSocket /// /// /// This event is fired when a reaction is added to a user message. The event handler must return a - /// and accept a , an + /// and accept a , an /// , and a as its parameter. /// /// /// If caching is enabled via , the /// entity will contain the original message; otherwise, in event - /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the + /// that the message cannot be retrieved, the snowflake ID of the message is preserved in the /// . /// /// - /// The source channel of the reaction addition will be passed into the + /// The source channel of the reaction addition will be passed into the /// parameter. /// /// /// The reaction that was added will be passed into the parameter. /// /// - /// When fetching the reaction from this event, a user may not be provided under + /// When fetching the reaction from this event, a user may not be provided under /// . Please see the documentation of the property for more /// information. /// @@ -367,7 +367,7 @@ namespace Discord.WebSocket } internal readonly AsyncEvent, SocketGuildEvent, Task>> _guildScheduledEventUpdated = new AsyncEvent, SocketGuildEvent, Task>>(); - + /// /// Fired when a guild event is cancelled. /// @@ -877,5 +877,20 @@ namespace Discord.WebSocket } internal readonly AsyncEvent> _guildStickerDeleted = new AsyncEvent>(); #endregion + + #region Webhooks + + /// + /// Fired when a webhook is modified, moved, or deleted. If the webhook was + /// moved the channel represents the destination channel, not the source. + /// + public event Func WebhooksUpdated + { + add { _webhooksUpdated.Add(value); } + remove { _webhooksUpdated.Remove(value); } + } + internal readonly AsyncEvent> _webhooksUpdated = new AsyncEvent>(); + + #endregion } } diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 3a14692e0..9fc717762 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -496,6 +496,8 @@ namespace Discord.WebSocket client.GuildScheduledEventStarted += (arg) => _guildScheduledEventStarted.InvokeAsync(arg); client.GuildScheduledEventUserAdd += (arg1, arg2) => _guildScheduledEventUserAdd.InvokeAsync(arg1, arg2); client.GuildScheduledEventUserRemove += (arg1, arg2) => _guildScheduledEventUserRemove.InvokeAsync(arg1, arg2); + + client.WebhooksUpdated += (arg1, arg2) => _webhooksUpdated.InvokeAsync(arg1, arg2); } #endregion diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 5743d9abd..b87b52ffb 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -2839,6 +2839,23 @@ namespace Discord.WebSocket #endregion + #region Webhooks + + case "WEBHOOKS_UPDATE": + { + var data = (payload as JToken).ToObject(_serializer); + type = "WEBHOOKS_UPDATE"; + await _gatewayLogger.DebugAsync("Received Dispatch (WEBHOOKS_UPDATE)").ConfigureAwait(false); + + var guild = State.GetGuild(data.GuildId); + var channel = State.GetChannel(data.ChanelId); + + await TimedInvokeAsync(_webhooksUpdated, nameof(WebhooksUpdated), guild, channel); + } + break; + + #endregion + #region Ignored (User only) case "CHANNEL_PINS_ACK": await _gatewayLogger.DebugAsync("Ignored Dispatch (CHANNEL_PINS_ACK)").ConfigureAwait(false); @@ -2858,9 +2875,6 @@ namespace Discord.WebSocket case "USER_SETTINGS_UPDATE": await _gatewayLogger.DebugAsync("Ignored Dispatch (USER_SETTINGS_UPDATE)").ConfigureAwait(false); break; - case "WEBHOOKS_UPDATE": - await _gatewayLogger.DebugAsync("Ignored Dispatch (WEBHOOKS_UPDATE)").ConfigureAwait(false); - break; #endregion #region Others