[Fix] NRE in message commands in user app contexts (#3035)
* bump few more things * expose `MentionedRoleIds` on `SocketMessage` + add some syntax sugar * right, we can't have nice things cuz netfx * omfg that's an I/O operation in ctor bruh
This commit is contained in:
@@ -20,6 +20,6 @@
|
||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -16,8 +16,8 @@ namespace Discord.WebSocket
|
||||
{
|
||||
#region SocketMessage
|
||||
private long _timestampTicks;
|
||||
private readonly List<SocketReaction> _reactions = new List<SocketReaction>();
|
||||
private ImmutableArray<SocketUser> _userMentions = ImmutableArray.Create<SocketUser>();
|
||||
private readonly List<SocketReaction> _reactions = [];
|
||||
private ImmutableArray<SocketUser> _userMentions = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the author of this message.
|
||||
@@ -113,18 +113,26 @@ namespace Discord.WebSocket
|
||||
/// <returns>
|
||||
/// Collection of WebSocket-based guild channels.
|
||||
/// </returns>
|
||||
public virtual IReadOnlyCollection<SocketGuildChannel> MentionedChannels => ImmutableArray.Create<SocketGuildChannel>();
|
||||
public virtual IReadOnlyCollection<SocketGuildChannel> MentionedChannels => [];
|
||||
/// <summary>
|
||||
/// Returns the roles mentioned in this message.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This collection may be missing values due to the guild being missing from cache (i.e. in user app interaction context).
|
||||
/// In that case you can use the <see cref="MentionedRoleIds"/> property.
|
||||
/// </remarks>
|
||||
/// <returns>
|
||||
/// Collection of WebSocket-based roles.
|
||||
/// </returns>
|
||||
public virtual IReadOnlyCollection<SocketRole> MentionedRoles => ImmutableArray.Create<SocketRole>();
|
||||
public virtual IReadOnlyCollection<SocketRole> MentionedRoles => [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual IReadOnlyCollection<ITag> Tags => ImmutableArray.Create<ITag>();
|
||||
public virtual IReadOnlyCollection<ulong> MentionedRoleIds => [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual IReadOnlyCollection<SocketSticker> Stickers => ImmutableArray.Create<SocketSticker>();
|
||||
public virtual IReadOnlyCollection<ITag> Tags => [];
|
||||
/// <inheritdoc />
|
||||
public virtual IReadOnlyCollection<SocketSticker> Stickers => [];
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyDictionary<IEmote, ReactionMetadata> Reactions => _reactions.GroupBy(r => r.Emote).ToDictionary(x => x.Key, x => new ReactionMetadata { ReactionCount = x.Count(), IsMe = x.Any(y => y.UserId == Discord.CurrentUser.Id) });
|
||||
/// <summary>
|
||||
@@ -271,7 +279,8 @@ namespace Discord.WebSocket
|
||||
var val = value[i];
|
||||
if (val != null)
|
||||
{
|
||||
var user = Channel.GetUserAsync(val.Id, CacheMode.CacheOnly).GetAwaiter().GetResult() as SocketUser;
|
||||
// TODO: this is cursed af and should be yeeted
|
||||
var user = Channel?.GetUserAsync(val.Id, CacheMode.CacheOnly).GetAwaiter().GetResult() as SocketUser;
|
||||
if (user != null)
|
||||
newMentions.Add(user);
|
||||
else
|
||||
@@ -346,8 +355,6 @@ namespace Discord.WebSocket
|
||||
/// <inheritdoc />
|
||||
IReadOnlyCollection<ulong> IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray();
|
||||
/// <inheritdoc />
|
||||
IReadOnlyCollection<ulong> IMessage.MentionedRoleIds => MentionedRoles.Select(x => x.Id).ToImmutableArray();
|
||||
/// <inheritdoc />
|
||||
IReadOnlyCollection<ulong> IMessage.MentionedUserIds => MentionedUsers.Select(x => x.Id).ToImmutableArray();
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -18,11 +18,12 @@ namespace Discord.WebSocket
|
||||
private bool _isMentioningEveryone, _isTTS, _isPinned;
|
||||
private long? _editedTimestampTicks;
|
||||
private IUserMessage _referencedMessage;
|
||||
private ImmutableArray<Attachment> _attachments = ImmutableArray.Create<Attachment>();
|
||||
private ImmutableArray<Embed> _embeds = ImmutableArray.Create<Embed>();
|
||||
private ImmutableArray<ITag> _tags = ImmutableArray.Create<ITag>();
|
||||
private ImmutableArray<SocketRole> _roleMentions = ImmutableArray.Create<SocketRole>();
|
||||
private ImmutableArray<SocketSticker> _stickers = ImmutableArray.Create<SocketSticker>();
|
||||
private ImmutableArray<Attachment> _attachments = [];
|
||||
private ImmutableArray<Embed> _embeds = [];
|
||||
private ImmutableArray<ITag> _tags = [];
|
||||
private ImmutableArray<SocketRole> _roleMentions;
|
||||
private ImmutableArray<ulong> _roleMentionsIds = [];
|
||||
private ImmutableArray<SocketSticker> _stickers = [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsTTS => _isTTS;
|
||||
@@ -44,6 +45,9 @@ namespace Discord.WebSocket
|
||||
public override IReadOnlyCollection<SocketGuildChannel> MentionedChannels => MessageHelper.FilterTagsByValue<SocketGuildChannel>(TagType.ChannelMention, _tags);
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<SocketRole> MentionedRoles => _roleMentions;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<ulong> MentionedRoleIds => _roleMentionsIds;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<SocketSticker> Stickers => _stickers;
|
||||
/// <inheritdoc />
|
||||
@@ -64,6 +68,7 @@ namespace Discord.WebSocket
|
||||
internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author, MessageSource source)
|
||||
: base(discord, id, channel, author, source)
|
||||
{
|
||||
_roleMentions = ImmutableArray.Create<SocketRole>();
|
||||
}
|
||||
internal new static SocketUserMessage Create(DiscordSocketClient discord, ClientState state, SocketUser author, ISocketMessageChannel channel, Model model)
|
||||
{
|
||||
@@ -87,7 +92,12 @@ namespace Discord.WebSocket
|
||||
if (model.MentionEveryone.IsSpecified)
|
||||
_isMentioningEveryone = model.MentionEveryone.Value;
|
||||
if (model.RoleMentions.IsSpecified)
|
||||
{
|
||||
if (guild is not null)
|
||||
_roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
|
||||
_roleMentionsIds = model.RoleMentions.Value.ToImmutableArray();
|
||||
}
|
||||
|
||||
|
||||
if (model.Attachments.IsSpecified)
|
||||
{
|
||||
@@ -100,7 +110,7 @@ namespace Discord.WebSocket
|
||||
_attachments = attachments.ToImmutable();
|
||||
}
|
||||
else
|
||||
_attachments = ImmutableArray.Create<Attachment>();
|
||||
_attachments = [];
|
||||
}
|
||||
|
||||
if (model.Embeds.IsSpecified)
|
||||
@@ -114,7 +124,7 @@ namespace Discord.WebSocket
|
||||
_embeds = embeds.ToImmutable();
|
||||
}
|
||||
else
|
||||
_embeds = ImmutableArray.Create<Embed>();
|
||||
_embeds = [];
|
||||
}
|
||||
|
||||
if (model.Content.IsSpecified)
|
||||
@@ -155,26 +165,24 @@ namespace Discord.WebSocket
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var stickers = ImmutableArray.CreateBuilder<SocketSticker>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
foreach (var stickerItem in value)
|
||||
{
|
||||
var stickerItem = value[i];
|
||||
SocketSticker sticker = null;
|
||||
|
||||
if (guild != null)
|
||||
sticker = guild.GetSticker(stickerItem.Id);
|
||||
|
||||
if (sticker == null)
|
||||
sticker = Discord.GetSticker(stickerItem.Id);
|
||||
sticker ??= Discord.GetSticker(stickerItem.Id);
|
||||
|
||||
// if they want to auto resolve
|
||||
if (Discord.AlwaysResolveStickers)
|
||||
{
|
||||
sticker = Task.Run(async () => await Discord.GetStickerAsync(stickerItem.Id).ConfigureAwait(false)).GetAwaiter().GetResult();
|
||||
var item = stickerItem;
|
||||
sticker = Task.Run(async () => await Discord.GetStickerAsync(item.Id).ConfigureAwait(false)).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
// if its still null, create an unknown
|
||||
if (sticker == null)
|
||||
sticker = SocketUnknownSticker.Create(Discord, stickerItem);
|
||||
sticker ??= SocketUnknownSticker.Create(Discord, stickerItem);
|
||||
|
||||
stickers.Add(sticker);
|
||||
}
|
||||
@@ -182,7 +190,7 @@ namespace Discord.WebSocket
|
||||
_stickers = stickers.ToImmutable();
|
||||
}
|
||||
else
|
||||
_stickers = ImmutableArray.Create<SocketSticker>();
|
||||
_stickers = [];
|
||||
}
|
||||
|
||||
if (model.Resolved.IsSpecified)
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="FluentAssertions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
<ProjectReference Include="../../src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="FluentAssertions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
|
||||
Reference in New Issue
Block a user