[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" />
|
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ namespace Discord.WebSocket
|
|||||||
{
|
{
|
||||||
#region SocketMessage
|
#region SocketMessage
|
||||||
private long _timestampTicks;
|
private long _timestampTicks;
|
||||||
private readonly List<SocketReaction> _reactions = new List<SocketReaction>();
|
private readonly List<SocketReaction> _reactions = [];
|
||||||
private ImmutableArray<SocketUser> _userMentions = ImmutableArray.Create<SocketUser>();
|
private ImmutableArray<SocketUser> _userMentions = [];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the author of this message.
|
/// Gets the author of this message.
|
||||||
@@ -113,18 +113,26 @@ namespace Discord.WebSocket
|
|||||||
/// <returns>
|
/// <returns>
|
||||||
/// Collection of WebSocket-based guild channels.
|
/// Collection of WebSocket-based guild channels.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public virtual IReadOnlyCollection<SocketGuildChannel> MentionedChannels => ImmutableArray.Create<SocketGuildChannel>();
|
public virtual IReadOnlyCollection<SocketGuildChannel> MentionedChannels => [];
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the roles mentioned in this message.
|
/// Returns the roles mentioned in this message.
|
||||||
/// </summary>
|
/// </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>
|
/// <returns>
|
||||||
/// Collection of WebSocket-based roles.
|
/// Collection of WebSocket-based roles.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
public virtual IReadOnlyCollection<SocketRole> MentionedRoles => ImmutableArray.Create<SocketRole>();
|
public virtual IReadOnlyCollection<SocketRole> MentionedRoles => [];
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual IReadOnlyCollection<ITag> Tags => ImmutableArray.Create<ITag>();
|
public virtual IReadOnlyCollection<ulong> MentionedRoleIds => [];
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual IReadOnlyCollection<SocketSticker> Stickers => ImmutableArray.Create<SocketSticker>();
|
public virtual IReadOnlyCollection<ITag> Tags => [];
|
||||||
|
/// <inheritdoc />
|
||||||
|
public virtual IReadOnlyCollection<SocketSticker> Stickers => [];
|
||||||
/// <inheritdoc />
|
/// <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) });
|
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>
|
/// <summary>
|
||||||
@@ -271,7 +279,8 @@ namespace Discord.WebSocket
|
|||||||
var val = value[i];
|
var val = value[i];
|
||||||
if (val != null)
|
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)
|
if (user != null)
|
||||||
newMentions.Add(user);
|
newMentions.Add(user);
|
||||||
else
|
else
|
||||||
@@ -346,8 +355,6 @@ namespace Discord.WebSocket
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
IReadOnlyCollection<ulong> IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray();
|
IReadOnlyCollection<ulong> IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray();
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
IReadOnlyCollection<ulong> IMessage.MentionedRoleIds => MentionedRoles.Select(x => x.Id).ToImmutableArray();
|
|
||||||
/// <inheritdoc />
|
|
||||||
IReadOnlyCollection<ulong> IMessage.MentionedUserIds => MentionedUsers.Select(x => x.Id).ToImmutableArray();
|
IReadOnlyCollection<ulong> IMessage.MentionedUserIds => MentionedUsers.Select(x => x.Id).ToImmutableArray();
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|||||||
@@ -18,11 +18,12 @@ namespace Discord.WebSocket
|
|||||||
private bool _isMentioningEveryone, _isTTS, _isPinned;
|
private bool _isMentioningEveryone, _isTTS, _isPinned;
|
||||||
private long? _editedTimestampTicks;
|
private long? _editedTimestampTicks;
|
||||||
private IUserMessage _referencedMessage;
|
private IUserMessage _referencedMessage;
|
||||||
private ImmutableArray<Attachment> _attachments = ImmutableArray.Create<Attachment>();
|
private ImmutableArray<Attachment> _attachments = [];
|
||||||
private ImmutableArray<Embed> _embeds = ImmutableArray.Create<Embed>();
|
private ImmutableArray<Embed> _embeds = [];
|
||||||
private ImmutableArray<ITag> _tags = ImmutableArray.Create<ITag>();
|
private ImmutableArray<ITag> _tags = [];
|
||||||
private ImmutableArray<SocketRole> _roleMentions = ImmutableArray.Create<SocketRole>();
|
private ImmutableArray<SocketRole> _roleMentions;
|
||||||
private ImmutableArray<SocketSticker> _stickers = ImmutableArray.Create<SocketSticker>();
|
private ImmutableArray<ulong> _roleMentionsIds = [];
|
||||||
|
private ImmutableArray<SocketSticker> _stickers = [];
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override bool IsTTS => _isTTS;
|
public override bool IsTTS => _isTTS;
|
||||||
@@ -44,6 +45,9 @@ namespace Discord.WebSocket
|
|||||||
public override IReadOnlyCollection<SocketGuildChannel> MentionedChannels => MessageHelper.FilterTagsByValue<SocketGuildChannel>(TagType.ChannelMention, _tags);
|
public override IReadOnlyCollection<SocketGuildChannel> MentionedChannels => MessageHelper.FilterTagsByValue<SocketGuildChannel>(TagType.ChannelMention, _tags);
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override IReadOnlyCollection<SocketRole> MentionedRoles => _roleMentions;
|
public override IReadOnlyCollection<SocketRole> MentionedRoles => _roleMentions;
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override IReadOnlyCollection<ulong> MentionedRoleIds => _roleMentionsIds;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override IReadOnlyCollection<SocketSticker> Stickers => _stickers;
|
public override IReadOnlyCollection<SocketSticker> Stickers => _stickers;
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -64,6 +68,7 @@ namespace Discord.WebSocket
|
|||||||
internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author, MessageSource source)
|
internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author, MessageSource source)
|
||||||
: base(discord, id, channel, author, 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)
|
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)
|
if (model.MentionEveryone.IsSpecified)
|
||||||
_isMentioningEveryone = model.MentionEveryone.Value;
|
_isMentioningEveryone = model.MentionEveryone.Value;
|
||||||
if (model.RoleMentions.IsSpecified)
|
if (model.RoleMentions.IsSpecified)
|
||||||
_roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
|
{
|
||||||
|
if (guild is not null)
|
||||||
|
_roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
|
||||||
|
_roleMentionsIds = model.RoleMentions.Value.ToImmutableArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (model.Attachments.IsSpecified)
|
if (model.Attachments.IsSpecified)
|
||||||
{
|
{
|
||||||
@@ -100,7 +110,7 @@ namespace Discord.WebSocket
|
|||||||
_attachments = attachments.ToImmutable();
|
_attachments = attachments.ToImmutable();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_attachments = ImmutableArray.Create<Attachment>();
|
_attachments = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model.Embeds.IsSpecified)
|
if (model.Embeds.IsSpecified)
|
||||||
@@ -114,7 +124,7 @@ namespace Discord.WebSocket
|
|||||||
_embeds = embeds.ToImmutable();
|
_embeds = embeds.ToImmutable();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_embeds = ImmutableArray.Create<Embed>();
|
_embeds = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model.Content.IsSpecified)
|
if (model.Content.IsSpecified)
|
||||||
@@ -155,26 +165,24 @@ namespace Discord.WebSocket
|
|||||||
if (value.Length > 0)
|
if (value.Length > 0)
|
||||||
{
|
{
|
||||||
var stickers = ImmutableArray.CreateBuilder<SocketSticker>(value.Length);
|
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;
|
SocketSticker sticker = null;
|
||||||
|
|
||||||
if (guild != null)
|
if (guild != null)
|
||||||
sticker = guild.GetSticker(stickerItem.Id);
|
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 they want to auto resolve
|
||||||
if (Discord.AlwaysResolveStickers)
|
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 its still null, create an unknown
|
||||||
if (sticker == null)
|
sticker ??= SocketUnknownSticker.Create(Discord, stickerItem);
|
||||||
sticker = SocketUnknownSticker.Create(Discord, stickerItem);
|
|
||||||
|
|
||||||
stickers.Add(sticker);
|
stickers.Add(sticker);
|
||||||
}
|
}
|
||||||
@@ -182,7 +190,7 @@ namespace Discord.WebSocket
|
|||||||
_stickers = stickers.ToImmutable();
|
_stickers = stickers.ToImmutable();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_stickers = ImmutableArray.Create<SocketSticker>();
|
_stickers = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model.Resolved.IsSpecified)
|
if (model.Resolved.IsSpecified)
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.12.2" />
|
<PackageReference Include="FluentAssertions" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
<PackageReference Include="xunit" Version="2.9.2" />
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|||||||
@@ -12,8 +12,8 @@
|
|||||||
<ProjectReference Include="../../src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj" />
|
<ProjectReference Include="../../src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="6.12.2" />
|
<PackageReference Include="FluentAssertions" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||||
<PackageReference Include="xunit" Version="2.9.2" />
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||||
|
|||||||
Reference in New Issue
Block a user