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

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