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.

RpcGroupChannel.cs 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Model = Discord.API.Rpc.Channel;
  9. namespace Discord.Rpc
  10. {
  11. public class RpcGroupChannel : RpcChannel, IRpcMessageChannel, IRpcAudioChannel, IRpcPrivateChannel, IGroupChannel
  12. {
  13. public IReadOnlyCollection<RpcMessage> CachedMessages { get; private set; }
  14. public IReadOnlyCollection<RpcVoiceState> VoiceStates { get; private set; }
  15. internal RpcGroupChannel(DiscordRpcClient discord, ulong id)
  16. : base(discord, id)
  17. {
  18. }
  19. internal new static RpcGroupChannel Create(DiscordRpcClient discord, Model model)
  20. {
  21. var entity = new RpcGroupChannel(discord, model.Id);
  22. entity.Update(model);
  23. return entity;
  24. }
  25. internal override void Update(Model model)
  26. {
  27. base.Update(model);
  28. CachedMessages = model.Messages.Select(x => RpcMessage.Create(Discord, Id, x)).ToImmutableArray();
  29. VoiceStates = model.VoiceStates.Select(x => RpcVoiceState.Create(Discord, x)).ToImmutableArray();
  30. }
  31. public Task LeaveAsync(RequestOptions options = null)
  32. => ChannelHelper.DeleteAsync(this, Discord, options);
  33. //TODO: Use RPC cache
  34. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  35. => ChannelHelper.GetMessageAsync(this, Discord, id, null, options);
  36. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  37. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options);
  38. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  39. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options);
  40. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  41. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options);
  42. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  43. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options);
  44. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS, RequestOptions options = null)
  45. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options);
  46. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options = null)
  47. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options);
  48. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options = null)
  49. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, null, options);
  50. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  51. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
  52. public Task TriggerTypingAsync(RequestOptions options = null)
  53. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  54. public IDisposable EnterTypingState(RequestOptions options = null)
  55. => ChannelHelper.EnterTypingState(this, Discord, options);
  56. public override string ToString() => Id.ToString();
  57. private string DebuggerDisplay => $"({Id}, Group)";
  58. //IPrivateChannel
  59. IReadOnlyCollection<IUser> IPrivateChannel.Recipients { get { throw new NotSupportedException(); } }
  60. //IMessageChannel
  61. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  62. {
  63. if (mode == CacheMode.AllowDownload)
  64. return await GetMessageAsync(id, options).ConfigureAwait(false);
  65. else
  66. return null;
  67. }
  68. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  69. {
  70. if (mode == CacheMode.AllowDownload)
  71. return GetMessagesAsync(limit, options).ConfigureAwait(false);
  72. else
  73. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  74. }
  75. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  76. {
  77. if (mode == CacheMode.AllowDownload)
  78. return GetMessagesAsync(fromMessageId, dir, limit, options);
  79. else
  80. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  81. }
  82. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  83. {
  84. if (mode == CacheMode.AllowDownload)
  85. return GetMessagesAsync(fromMessage, dir, limit, options);
  86. else
  87. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  88. }
  89. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  90. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  91. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  92. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  93. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  94. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  95. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, RequestOptions options)
  96. => await SendMessageAsync(text, isTTS, options).ConfigureAwait(false);
  97. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  98. => EnterTypingState(options);
  99. //IChannel
  100. string IChannel.Name { get { throw new NotSupportedException(); } }
  101. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  102. {
  103. throw new NotSupportedException();
  104. }
  105. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  106. {
  107. throw new NotSupportedException();
  108. }
  109. }
  110. }