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 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  25. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  26. public IAsyncEnumerable<IReadOnlyCollection<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<IReadOnlyCollection<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 = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  33. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  34. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  35. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  36. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  37. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  38. public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
  39. => ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options);
  40. public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
  41. => ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, 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<IReadOnlyCollection<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<IReadOnlyCollection<IMessage>>();
  61. }
  62. IAsyncEnumerable<IReadOnlyCollection<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<IReadOnlyCollection<IMessage>>();
  68. }
  69. IAsyncEnumerable<IReadOnlyCollection<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<IReadOnlyCollection<IMessage>>();
  75. }
  76. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  77. => await GetPinnedMessagesAsync(options);
  78. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  79. => await SendFileAsync(filePath, text, isTTS, embed, options);
  80. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  81. => await SendFileAsync(stream, filename, text, isTTS, embed, options);
  82. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  83. => await SendMessageAsync(text, isTTS, embed, options);
  84. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  85. => EnterTypingState(options);
  86. //IChannel
  87. string IChannel.Name { get { throw new NotSupportedException(); } }
  88. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  89. {
  90. throw new NotSupportedException();
  91. }
  92. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  93. {
  94. throw new NotSupportedException();
  95. }
  96. }
  97. }