fix: Parse mentions from message payload (#1621)

This commit is contained in:
Paulo
2020-10-14 19:02:33 -03:00
committed by GitHub
parent df8a0f7cd6
commit 366ca9a562
5 changed files with 41 additions and 15 deletions

View File

@@ -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>

View File

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