fix: Issues related to absence of bot scope (#2352)

This commit is contained in:
d4n
2022-08-03 04:17:43 -05:00
committed by GitHub
parent c49d4830af
commit 1eb42c6128
16 changed files with 49 additions and 45 deletions

View File

@@ -2318,7 +2318,7 @@ namespace Discord.WebSocket
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;
@@ -2326,7 +2326,6 @@ namespace Discord.WebSocket
if (guild != null && !guild.IsSynced)
{
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
return;
}
SocketUser user = data.User.IsSpecified
@@ -2346,15 +2345,8 @@ namespace Discord.WebSocket
{
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)

View File

@@ -39,7 +39,7 @@ namespace Discord.WebSocket
}
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;
}

View File

@@ -34,7 +34,7 @@ namespace Discord.WebSocket
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;
}

View File

@@ -23,7 +23,7 @@ namespace Discord.WebSocket
}
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;
}

View File

@@ -49,7 +49,7 @@ namespace Discord.WebSocket
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;
}

View File

@@ -61,12 +61,12 @@ namespace Discord.WebSocket
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;
}

View File

@@ -50,7 +50,7 @@ namespace Discord.WebSocket
}
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;
}
@@ -58,8 +58,8 @@ namespace Discord.WebSocket
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);
}

View File

@@ -20,9 +20,7 @@ namespace Discord.WebSocket
? (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);
}

View File

@@ -20,9 +20,7 @@ namespace Discord.WebSocket
? (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);
}

View File

@@ -61,7 +61,9 @@ namespace Discord.WebSocket
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);
}

View File

@@ -20,9 +20,7 @@ namespace Discord.WebSocket
? (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);
}

View File

@@ -19,7 +19,7 @@ namespace Discord.WebSocket
/// 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; }

View File

@@ -43,9 +43,7 @@ namespace Discord.WebSocket
? (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);
}

View File

@@ -1,3 +1,4 @@
using Discord.Net;
using System.Collections.Generic;
namespace Discord.WebSocket
@@ -45,13 +46,24 @@ namespace Discord.WebSocket
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();
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);
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);
@@ -73,7 +85,10 @@ namespace Discord.WebSocket
{
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);
}
}
@@ -93,16 +108,19 @@ namespace Discord.WebSocket
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);
}

View File

@@ -127,7 +127,7 @@ namespace Discord.WebSocket
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);
}

View File

@@ -63,7 +63,7 @@ namespace Discord.WebSocket
=> 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;
}