Skip to content

Commit 6290f75

Browse files
authored
Fix attempts to fetch channels in interactions (#2090)
* fix attempts to fetch channels in interactions * remove test case
1 parent 97e54e1 commit 6290f75

File tree

3 files changed

+35
-46
lines changed

3 files changed

+35
-46
lines changed

src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Model = Discord.API.Interaction;
88
using DataModel = Discord.API.ApplicationCommandInteractionData;
99
using Newtonsoft.Json;
10+
using Discord.Net;
1011

1112
namespace Discord.Rest
1213
{
@@ -130,7 +131,11 @@ internal virtual async Task UpdateAsync(DiscordRestClient discord, Model model)
130131

131132
if(Channel == null && model.ChannelId.IsSpecified)
132133
{
133-
Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value);
134+
try
135+
{
136+
Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value);
137+
}
138+
catch(HttpException x) when(x.DiscordCode == DiscordErrorCode.MissingPermissions) { } // ignore
134139
}
135140

136141
UserLocale = model.UserLocale.IsSpecified

src/Discord.Net.WebSocket/DiscordSocketClient.cs

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,57 +2243,37 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
22432243
channel = State.GetDMChannel(data.User.Value.Id);
22442244
}
22452245

2246-
if (channel == null)
2246+
var guild = (channel as SocketGuildChannel)?.Guild;
2247+
if (guild != null && !guild.IsSynced)
22472248
{
2248-
var channelModel = await Rest.ApiClient.GetChannelAsync(data.ChannelId.Value);
2249-
2250-
if (data.GuildId.IsSpecified)
2251-
channel = SocketTextChannel.Create(State.GetGuild(data.GuildId.Value), State, channelModel);
2252-
else
2253-
channel = (SocketChannel)SocketChannel.CreatePrivate(this, State, channelModel);
2254-
2255-
State.AddChannel(channel);
2249+
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
2250+
return;
22562251
}
22572252

2258-
if (channel is ISocketMessageChannel textChannel)
2259-
{
2260-
var guild = (channel as SocketGuildChannel)?.Guild;
2261-
if (guild != null && !guild.IsSynced)
2262-
{
2263-
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
2264-
return;
2265-
}
2266-
2267-
var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel);
2253+
var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel);
22682254

2269-
await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false);
2255+
await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false);
22702256

2271-
switch (interaction)
2272-
{
2273-
case SocketSlashCommand slashCommand:
2274-
await TimedInvokeAsync(_slashCommandExecuted, nameof(SlashCommandExecuted), slashCommand).ConfigureAwait(false);
2275-
break;
2276-
case SocketMessageComponent messageComponent:
2277-
if(messageComponent.Data.Type == ComponentType.SelectMenu)
2278-
await TimedInvokeAsync(_selectMenuExecuted, nameof(SelectMenuExecuted), messageComponent).ConfigureAwait(false);
2279-
if(messageComponent.Data.Type == ComponentType.Button)
2280-
await TimedInvokeAsync(_buttonExecuted, nameof(ButtonExecuted), messageComponent).ConfigureAwait(false);
2281-
break;
2282-
case SocketUserCommand userCommand:
2283-
await TimedInvokeAsync(_userCommandExecuted, nameof(UserCommandExecuted), userCommand).ConfigureAwait(false);
2284-
break;
2285-
case SocketMessageCommand messageCommand:
2286-
await TimedInvokeAsync(_messageCommandExecuted, nameof(MessageCommandExecuted), messageCommand).ConfigureAwait(false);
2287-
break;
2288-
case SocketAutocompleteInteraction autocomplete:
2289-
await TimedInvokeAsync(_autocompleteExecuted, nameof(AutocompleteExecuted), autocomplete).ConfigureAwait(false);
2290-
break;
2291-
}
2292-
}
2293-
else
2257+
switch (interaction)
22942258
{
2295-
await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false);
2296-
return;
2259+
case SocketSlashCommand slashCommand:
2260+
await TimedInvokeAsync(_slashCommandExecuted, nameof(SlashCommandExecuted), slashCommand).ConfigureAwait(false);
2261+
break;
2262+
case SocketMessageComponent messageComponent:
2263+
if (messageComponent.Data.Type == ComponentType.SelectMenu)
2264+
await TimedInvokeAsync(_selectMenuExecuted, nameof(SelectMenuExecuted), messageComponent).ConfigureAwait(false);
2265+
if (messageComponent.Data.Type == ComponentType.Button)
2266+
await TimedInvokeAsync(_buttonExecuted, nameof(ButtonExecuted), messageComponent).ConfigureAwait(false);
2267+
break;
2268+
case SocketUserCommand userCommand:
2269+
await TimedInvokeAsync(_userCommandExecuted, nameof(UserCommandExecuted), userCommand).ConfigureAwait(false);
2270+
break;
2271+
case SocketMessageCommand messageCommand:
2272+
await TimedInvokeAsync(_messageCommandExecuted, nameof(MessageCommandExecuted), messageCommand).ConfigureAwait(false);
2273+
break;
2274+
case SocketAutocompleteInteraction autocomplete:
2275+
await TimedInvokeAsync(_autocompleteExecuted, nameof(AutocompleteExecuted), autocomplete).ConfigureAwait(false);
2276+
break;
22972277
}
22982278
}
22992279
break;

src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public abstract class SocketInteraction : SocketEntity<ulong>, IDiscordInteracti
1717
/// <summary>
1818
/// The <see cref="ISocketMessageChannel"/> this interaction was used in.
1919
/// </summary>
20+
/// <remarks>
21+
/// If the channel isn't cached or the bot doesn't have access to it then
22+
/// this property will be <see langword="null"/>.
23+
/// </remarks>
2024
public ISocketMessageChannel Channel { get; private set; }
2125

2226
/// <summary>

0 commit comments

Comments
 (0)