fix: Parse mentions from message payload (#1621)
This commit is contained in:
@@ -39,6 +39,13 @@ namespace Discord
|
||||
/// </returns>
|
||||
bool IsSuppressed { get; }
|
||||
/// <summary>
|
||||
/// Gets the value that indicates whether this message mentioned everyone.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// <c>true</c> if this message mentioned everyone; otherwise <c>false</c>.
|
||||
/// </returns>
|
||||
bool MentionedEveryone { get; }
|
||||
/// <summary>
|
||||
/// Gets the content for this message.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
|
||||
@@ -37,6 +37,9 @@ namespace Discord.Rest
|
||||
public virtual bool IsSuppressed => false;
|
||||
/// <inheritdoc />
|
||||
public virtual DateTimeOffset? EditedTimestamp => null;
|
||||
/// <inheritdoc />
|
||||
public virtual bool MentionedEveryone => false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of the <see cref="Attachment"/>'s on the message.
|
||||
/// </summary>
|
||||
|
||||
@@ -18,6 +18,8 @@ namespace Discord.Rest
|
||||
private ImmutableArray<Attachment> _attachments = ImmutableArray.Create<Attachment>();
|
||||
private ImmutableArray<Embed> _embeds = ImmutableArray.Create<Embed>();
|
||||
private ImmutableArray<ITag> _tags = ImmutableArray.Create<ITag>();
|
||||
private ImmutableArray<ulong> _roleMentionIds = ImmutableArray.Create<ulong>();
|
||||
private ImmutableArray<RestUser> _userMentions = ImmutableArray.Create<RestUser>();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsTTS => _isTTS;
|
||||
@@ -28,15 +30,17 @@ namespace Discord.Rest
|
||||
/// <inheritdoc />
|
||||
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
|
||||
/// <inheritdoc />
|
||||
public override bool MentionedEveryone => _isMentioningEveryone;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<Attachment> Attachments => _attachments;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<Embed> Embeds => _embeds;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<ulong> MentionedChannelIds => MessageHelper.FilterTagsByKey(TagType.ChannelMention, _tags);
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<ulong> MentionedRoleIds => MessageHelper.FilterTagsByKey(TagType.RoleMention, _tags);
|
||||
public override IReadOnlyCollection<ulong> MentionedRoleIds => _roleMentionIds;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<RestUser> MentionedUsers => MessageHelper.FilterTagsByValue<RestUser>(TagType.UserMention, _tags);
|
||||
public override IReadOnlyCollection<RestUser> MentionedUsers => _userMentions;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<ITag> Tags => _tags;
|
||||
|
||||
@@ -67,6 +71,8 @@ namespace Discord.Rest
|
||||
{
|
||||
_isSuppressed = model.Flags.Value.HasFlag(API.MessageFlags.Suppressed);
|
||||
}
|
||||
if (model.RoleMentions.IsSpecified)
|
||||
_roleMentionIds = model.RoleMentions.Value.ToImmutableArray();
|
||||
|
||||
if (model.Attachments.IsSpecified)
|
||||
{
|
||||
@@ -96,20 +102,19 @@ namespace Discord.Rest
|
||||
_embeds = ImmutableArray.Create<Embed>();
|
||||
}
|
||||
|
||||
ImmutableArray<IUser> mentions = ImmutableArray.Create<IUser>();
|
||||
if (model.UserMentions.IsSpecified)
|
||||
{
|
||||
var value = model.UserMentions.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var newMentions = ImmutableArray.CreateBuilder<IUser>(value.Length);
|
||||
var newMentions = ImmutableArray.CreateBuilder<RestUser>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
var val = value[i];
|
||||
if (val.Object != null)
|
||||
newMentions.Add(RestUser.Create(Discord, val.Object));
|
||||
}
|
||||
mentions = newMentions.ToImmutable();
|
||||
_userMentions = newMentions.ToImmutable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +123,7 @@ namespace Discord.Rest
|
||||
var text = model.Content.Value;
|
||||
var guildId = (Channel as IGuildChannel)?.GuildId;
|
||||
var guild = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;
|
||||
_tags = MessageHelper.ParseTags(text, null, guild, mentions);
|
||||
_tags = MessageHelper.ParseTags(text, null, guild, _userMentions);
|
||||
model.Content = text;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ namespace Discord.WebSocket
|
||||
public virtual bool IsSuppressed => false;
|
||||
/// <inheritdoc />
|
||||
public virtual DateTimeOffset? EditedTimestamp => null;
|
||||
/// <inheritdoc />
|
||||
public virtual bool MentionedEveryone => false;
|
||||
|
||||
/// <inheritdoc />
|
||||
public MessageActivity Activity { get; private set; }
|
||||
|
||||
@@ -20,6 +20,8 @@ namespace Discord.WebSocket
|
||||
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<SocketUser> _userMentions = ImmutableArray.Create<SocketUser>();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsTTS => _isTTS;
|
||||
@@ -30,6 +32,8 @@ namespace Discord.WebSocket
|
||||
/// <inheritdoc />
|
||||
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
|
||||
/// <inheritdoc />
|
||||
public override bool MentionedEveryone => _isMentioningEveryone;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<Attachment> Attachments => _attachments;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<Embed> Embeds => _embeds;
|
||||
@@ -38,9 +42,9 @@ namespace Discord.WebSocket
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<SocketGuildChannel> MentionedChannels => MessageHelper.FilterTagsByValue<SocketGuildChannel>(TagType.ChannelMention, _tags);
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<SocketRole> MentionedRoles => MessageHelper.FilterTagsByValue<SocketRole>(TagType.RoleMention, _tags);
|
||||
public override IReadOnlyCollection<SocketRole> MentionedRoles => _roleMentions;
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyCollection<SocketUser> MentionedUsers => MessageHelper.FilterTagsByValue<SocketUser>(TagType.UserMention, _tags);
|
||||
public override IReadOnlyCollection<SocketUser> MentionedUsers => _userMentions;
|
||||
|
||||
internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author, MessageSource source)
|
||||
: base(discord, id, channel, author, source)
|
||||
@@ -57,6 +61,8 @@ namespace Discord.WebSocket
|
||||
{
|
||||
base.Update(state, model);
|
||||
|
||||
SocketGuild guild = (Channel as SocketGuildChannel)?.Guild;
|
||||
|
||||
if (model.IsTextToSpeech.IsSpecified)
|
||||
_isTTS = model.IsTextToSpeech.Value;
|
||||
if (model.Pinned.IsSpecified)
|
||||
@@ -69,6 +75,8 @@ namespace Discord.WebSocket
|
||||
{
|
||||
_isSuppressed = model.Flags.Value.HasFlag(API.MessageFlags.Suppressed);
|
||||
}
|
||||
if (model.RoleMentions.IsSpecified)
|
||||
_roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
|
||||
|
||||
if (model.Attachments.IsSpecified)
|
||||
{
|
||||
@@ -98,28 +106,29 @@ namespace Discord.WebSocket
|
||||
_embeds = ImmutableArray.Create<Embed>();
|
||||
}
|
||||
|
||||
IReadOnlyCollection<IUser> mentions = ImmutableArray.Create<SocketUnknownUser>(); //Is passed to ParseTags to get real mention collection
|
||||
if (model.UserMentions.IsSpecified)
|
||||
{
|
||||
var value = model.UserMentions.Value;
|
||||
if (value.Length > 0)
|
||||
{
|
||||
var newMentions = ImmutableArray.CreateBuilder<SocketUnknownUser>(value.Length);
|
||||
var newMentions = ImmutableArray.CreateBuilder<SocketUser>(value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
var val = value[i];
|
||||
if (val.Object != null)
|
||||
var guildUser = guild.GetUser(val.Id);
|
||||
if (guildUser != null)
|
||||
newMentions.Add(guildUser);
|
||||
else if (val.Object != null)
|
||||
newMentions.Add(SocketUnknownUser.Create(Discord, state, val.Object));
|
||||
}
|
||||
mentions = newMentions.ToImmutable();
|
||||
_userMentions = newMentions.ToImmutable();
|
||||
}
|
||||
}
|
||||
|
||||
if (model.Content.IsSpecified)
|
||||
{
|
||||
var text = model.Content.Value;
|
||||
var guild = (Channel as SocketGuildChannel)?.Guild;
|
||||
_tags = MessageHelper.ParseTags(text, Channel, guild, mentions);
|
||||
_tags = MessageHelper.ParseTags(text, Channel, guild, _userMentions);
|
||||
model.Content = text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user