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.

RpcMessage.cs 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Threading.Tasks;
  6. using Model = Discord.API.Rpc.Message;
  7. namespace Discord.Rpc
  8. {
  9. public abstract class RpcMessage : RpcEntity<ulong>, IMessage
  10. {
  11. private long _timestampTicks;
  12. public IMessageChannel Channel { get; }
  13. public RpcUser Author { get; }
  14. public MessageSource Source { get; }
  15. public string Content { get; private set; }
  16. public Color AuthorColor { get; private set; }
  17. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  18. public virtual bool IsTTS => false;
  19. public virtual bool IsPinned => false;
  20. public virtual bool IsBlocked => false;
  21. public virtual DateTimeOffset? EditedTimestamp => null;
  22. public virtual IReadOnlyCollection<Attachment> Attachments => ImmutableArray.Create<Attachment>();
  23. public virtual IReadOnlyCollection<Embed> Embeds => ImmutableArray.Create<Embed>();
  24. public virtual IReadOnlyCollection<ulong> MentionedChannelIds => ImmutableArray.Create<ulong>();
  25. public virtual IReadOnlyCollection<ulong> MentionedRoleIds => ImmutableArray.Create<ulong>();
  26. public virtual IReadOnlyCollection<ulong> MentionedUserIds => ImmutableArray.Create<ulong>();
  27. public virtual IReadOnlyCollection<ITag> Tags => ImmutableArray.Create<ITag>();
  28. public virtual ulong? WebhookId => null;
  29. public bool IsWebhook => WebhookId != null;
  30. public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
  31. internal RpcMessage(DiscordRpcClient discord, ulong id, RestVirtualMessageChannel channel, RpcUser author, MessageSource source)
  32. : base(discord, id)
  33. {
  34. Channel = channel;
  35. Author = author;
  36. Source = source;
  37. }
  38. internal static RpcMessage Create(DiscordRpcClient discord, ulong channelId, Model model)
  39. {
  40. //model.ChannelId is always 0, needs to be passed from the event
  41. if (model.Type == MessageType.Default)
  42. return RpcUserMessage.Create(discord, channelId, model);
  43. else
  44. return RpcSystemMessage.Create(discord, channelId, model);
  45. }
  46. internal virtual void Update(Model model)
  47. {
  48. if (model.Timestamp.IsSpecified)
  49. _timestampTicks = model.Timestamp.Value.UtcTicks;
  50. if (model.Content.IsSpecified)
  51. Content = model.Content.Value;
  52. if (model.AuthorColor.IsSpecified)
  53. AuthorColor = new Color(Convert.ToUInt32(model.AuthorColor.Value.Substring(1), 16));
  54. }
  55. public Task DeleteAsync(RequestOptions options = null)
  56. => MessageHelper.DeleteAsync(this, Discord, options);
  57. public override string ToString() => Content;
  58. //IMessage
  59. IMessageChannel IMessage.Channel => Channel;
  60. MessageType IMessage.Type => MessageType.Default;
  61. IUser IMessage.Author => Author;
  62. IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments;
  63. IReadOnlyCollection<IEmbed> IMessage.Embeds => Embeds;
  64. }
  65. }