From e17794e58e551b0ef193e7d0d9c3367c6c8846a9 Mon Sep 17 00:00:00 2001 From: quin lynch Date: Sun, 30 May 2021 08:58:33 -0300 Subject: [PATCH] Update channel on SocketInteraction --- .../DiscordSocketClient.cs | 7 +++-- .../SocketMessageComponent.cs | 8 ++--- .../Slash Commands/SocketSlashCommand.cs | 8 ++--- .../Entities/Interaction/SocketInteraction.cs | 31 ++++++------------- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index ad58c19ac..f4a862998 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1786,7 +1786,7 @@ namespace Discord.WebSocket var data = (payload as JToken).ToObject(_serializer); - SocketChannel channel; + SocketChannel channel = null; if(data.ChannelId.IsSpecified) { channel = State.GetChannel(data.ChannelId.Value); @@ -1795,7 +1795,8 @@ namespace Discord.WebSocket { channel = State.GetDMChannel(data.User.Value.Id); } - else + + if(channel == null) { await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false); return; @@ -1808,7 +1809,7 @@ namespace Discord.WebSocket return; } - var interaction = SocketInteraction.Create(this, data); + var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel); if (this.AlwaysAcknowledgeInteractions) await interaction.AcknowledgeAsync().ConfigureAwait(false); diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs index 2afc474c1..a6ad7ed1c 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs @@ -25,8 +25,8 @@ namespace Discord.WebSocket /// public SocketMessage Message { get; private set; } - internal SocketMessageComponent(DiscordSocketClient client, Model model) - : base(client, model.Id) + internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel) + : base(client, model.Id, channel) { var dataModel = model.Data.IsSpecified ? (model.Data.Value as JToken).ToObject() @@ -37,9 +37,9 @@ namespace Discord.WebSocket } - new internal static SocketMessageComponent Create(DiscordSocketClient client, Model model) + new internal static SocketMessageComponent Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel) { - var entity = new SocketMessageComponent(client, model); + var entity = new SocketMessageComponent(client, model, channel); entity.Update(model); return entity; } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs index 3561ac8c0..faf50e38e 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs @@ -18,8 +18,8 @@ namespace Discord.WebSocket /// new public SocketSlashCommandData Data { get; private set; } - internal SocketSlashCommand(DiscordSocketClient client, Model model) - : base(client, model.Id) + internal SocketSlashCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel) + : base(client, model.Id, channel) { var dataModel = model.Data.IsSpecified ? (model.Data.Value as JToken).ToObject() @@ -28,9 +28,9 @@ namespace Discord.WebSocket Data = SocketSlashCommandData.Create(client, dataModel, model.Id); } - new internal static SocketInteraction Create(DiscordSocketClient client, Model model) + new internal static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel) { - var entity = new SocketSlashCommand(client, model); + var entity = new SocketSlashCommand(client, model, channel); entity.Update(model); return entity; } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index 62f0debef..4b213f4ec 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -54,17 +54,18 @@ namespace Discord.WebSocket private ulong? GuildId { get; set; } private ulong? ChannelId { get; set; } - internal SocketInteraction(DiscordSocketClient client, ulong id) + internal SocketInteraction(DiscordSocketClient client, ulong id, ISocketMessageChannel channel) : base(client, id) { + this.Channel = channel; } - internal static SocketInteraction Create(DiscordSocketClient client, Model model) + internal static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel) { if (model.Type == InteractionType.ApplicationCommand) - return SocketSlashCommand.Create(client, model); + return SocketSlashCommand.Create(client, model, channel); if (model.Type == InteractionType.MessageComponent) - return SocketMessageComponent.Create(client, model); + return SocketMessageComponent.Create(client, model, channel); else return null; } @@ -92,18 +93,6 @@ namespace Discord.WebSocket this.User = SocketGlobalUser.Create(this.Discord, this.Discord.State, model.User.Value); } } - - if (this.Channel == null) - { - if (model.ChannelId.IsSpecified) - { - this.Channel = Discord.State.GetChannel(model.ChannelId.Value) as ISocketMessageChannel; - } - else - { - this.Channel = Discord.State.GetDMChannel(this.User.Id); - } - } } /// @@ -127,9 +116,8 @@ namespace Discord.WebSocket /// Message content is too long, length must be less or equal to . /// The parameters provided were invalid or the token was invalid. - public virtual Task RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, - bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) - { return null; } + public abstract Task RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, + bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null); /// /// Sends a followup message for this interaction. @@ -145,10 +133,9 @@ namespace Discord.WebSocket /// /// The sent message. /// - public virtual Task FollowupAsync(string text = null, bool isTTS = false, Embed embed = null, bool ephemeral = false, + public abstract Task FollowupAsync(string text = null, bool isTTS = false, Embed embed = null, bool ephemeral = false, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, - AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) - { return null; } + AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null); /// /// Acknowledges this interaction with the .