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.

RpcVirtualMessageChannel.cs 5.9 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace Discord.Rest
  8. {
  9. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  10. internal class RestVirtualMessageChannel : RestEntity<ulong>, IMessageChannel
  11. {
  12. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  13. public string Mention => MentionUtils.MentionChannel(Id);
  14. internal RestVirtualMessageChannel(BaseDiscordClient discord, ulong id)
  15. : base(discord, id)
  16. {
  17. }
  18. internal static RestVirtualMessageChannel Create(BaseDiscordClient discord, ulong id)
  19. {
  20. return new RestVirtualMessageChannel(discord, id);
  21. }
  22. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  23. => ChannelHelper.GetMessageAsync(this, Discord, id, options);
  24. public IAsyncEnumerable<RestMessage> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  25. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  26. public IAsyncEnumerable<RestMessage> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  27. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
  28. public IAsyncEnumerable<RestMessage> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  29. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
  30. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  31. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  32. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS, Embed embed = null, RequestOptions options = null)
  33. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  34. #if FILESYSTEM
  35. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options = null)
  36. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  37. #endif
  38. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options = null)
  39. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
  40. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  41. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
  42. public Task TriggerTypingAsync(RequestOptions options = null)
  43. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  44. public IDisposable EnterTypingState(RequestOptions options = null)
  45. => ChannelHelper.EnterTypingState(this, Discord, options);
  46. private string DebuggerDisplay => $"({Id}, Text)";
  47. //IMessageChannel
  48. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  49. {
  50. if (mode == CacheMode.AllowDownload)
  51. return await GetMessageAsync(id, options);
  52. else
  53. return null;
  54. }
  55. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  56. {
  57. if (mode == CacheMode.AllowDownload)
  58. return GetMessagesAsync(limit, options);
  59. else
  60. return AsyncEnumerable.Empty<IMessage>();
  61. }
  62. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  63. {
  64. if (mode == CacheMode.AllowDownload)
  65. return GetMessagesAsync(fromMessageId, dir, limit, options);
  66. else
  67. return AsyncEnumerable.Empty<IMessage>();
  68. }
  69. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  70. {
  71. if (mode == CacheMode.AllowDownload)
  72. return GetMessagesAsync(fromMessage, dir, limit, options);
  73. else
  74. return AsyncEnumerable.Empty<IMessage>();
  75. }
  76. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  77. => await GetPinnedMessagesAsync(options);
  78. #if FILESYSTEM
  79. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  80. => await SendFileAsync(filePath, text, isTTS, options);
  81. #endif
  82. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  83. => await SendFileAsync(stream, filename, text, isTTS, options);
  84. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  85. => await SendMessageAsync(text, isTTS, embed, options);
  86. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  87. => EnterTypingState(options);
  88. //IChannel
  89. string IChannel.Name { get { throw new NotSupportedException(); } }
  90. bool IChannel.IsNsfw { get { throw new NotSupportedException(); } }
  91. IAsyncEnumerable<IUser> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  92. {
  93. throw new NotSupportedException();
  94. }
  95. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  96. {
  97. throw new NotSupportedException();
  98. }
  99. }
  100. }