From cd5d3fa2425bcd890bca96ef3136242404017d53 Mon Sep 17 00:00:00 2001 From: Quin Lynch Date: Tue, 8 Feb 2022 22:05:15 -0400 Subject: [PATCH] fix attempts to fetch channels in interactions --- .../Entities/Interactions/RestInteraction.cs | 7 +- .../DiscordSocketClient.cs | 72 +++++++------------ .../Entities/Interaction/SocketInteraction.cs | 4 ++ 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs index c4d66c642..566d60d14 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Model = Discord.API.Interaction; using DataModel = Discord.API.ApplicationCommandInteractionData; using Newtonsoft.Json; +using Discord.Net; namespace Discord.Rest { @@ -130,7 +131,11 @@ namespace Discord.Rest if(Channel == null && model.ChannelId.IsSpecified) { - Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); + try + { + Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); + } + catch(HttpException x) when(x.DiscordCode == DiscordErrorCode.MissingPermissions) { } // ignore } UserLocale = model.UserLocale.IsSpecified diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 349e2714f..869a91e6e 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -2238,57 +2238,39 @@ namespace Discord.WebSocket channel = State.GetDMChannel(data.User.Value.Id); } - if (channel == null) - { - var channelModel = await Rest.ApiClient.GetChannelAsync(data.ChannelId.Value); - - if (data.GuildId.IsSpecified) - channel = SocketTextChannel.Create(State.GetGuild(data.GuildId.Value), State, channelModel); - else - channel = (SocketChannel)SocketChannel.CreatePrivate(this, State, channelModel); + channel = null; - State.AddChannel(channel); - } - - if (channel is ISocketMessageChannel textChannel) + var guild = (channel as SocketGuildChannel)?.Guild; + if (guild != null && !guild.IsSynced) { - var guild = (channel as SocketGuildChannel)?.Guild; - if (guild != null && !guild.IsSynced) - { - await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); - return; - } + await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); + return; + } - var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel); + var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel); - await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false); + await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false); - switch (interaction) - { - case SocketSlashCommand slashCommand: - await TimedInvokeAsync(_slashCommandExecuted, nameof(SlashCommandExecuted), slashCommand).ConfigureAwait(false); - break; - case SocketMessageComponent messageComponent: - if(messageComponent.Data.Type == ComponentType.SelectMenu) - await TimedInvokeAsync(_selectMenuExecuted, nameof(SelectMenuExecuted), messageComponent).ConfigureAwait(false); - if(messageComponent.Data.Type == ComponentType.Button) - await TimedInvokeAsync(_buttonExecuted, nameof(ButtonExecuted), messageComponent).ConfigureAwait(false); - break; - case SocketUserCommand userCommand: - await TimedInvokeAsync(_userCommandExecuted, nameof(UserCommandExecuted), userCommand).ConfigureAwait(false); - break; - case SocketMessageCommand messageCommand: - await TimedInvokeAsync(_messageCommandExecuted, nameof(MessageCommandExecuted), messageCommand).ConfigureAwait(false); - break; - case SocketAutocompleteInteraction autocomplete: - await TimedInvokeAsync(_autocompleteExecuted, nameof(AutocompleteExecuted), autocomplete).ConfigureAwait(false); - break; - } - } - else + switch (interaction) { - await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false); - return; + case SocketSlashCommand slashCommand: + await TimedInvokeAsync(_slashCommandExecuted, nameof(SlashCommandExecuted), slashCommand).ConfigureAwait(false); + break; + case SocketMessageComponent messageComponent: + if (messageComponent.Data.Type == ComponentType.SelectMenu) + await TimedInvokeAsync(_selectMenuExecuted, nameof(SelectMenuExecuted), messageComponent).ConfigureAwait(false); + if (messageComponent.Data.Type == ComponentType.Button) + await TimedInvokeAsync(_buttonExecuted, nameof(ButtonExecuted), messageComponent).ConfigureAwait(false); + break; + case SocketUserCommand userCommand: + await TimedInvokeAsync(_userCommandExecuted, nameof(UserCommandExecuted), userCommand).ConfigureAwait(false); + break; + case SocketMessageCommand messageCommand: + await TimedInvokeAsync(_messageCommandExecuted, nameof(MessageCommandExecuted), messageCommand).ConfigureAwait(false); + break; + case SocketAutocompleteInteraction autocomplete: + await TimedInvokeAsync(_autocompleteExecuted, nameof(AutocompleteExecuted), autocomplete).ConfigureAwait(false); + break; } } break; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index b53739553..985e8e0d9 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -17,6 +17,10 @@ namespace Discord.WebSocket /// /// The this interaction was used in. /// + /// + /// If the channel isn't cached or the bot doesn't have access to it then + /// this property will be . + /// public ISocketMessageChannel Channel { get; private set; } ///