Skip to content

Commit

Permalink
fix: Issues related to absence of bot scope (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
d4n3436 authored Aug 3, 2022
1 parent c49d483 commit 1eb42c6
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 46 deletions.
14 changes: 3 additions & 11 deletions src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2318,15 +2318,14 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
case "INTERACTION_CREATE":
{
await _gatewayLogger.DebugAsync("Received Dispatch (INTERACTION_CREATE)").ConfigureAwait(false);

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

var guild = data.GuildId.IsSpecified ? GetGuild(data.GuildId.Value) : null;

if (guild != null && !guild.IsSynced)
{
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
return;
}

SocketUser user = data.User.IsSpecified
Expand All @@ -2346,15 +2345,8 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
{
channel = CreateDMChannel(data.ChannelId.Value, user, State);
}
else
{
if (guild != null) // The guild id is set, but the guild cannot be found as the bot scope is not set.
{
await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false);
return;
}
// The channel isnt required when responding to an interaction, so we can leave the channel null.
}

// The channel isnt required when responding to an interaction, so we can leave the channel null.
}
}
else if (data.User.IsSpecified)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal SocketCategoryChannel(DiscordSocketClient discord, ulong id, SocketGuil
}
internal new static SocketCategoryChannel Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketCategoryChannel(guild.Discord, model.Id, guild);
var entity = new SocketCategoryChannel(guild?.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal SocketForumChannel(DiscordSocketClient discord, ulong id, SocketGuild g

internal new static SocketForumChannel Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketForumChannel(guild.Discord, model.Id, guild);
var entity = new SocketForumChannel(guild?.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal SocketNewsChannel(DiscordSocketClient discord, ulong id, SocketGuild gu
}
internal new static SocketNewsChannel Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketNewsChannel(guild.Discord, model.Id, guild);
var entity = new SocketNewsChannel(guild?.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal SocketStageChannel(DiscordSocketClient discord, ulong id, SocketGuild g

internal new static SocketStageChannel Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketStageChannel(guild.Discord, model.Id, guild);
var entity = new SocketStageChannel(guild?.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public IReadOnlyCollection<SocketThreadChannel> Threads
internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild)
{
if (Discord.MessageCacheSize > 0)
if (Discord?.MessageCacheSize > 0)
_messages = new MessageCache(Discord);
}
internal new static SocketTextChannel Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketTextChannel(guild.Discord, model.Id, guild);
var entity = new SocketTextChannel(guild?.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ internal SocketVoiceChannel(DiscordSocketClient discord, ulong id, SocketGuild g
}
internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketVoiceChannel(guild.Discord, model.Id, guild);
var entity = new SocketVoiceChannel(guild?.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}
/// <inheritdoc />
internal override void Update(ClientState state, Model model)
{
base.Update(state, model);
Bitrate = model.Bitrate.Value;
UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null;
Bitrate = model.Bitrate.GetValueOrDefault(64000);
UserLimit = model.UserLimit.GetValueOrDefault() != 0 ? model.UserLimit.Value : (int?)null;
RTCRegion = model.RTCRegion.GetValueOrDefault(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ internal SocketMessageCommand(DiscordSocketClient client, Model model, ISocketMe
? (DataModel)model.Data.Value
: null;

ulong? guildId = null;
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
ulong? guildId = model.GuildId.ToNullable();

Data = SocketMessageCommandData.Create(client, dataModel, model.Id, guildId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ internal SocketUserCommand(DiscordSocketClient client, Model model, ISocketMessa
? (DataModel)model.Data.Value
: null;

ulong? guildId = null;
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
ulong? guildId = model.GuildId.ToNullable();

Data = SocketUserCommandData.Create(client, dataModel, model.Id, guildId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ internal override void Update(Model model)
author = channel.Guild.GetUser(model.Message.Value.Author.Value.Id);
}
else if (model.Message.Value.Author.IsSpecified)
author = (Channel as SocketChannel).GetUser(model.Message.Value.Author.Value.Id);
author = (Channel as SocketChannel)?.GetUser(model.Message.Value.Author.Value.Id);

author ??= Discord.State.GetOrAddUser(model.Message.Value.Author.Value.Id, _ => SocketGlobalUser.Create(Discord, Discord.State, model.Message.Value.Author.Value));

Message = SocketUserMessage.Create(Discord, Discord.State, author, Channel, model.Message.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ internal SocketSlashCommand(DiscordSocketClient client, Model model, ISocketMess
? (DataModel)model.Data.Value
: null;

ulong? guildId = null;
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
ulong? guildId = model.GuildId.ToNullable();

Data = SocketSlashCommandData.Create(client, dataModel, guildId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SocketApplicationCommand : SocketEntity<ulong>, IApplicationCommand
/// Gets whether or not this command is a global application command.
/// </summary>
public bool IsGlobalCommand
=> Guild == null;
=> GuildId is null;

/// <inheritdoc/>
public ulong ApplicationId { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ internal SocketCommandBase(DiscordSocketClient client, Model model, ISocketMessa
? (DataModel)model.Data.Value
: null;

ulong? guildId = null;
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
ulong? guildId = model.GuildId.ToNullable();

Data = SocketCommandBaseData.Create(client, dataModel, model.Id, guildId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Discord.Net;
using System.Collections.Generic;

namespace Discord.WebSocket
Expand Down Expand Up @@ -45,13 +46,24 @@ internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T mod

if (socketChannel == null)
{
var channelModel = guild != null
? discord.Rest.ApiClient.GetChannelAsync(guild.Id, channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult()
: discord.Rest.ApiClient.GetChannelAsync(channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult();

socketChannel = guild != null
? SocketGuildChannel.Create(guild, discord.State, channelModel)
: (SocketChannel)SocketChannel.CreatePrivate(discord, discord.State, channelModel);
try
{
var channelModel = guild != null
? discord.Rest.ApiClient.GetChannelAsync(guild.Id, channel.Value.Id)
.ConfigureAwait(false).GetAwaiter().GetResult()
: discord.Rest.ApiClient.GetChannelAsync(channel.Value.Id).ConfigureAwait(false)
.GetAwaiter().GetResult();

socketChannel = guild != null
? SocketGuildChannel.Create(guild, discord.State, channelModel)
: (SocketChannel)SocketChannel.CreatePrivate(discord, discord.State, channelModel);
}
catch (HttpException ex) when (ex.DiscordCode == DiscordErrorCode.MissingPermissions)
{
socketChannel = guildId != null
? SocketGuildChannel.Create(guild, discord.State, channel.Value)
: (SocketChannel)SocketChannel.CreatePrivate(discord, discord.State, channel.Value);
}
}

discord.State.AddChannel(socketChannel);
Expand All @@ -73,7 +85,10 @@ internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T mod
{
foreach (var role in resolved.Roles.Value)
{
var socketRole = guild.AddOrUpdateRole(role.Value);
var socketRole = guild is null
? SocketRole.Create(null, discord.State, role.Value)
: guild.AddOrUpdateRole(role.Value);

Roles.Add(ulong.Parse(role.Key), socketRole);
}
}
Expand All @@ -93,16 +108,19 @@ internal SocketResolvableData(DiscordSocketClient discord, ulong? guildId, T mod
author = guild.GetUser(msg.Value.Author.Value.Id);
}
else
author = (channel as SocketChannel).GetUser(msg.Value.Author.Value.Id);
author = (channel as SocketChannel)?.GetUser(msg.Value.Author.Value.Id);

if (channel == null)
{
if (!msg.Value.GuildId.IsSpecified) // assume it is a DM
if (guildId is null) // assume it is a DM
{
channel = discord.CreateDMChannel(msg.Value.ChannelId, msg.Value.Author.Value, discord.State);
author = ((SocketDMChannel)channel).Recipient;
}
}

author ??= discord.State.GetOrAddUser(msg.Value.Author.Value.Id, _ => SocketGlobalUser.Create(discord, discord.State, msg.Value.Author.Value));

var message = SocketMessage.Create(discord, discord.State, author, channel, msg.Value);
Messages.Add(message.Id, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal override void Update(ClientState state, Model model)
refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id);
}
else
refMsgAuthor = (Channel as SocketChannel).GetUser(refMsg.Author.Value.Id);
refMsgAuthor = (Channel as SocketChannel)?.GetUser(refMsg.Author.Value.Id);
if (refMsgAuthor == null)
refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.WebSocket/Entities/Roles/SocketRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public IEnumerable<SocketGuildUser> Members
=> Guild.Users.Where(x => x.Roles.Any(r => r.Id == Id));

internal SocketRole(SocketGuild guild, ulong id)
: base(guild.Discord, id)
: base(guild?.Discord, id)
{
Guild = guild;
}
Expand Down

0 comments on commit 1eb42c6

Please sign in to comment.