Browse Source

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

tags/3.8.0
d4n GitHub 2 years ago
parent
commit
1eb42c6128
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 50 additions and 46 deletions
  1. +3
    -11
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  2. +1
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketCategoryChannel.cs
  3. +1
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs
  4. +1
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs
  5. +1
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketStageChannel.cs
  6. +2
    -2
      src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
  7. +3
    -3
      src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
  8. +1
    -3
      src/Discord.Net.WebSocket/Entities/Interaction/ContextMenuCommands/MessageCommands/SocketMessageCommand.cs
  9. +1
    -3
      src/Discord.Net.WebSocket/Entities/Interaction/ContextMenuCommands/UserCommands/SocketUserCommand.cs
  10. +3
    -1
      src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs
  11. +1
    -3
      src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommand.cs
  12. +1
    -1
      src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs
  13. +1
    -3
      src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs
  14. +28
    -10
      src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs
  15. +1
    -1
      src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs
  16. +1
    -1
      src/Discord.Net.WebSocket/Entities/Roles/SocketRole.cs

+ 3
- 11
src/Discord.Net.WebSocket/DiscordSocketClient.cs 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)


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketCategoryChannel.cs 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;
}


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs 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;
}


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs 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;
}


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketStageChannel.cs 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;
}


+ 2
- 2
src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs 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;
}


+ 3
- 3
src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs 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);
}



+ 1
- 3
src/Discord.Net.WebSocket/Entities/Interaction/ContextMenuCommands/MessageCommands/SocketMessageCommand.cs 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);
}


+ 1
- 3
src/Discord.Net.WebSocket/Entities/Interaction/ContextMenuCommands/UserCommands/SocketUserCommand.cs 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);
}


+ 3
- 1
src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs 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);
}


+ 1
- 3
src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketSlashCommand.cs 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);
}


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs 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; }


+ 1
- 3
src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs 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);
}


+ 28
- 10
src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketResolvableData.cs 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();

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);
@@ -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);
}


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs 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);
}


+ 1
- 1
src/Discord.Net.WebSocket/Entities/Roles/SocketRole.cs 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;
}


Loading…
Cancel
Save