You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

RpcUserMessage.cs 6.5 KiB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. using Model = Discord.API.Rpc.Message;
  8. namespace Discord.Rpc
  9. {
  10. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  11. public class RpcUserMessage : RpcMessage, IUserMessage
  12. {
  13. private bool _isMentioningEveryone, _isTTS, _isPinned, _isBlocked;
  14. private long? _editedTimestampTicks;
  15. private ulong? _webhookId;
  16. private ImmutableArray<Attachment> _attachments;
  17. private ImmutableArray<Embed> _embeds;
  18. private ImmutableArray<ITag> _tags;
  19. public override bool IsTTS => _isTTS;
  20. public override bool IsPinned => _isPinned;
  21. public override bool IsBlocked => _isBlocked;
  22. public override ulong? WebhookId => _webhookId;
  23. public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
  24. public override IReadOnlyCollection<Attachment> Attachments => _attachments;
  25. public override IReadOnlyCollection<Embed> Embeds => _embeds;
  26. public override IReadOnlyCollection<ulong> MentionedChannelIds => MessageHelper.FilterTagsByKey(TagType.ChannelMention, _tags);
  27. public override IReadOnlyCollection<ulong> MentionedRoleIds => MessageHelper.FilterTagsByKey(TagType.RoleMention, _tags);
  28. public override IReadOnlyCollection<ulong> MentionedUserIds => MessageHelper.FilterTagsByKey(TagType.UserMention, _tags);
  29. public override IReadOnlyCollection<ITag> Tags => _tags;
  30. public IReadOnlyDictionary<IEmote, ReactionMetadata> Reactions => ImmutableDictionary.Create<IEmote, ReactionMetadata>();
  31. internal RpcUserMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author, MessageSource source)
  32. : base(discord, id, channel, author, source)
  33. {
  34. }
  35. internal new static RpcUserMessage Create(DiscordRpcClient discord, ulong channelId, Model model)
  36. {
  37. var entity = new RpcUserMessage(discord, model.Id,
  38. RestVirtualMessageChannel.Create(discord, channelId),
  39. RpcUser.Create(discord, model.Author.Value, model.WebhookId.ToNullable()),
  40. MessageHelper.GetSource(model));
  41. entity.Update(model);
  42. return entity;
  43. }
  44. internal override void Update(Model model)
  45. {
  46. base.Update(model);
  47. if (model.IsTextToSpeech.IsSpecified)
  48. _isTTS = model.IsTextToSpeech.Value;
  49. if (model.Pinned.IsSpecified)
  50. _isPinned = model.Pinned.Value;
  51. if (model.EditedTimestamp.IsSpecified)
  52. _editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
  53. if (model.MentionEveryone.IsSpecified)
  54. _isMentioningEveryone = model.MentionEveryone.Value;
  55. if (model.WebhookId.IsSpecified)
  56. _webhookId = model.WebhookId.Value;
  57. if (model.IsBlocked.IsSpecified)
  58. _isBlocked = model.IsBlocked.Value;
  59. if (model.Attachments.IsSpecified)
  60. {
  61. var value = model.Attachments.Value;
  62. if (value.Length > 0)
  63. {
  64. var attachments = ImmutableArray.CreateBuilder<Attachment>(value.Length);
  65. for (int i = 0; i < value.Length; i++)
  66. attachments.Add(Attachment.Create(value[i]));
  67. _attachments = attachments.ToImmutable();
  68. }
  69. else
  70. _attachments = ImmutableArray.Create<Attachment>();
  71. }
  72. if (model.Embeds.IsSpecified)
  73. {
  74. var value = model.Embeds.Value;
  75. if (value.Length > 0)
  76. {
  77. var embeds = ImmutableArray.CreateBuilder<Embed>(value.Length);
  78. for (int i = 0; i < value.Length; i++)
  79. embeds.Add(value[i].ToEntity());
  80. _embeds = embeds.ToImmutable();
  81. }
  82. else
  83. _embeds = ImmutableArray.Create<Embed>();
  84. }
  85. if (model.Content.IsSpecified)
  86. {
  87. var text = model.Content.Value;
  88. _tags = MessageHelper.ParseTags(text, null, null, ImmutableArray.Create<IUser>());
  89. model.Content = text;
  90. }
  91. }
  92. public Task ModifyAsync(Action<MessageProperties> func, RequestOptions options)
  93. => MessageHelper.ModifyAsync(this, Discord, func, options);
  94. public Task AddReactionAsync(IEmote emote, RequestOptions options = null)
  95. => MessageHelper.AddReactionAsync(this, emote, Discord, options);
  96. public Task RemoveReactionAsync(IEmote emote, IUser user, RequestOptions options = null)
  97. => MessageHelper.RemoveReactionAsync(this, user, emote, Discord, options);
  98. public Task RemoveAllReactionsAsync(RequestOptions options = null)
  99. => MessageHelper.RemoveAllReactionsAsync(this, Discord, options);
  100. public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit = 100, ulong? afterUserId = null, RequestOptions options = null)
  101. => MessageHelper.GetReactionUsersAsync(this, emote, x => { x.Limit = limit; x.AfterUserId = afterUserId ?? Optional.Create<ulong>(); }, Discord, options);
  102. public Task PinAsync(RequestOptions options)
  103. => MessageHelper.PinAsync(this, Discord, options);
  104. public Task UnpinAsync(RequestOptions options)
  105. => MessageHelper.UnpinAsync(this, Discord, options);
  106. public string Resolve(int startIndex, TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
  107. TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
  108. => MentionUtils.Resolve(this, startIndex, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
  109. public string Resolve(TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
  110. TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
  111. => MentionUtils.Resolve(this, 0, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
  112. private string DebuggerDisplay => $"{Author}: {Content} ({Id}{(Attachments.Count > 0 ? $", {Attachments.Count} Attachments" : "")})";
  113. }
  114. }