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.

RpcTextChannel.cs 7.7 KiB

8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Model = Discord.API.Rpc.Channel;
  10. namespace Discord.Rpc
  11. {
  12. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  13. public class RpcTextChannel : RpcGuildChannel, IRpcMessageChannel, ITextChannel
  14. {
  15. public IReadOnlyCollection<RpcMessage> CachedMessages { get; private set; }
  16. public string Mention => MentionUtils.MentionChannel(Id);
  17. // TODO: Check if RPC includes the 'nsfw' field on Channel models
  18. public bool IsNsfw => ChannelHelper.IsNsfw(this);
  19. internal RpcTextChannel(DiscordRpcClient discord, ulong id, ulong guildId)
  20. : base(discord, id, guildId)
  21. {
  22. }
  23. internal new static RpcVoiceChannel Create(DiscordRpcClient discord, Model model)
  24. {
  25. var entity = new RpcVoiceChannel(discord, model.Id, model.GuildId.Value);
  26. entity.Update(model);
  27. return entity;
  28. }
  29. internal override void Update(Model model)
  30. {
  31. base.Update(model);
  32. CachedMessages = model.Messages.Select(x => RpcMessage.Create(Discord, Id, x)).ToImmutableArray();
  33. }
  34. public Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
  35. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  36. //TODO: Use RPC cache
  37. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  38. => ChannelHelper.GetMessageAsync(this, Discord, id, options);
  39. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  40. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  41. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  42. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
  43. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  44. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
  45. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  46. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  47. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  48. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  49. #if FILESYSTEM
  50. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
  51. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  52. #endif
  53. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
  54. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
  55. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  56. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages.Select(x => x.Id), options);
  57. public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null)
  58. => ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options);
  59. public Task TriggerTypingAsync(RequestOptions options = null)
  60. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  61. public IDisposable EnterTypingState(RequestOptions options = null)
  62. => ChannelHelper.EnterTypingState(this, Discord, options);
  63. //Webhooks
  64. public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
  65. => ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
  66. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  67. => ChannelHelper.GetWebhookAsync(this, Discord, id, options);
  68. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  69. => ChannelHelper.GetWebhooksAsync(this, Discord, options);
  70. private string DebuggerDisplay => $"{Name} ({Id}, Text)";
  71. //ITextChannel
  72. string ITextChannel.Topic { get { throw new NotSupportedException(); } }
  73. async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
  74. => await CreateWebhookAsync(name, avatar, options);
  75. async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
  76. => await GetWebhookAsync(id, options);
  77. async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
  78. => await GetWebhooksAsync(options);
  79. //IMessageChannel
  80. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  81. {
  82. if (mode == CacheMode.AllowDownload)
  83. return await GetMessageAsync(id, options).ConfigureAwait(false);
  84. else
  85. return null;
  86. }
  87. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  88. {
  89. if (mode == CacheMode.AllowDownload)
  90. return GetMessagesAsync(limit, options);
  91. else
  92. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  93. }
  94. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  95. {
  96. if (mode == CacheMode.AllowDownload)
  97. return GetMessagesAsync(fromMessageId, dir, limit, options);
  98. else
  99. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  100. }
  101. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  102. {
  103. if (mode == CacheMode.AllowDownload)
  104. return GetMessagesAsync(fromMessage, dir, limit, options);
  105. else
  106. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  107. }
  108. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  109. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  110. #if FILESYSTEM
  111. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  112. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  113. #endif
  114. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  115. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  116. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  117. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  118. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  119. => EnterTypingState(options);
  120. }
  121. }