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

@@ -2326,7 +2326,6 @@ namespace Discord.WebSocket
if (guild != null && !guild.IsSynced) if (guild != null && !guild.IsSynced)
{ {
await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
return;
} }
SocketUser user = data.User.IsSpecified SocketUser user = data.User.IsSpecified
@@ -2346,15 +2345,8 @@ namespace Discord.WebSocket
{ {
channel = CreateDMChannel(data.ChannelId.Value, user, State); channel = CreateDMChannel(data.ChannelId.Value, user, State);
} }
else
{ // The channel isnt required when responding to an interaction, so we can leave the channel null.
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.
}
} }
} }
else if (data.User.IsSpecified) 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) 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); entity.Update(state, model);
return entity; return entity;
} }

View File

@@ -34,7 +34,7 @@ namespace Discord.WebSocket
internal new static SocketForumChannel Create(SocketGuild guild, ClientState state, Model model) 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); entity.Update(state, model);
return entity; return entity;
} }

View File

@@ -23,7 +23,7 @@ namespace Discord.WebSocket
} }
internal new static SocketNewsChannel Create(SocketGuild guild, ClientState state, Model model) 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); entity.Update(state, model);
return entity; return entity;
} }

View File

@@ -49,7 +49,7 @@ namespace Discord.WebSocket
internal new static SocketStageChannel Create(SocketGuild guild, ClientState state, Model model) 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); entity.Update(state, model);
return entity; return entity;
} }

View File

@@ -61,12 +61,12 @@ namespace Discord.WebSocket
internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild) internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild) : base(discord, id, guild)
{ {
if (Discord.MessageCacheSize > 0) if (Discord?.MessageCacheSize > 0)
_messages = new MessageCache(Discord); _messages = new MessageCache(Discord);
} }
internal new static SocketTextChannel Create(SocketGuild guild, ClientState state, Model model) 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); entity.Update(state, model);
return entity; return entity;
} }

View File

@@ -50,7 +50,7 @@ namespace Discord.WebSocket
} }
internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model) 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); entity.Update(state, model);
return entity; return entity;
} }
@@ -58,8 +58,8 @@ namespace Discord.WebSocket
internal override void Update(ClientState state, Model model) internal override void Update(ClientState state, Model model)
{ {
base.Update(state, model); base.Update(state, model);
Bitrate = model.Bitrate.Value; Bitrate = model.Bitrate.GetValueOrDefault(64000);
UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null; UserLimit = model.UserLimit.GetValueOrDefault() != 0 ? model.UserLimit.Value : (int?)null;
RTCRegion = model.RTCRegion.GetValueOrDefault(null); RTCRegion = model.RTCRegion.GetValueOrDefault(null);
} }

View File

@@ -20,9 +20,7 @@ namespace Discord.WebSocket
? (DataModel)model.Data.Value ? (DataModel)model.Data.Value
: null; : null;
ulong? guildId = null; ulong? guildId = model.GuildId.ToNullable();
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
Data = SocketMessageCommandData.Create(client, dataModel, model.Id, guildId); Data = SocketMessageCommandData.Create(client, dataModel, model.Id, guildId);
} }

View File

@@ -20,9 +20,7 @@ namespace Discord.WebSocket
? (DataModel)model.Data.Value ? (DataModel)model.Data.Value
: null; : null;
ulong? guildId = null; ulong? guildId = model.GuildId.ToNullable();
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
Data = SocketUserCommandData.Create(client, dataModel, model.Id, guildId); 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); author = channel.Guild.GetUser(model.Message.Value.Author.Value.Id);
} }
else if (model.Message.Value.Author.IsSpecified) 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); Message = SocketUserMessage.Create(Discord, Discord.State, author, Channel, model.Message.Value);
} }

View File

@@ -20,9 +20,7 @@ namespace Discord.WebSocket
? (DataModel)model.Data.Value ? (DataModel)model.Data.Value
: null; : null;
ulong? guildId = null; ulong? guildId = model.GuildId.ToNullable();
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
Data = SocketSlashCommandData.Create(client, dataModel, guildId); 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. /// Gets whether or not this command is a global application command.
/// </summary> /// </summary>
public bool IsGlobalCommand public bool IsGlobalCommand
=> Guild == null; => GuildId is null;
/// <inheritdoc/> /// <inheritdoc/>
public ulong ApplicationId { get; private set; } public ulong ApplicationId { get; private set; }

View File

@@ -43,9 +43,7 @@ namespace Discord.WebSocket
? (DataModel)model.Data.Value ? (DataModel)model.Data.Value
: null; : null;
ulong? guildId = null; ulong? guildId = model.GuildId.ToNullable();
if (Channel is SocketGuildChannel guildChannel)
guildId = guildChannel.Guild.Id;
Data = SocketCommandBaseData.Create(client, dataModel, model.Id, guildId); Data = SocketCommandBaseData.Create(client, dataModel, model.Id, guildId);
} }

View File

@@ -1,3 +1,4 @@
using Discord.Net;
using System.Collections.Generic; using System.Collections.Generic;
namespace Discord.WebSocket namespace Discord.WebSocket
@@ -45,13 +46,24 @@ namespace Discord.WebSocket
if (socketChannel == null) if (socketChannel == null)
{ {
var channelModel = guild != null try
? discord.Rest.ApiClient.GetChannelAsync(guild.Id, channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult() {
: discord.Rest.ApiClient.GetChannelAsync(channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult(); 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 socketChannel = guild != null
? SocketGuildChannel.Create(guild, discord.State, channelModel) ? SocketGuildChannel.Create(guild, discord.State, channelModel)
: (SocketChannel)SocketChannel.CreatePrivate(discord, 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); discord.State.AddChannel(socketChannel);
@@ -73,7 +85,10 @@ namespace Discord.WebSocket
{ {
foreach (var role in resolved.Roles.Value) 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); Roles.Add(ulong.Parse(role.Key), socketRole);
} }
} }
@@ -93,16 +108,19 @@ namespace Discord.WebSocket
author = guild.GetUser(msg.Value.Author.Value.Id); author = guild.GetUser(msg.Value.Author.Value.Id);
} }
else 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 (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); 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); var message = SocketMessage.Create(discord, discord.State, author, channel, msg.Value);
Messages.Add(message.Id, message); Messages.Add(message.Id, message);
} }

View File

@@ -127,7 +127,7 @@ namespace Discord.WebSocket
refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id); refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id);
} }
else else
refMsgAuthor = (Channel as SocketChannel).GetUser(refMsg.Author.Value.Id); refMsgAuthor = (Channel as SocketChannel)?.GetUser(refMsg.Author.Value.Id);
if (refMsgAuthor == null) if (refMsgAuthor == null)
refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value); 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)); => Guild.Users.Where(x => x.Roles.Any(r => r.Id == Id));
internal SocketRole(SocketGuild guild, ulong id) internal SocketRole(SocketGuild guild, ulong id)
: base(guild.Discord, id) : base(guild?.Discord, id)
{ {
Guild = guild; Guild = guild;
} }