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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, bool isTTS, 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, 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, Embed embed = null, RequestOptions options = null)
  37. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  38. public Task TriggerTypingAsync(RequestOptions options = null)
  39. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  40. public IDisposable EnterTypingState(RequestOptions options = null)
  41. => ChannelHelper.EnterTypingState(this, Discord, options);
  42. private string DebuggerDisplay => $"({Id}, Text)";
  43. //IMessageChannel
  44. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  45. {
  46. if (mode == CacheMode.AllowDownload)
  47. return await GetMessageAsync(id, options);
  48. else
  49. return null;
  50. }
  51. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  52. {
  53. if (mode == CacheMode.AllowDownload)
  54. return GetMessagesAsync(limit, options);
  55. else
  56. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  57. }
  58. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  59. {
  60. if (mode == CacheMode.AllowDownload)
  61. return GetMessagesAsync(fromMessageId, dir, limit, options);
  62. else
  63. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  64. }
  65. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  66. {
  67. if (mode == CacheMode.AllowDownload)
  68. return GetMessagesAsync(fromMessage, dir, limit, options);
  69. else
  70. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  71. }
  72. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  73. => await GetPinnedMessagesAsync(options);
  74. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  75. => await SendFileAsync(filePath, text, isTTS, embed, options);
  76. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  77. => await SendFileAsync(stream, filename, text, isTTS, embed, options);
  78. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  79. => await SendMessageAsync(text, isTTS, embed, options);
  80. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  81. => EnterTypingState(options);
  82. //IChannel
  83. string IChannel.Name { get { throw new NotSupportedException(); } }
  84. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  85. {
  86. throw new NotSupportedException();
  87. }
  88. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  89. {
  90. throw new NotSupportedException();
  91. }
  92. }
  93. }