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.2 kB

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