Skip to content

Commit 7e1b8c9

Browse files
authored
Fix channel being null in DMs on Interactions (#2098)
1 parent 0352374 commit 7e1b8c9

File tree

12 files changed

+108
-61
lines changed

12 files changed

+108
-61
lines changed

src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public interface IDiscordInteraction : ISnowflakeEntity
6262
/// </remarks>
6363
string GuildLocale { get; }
6464

65+
/// <summary>
66+
/// Gets whether or not this interaction was executed in a dm channel.
67+
/// </summary>
68+
bool IsDMInteraction { get; }
69+
6570
/// <summary>
6671
/// Responds to an Interaction with type <see cref="InteractionResponseType.ChannelMessageWithSource"/>.
6772
/// </summary>

src/Discord.Net.Interactions/Attributes/Preconditions/RequireContextAttribute.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ public override Task<PreconditionResult> CheckRequirementsAsync(IInteractionCont
5757
bool isValid = false;
5858

5959
if ((Contexts & ContextType.Guild) != 0)
60-
isValid = context.Channel is IGuildChannel;
61-
if ((Contexts & ContextType.DM) != 0)
62-
isValid = isValid || context.Channel is IDMChannel;
63-
if ((Contexts & ContextType.Group) != 0)
64-
isValid = isValid || context.Channel is IGroupChannel;
60+
isValid = !context.Interaction.IsDMInteraction;
61+
if ((Contexts & ContextType.DM) != 0 && (Contexts & ContextType.Group) != 0)
62+
isValid = context.Interaction.IsDMInteraction;
6563

6664
if (isValid)
6765
return Task.FromResult(PreconditionResult.FromSuccess());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public bool IsValidToken
6161
/// <inheritdoc/>
6262
public bool HasResponded { get; protected set; }
6363

64+
/// <inheritdoc/>
65+
public bool IsDMInteraction { get; private set; }
66+
6467
internal RestInteraction(BaseDiscordClient discord, ulong id)
6568
: base(discord, id)
6669
{
@@ -108,6 +111,8 @@ internal static async Task<RestInteraction> CreateAsync(DiscordRestClient client
108111

109112
internal virtual async Task UpdateAsync(DiscordRestClient discord, Model model)
110113
{
114+
IsDMInteraction = !model.GuildId.IsSpecified;
115+
111116
Data = model.Data.IsSpecified
112117
? model.Data.Value
113118
: null;

src/Discord.Net.WebSocket/DiscordSocketClient.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,24 +2233,42 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
22332233

22342234
var data = (payload as JToken).ToObject<API.Interaction>(_serializer);
22352235

2236+
var guild = data.GuildId.IsSpecified ? GetGuild(data.GuildId.Value) : null;
2237+
2238+
if (guild != null && !guild.IsSynced)
2239+
{
2240+
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
2241+
return;
2242+
}
2243+
2244+
SocketUser user = data.User.IsSpecified
2245+
? State.GetOrAddUser(data.User.Value.Id, (_) => SocketGlobalUser.Create(this, State, data.User.Value))
2246+
: guild.AddOrUpdateUser(data.Member.Value);
2247+
22362248
SocketChannel channel = null;
22372249
if(data.ChannelId.IsSpecified)
22382250
{
22392251
channel = State.GetChannel(data.ChannelId.Value);
2252+
2253+
if (channel == null)
2254+
{
2255+
if (!data.GuildId.IsSpecified) // assume it is a DM
2256+
{
2257+
channel = CreateDMChannel(data.ChannelId.Value, user, State);
2258+
}
2259+
else
2260+
{
2261+
await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false);
2262+
return;
2263+
}
2264+
}
22402265
}
22412266
else if (data.User.IsSpecified)
22422267
{
22432268
channel = State.GetDMChannel(data.User.Value.Id);
22442269
}
22452270

2246-
var guild = (channel as SocketGuildChannel)?.Guild;
2247-
if (guild != null && !guild.IsSynced)
2248-
{
2249-
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
2250-
return;
2251-
}
2252-
2253-
var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel);
2271+
var interaction = SocketInteraction.Create(this, data, channel as ISocketMessageChannel, user);
22542272

22552273
await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false);
22562274

src/Discord.Net.WebSocket/Entities/Interaction/ContextMenuCommands/MessageCommands/SocketMessageCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class SocketMessageCommand : SocketCommandBase, IMessageCommandInteractio
1313
/// </summary>
1414
public new SocketMessageCommandData Data { get; }
1515

16-
internal SocketMessageCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
17-
: base(client, model, channel)
16+
internal SocketMessageCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
17+
: base(client, model, channel, user)
1818
{
1919
var dataModel = model.Data.IsSpecified
2020
? (DataModel)model.Data.Value
@@ -27,9 +27,9 @@ internal SocketMessageCommand(DiscordSocketClient client, Model model, ISocketMe
2727
Data = SocketMessageCommandData.Create(client, dataModel, model.Id, guildId);
2828
}
2929

30-
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
30+
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
3131
{
32-
var entity = new SocketMessageCommand(client, model, channel);
32+
var entity = new SocketMessageCommand(client, model, channel, user);
3333
entity.Update(model);
3434
return entity;
3535
}

src/Discord.Net.WebSocket/Entities/Interaction/ContextMenuCommands/UserCommands/SocketUserCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class SocketUserCommand : SocketCommandBase, IUserCommandInteraction, IDi
1313
/// </summary>
1414
public new SocketUserCommandData Data { get; }
1515

16-
internal SocketUserCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
17-
: base(client, model, channel)
16+
internal SocketUserCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
17+
: base(client, model, channel, user)
1818
{
1919
var dataModel = model.Data.IsSpecified
2020
? (DataModel)model.Data.Value
@@ -27,9 +27,9 @@ internal SocketUserCommand(DiscordSocketClient client, Model model, ISocketMessa
2727
Data = SocketUserCommandData.Create(client, dataModel, model.Id, guildId);
2828
}
2929

30-
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
30+
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
3131
{
32-
var entity = new SocketUserCommand(client, model, channel);
32+
var entity = new SocketUserCommand(client, model, channel, user);
3333
entity.Update(model);
3434
return entity;
3535
}

src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class SocketMessageComponent : SocketInteraction, IComponentInteraction,
2828
private object _lock = new object();
2929
public override bool HasResponded { get; internal set; } = false;
3030

31-
internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
32-
: base(client, model.Id, channel)
31+
internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
32+
: base(client, model.Id, channel, user)
3333
{
3434
var dataModel = model.Data.IsSpecified
3535
? (DataModel)model.Data.Value
@@ -38,9 +38,9 @@ internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocket
3838
Data = new SocketMessageComponentData(dataModel);
3939
}
4040

41-
internal new static SocketMessageComponent Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
41+
internal new static SocketMessageComponent Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
4242
{
43-
var entity = new SocketMessageComponent(client, model, channel);
43+
var entity = new SocketMessageComponent(client, model, channel, user);
4444
entity.Update(model);
4545
return entity;
4646
}

src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class SocketModal : SocketInteraction, IDiscordInteraction, IModalInterac
2222
/// <value></value>
2323
public new SocketModalData Data { get; set; }
2424

25-
internal SocketModal(DiscordSocketClient client, ModelBase model, ISocketMessageChannel channel)
26-
: base(client, model.Id, channel)
25+
internal SocketModal(DiscordSocketClient client, ModelBase model, ISocketMessageChannel channel, SocketUser user)
26+
: base(client, model.Id, channel, user)
2727
{
2828
var dataModel = model.Data.IsSpecified
2929
? (DataModel)model.Data.Value
@@ -32,9 +32,9 @@ internal SocketModal(DiscordSocketClient client, ModelBase model, ISocketMessage
3232
Data = new SocketModalData(dataModel);
3333
}
3434

35-
internal new static SocketModal Create(DiscordSocketClient client, ModelBase model, ISocketMessageChannel channel)
35+
internal new static SocketModal Create(DiscordSocketClient client, ModelBase model, ISocketMessageChannel channel, SocketUser user)
3636
{
37-
var entity = new SocketModal(client, model, channel);
37+
var entity = new SocketModal(client, model, channel, user);
3838
entity.Update(model);
3939
return entity;
4040
}

src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class SocketAutocompleteInteraction : SocketInteraction, IAutocompleteInt
2121
public override bool HasResponded { get; internal set; }
2222
private object _lock = new object();
2323

24-
internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
25-
: base(client, model.Id, channel)
24+
internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
25+
: base(client, model.Id, channel, user)
2626
{
2727
var dataModel = model.Data.IsSpecified
2828
? (DataModel)model.Data.Value
@@ -32,9 +32,9 @@ internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model,
3232
Data = new SocketAutocompleteInteractionData(dataModel);
3333
}
3434

35-
internal new static SocketAutocompleteInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
35+
internal new static SocketAutocompleteInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
3636
{
37-
var entity = new SocketAutocompleteInteraction(client, model, channel);
37+
var entity = new SocketAutocompleteInteraction(client, model, channel, user);
3838
entity.Update(model);
3939
return entity;
4040
}

src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class SocketSlashCommand : SocketCommandBase, ISlashCommandInteraction, I
1313
/// </summary>
1414
public new SocketSlashCommandData Data { get; }
1515

16-
internal SocketSlashCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
17-
: base(client, model, channel)
16+
internal SocketSlashCommand(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
17+
: base(client, model, channel, user)
1818
{
1919
var dataModel = model.Data.IsSpecified
2020
? (DataModel)model.Data.Value
@@ -27,9 +27,9 @@ internal SocketSlashCommand(DiscordSocketClient client, Model model, ISocketMess
2727
Data = SocketSlashCommandData.Create(client, dataModel, guildId);
2828
}
2929

30-
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
30+
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)
3131
{
32-
var entity = new SocketSlashCommand(client, model, channel);
32+
var entity = new SocketSlashCommand(client, model, channel, user);
3333
entity.Update(model);
3434
return entity;
3535
}

0 commit comments

Comments
 (0)