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.

SocketTextChannel.cs 11 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Model = Discord.API.Channel;
  10. namespace Discord.WebSocket
  11. {
  12. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  13. public class SocketTextChannel : SocketGuildChannel, ITextChannel, ISocketMessageChannel
  14. {
  15. private readonly MessageCache _messages;
  16. public string Topic { get; private set; }
  17. private bool _nsfw;
  18. public bool IsNsfw => _nsfw || ChannelHelper.IsNsfw(this);
  19. public string Mention => MentionUtils.MentionChannel(Id);
  20. public IReadOnlyCollection<SocketMessage> CachedMessages => _messages?.Messages ?? ImmutableArray.Create<SocketMessage>();
  21. public override IReadOnlyCollection<SocketGuildUser> Users
  22. => Guild.Users.Where(x => Permissions.GetValue(
  23. Permissions.ResolveChannel(Guild, x, this, Permissions.ResolveGuild(Guild, x)),
  24. ChannelPermission.ViewChannel)).ToImmutableArray();
  25. internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
  26. : base(discord, id, guild)
  27. {
  28. if (Discord.MessageCacheSize > 0)
  29. _messages = new MessageCache(Discord, this);
  30. }
  31. internal new static SocketTextChannel Create(SocketGuild guild, ClientState state, Model model)
  32. {
  33. var entity = new SocketTextChannel(guild.Discord, model.Id, guild);
  34. entity.Update(state, model);
  35. return entity;
  36. }
  37. internal override void Update(ClientState state, Model model)
  38. {
  39. base.Update(state, model);
  40. Topic = model.Topic.Value;
  41. _nsfw = model.Nsfw.GetValueOrDefault();
  42. }
  43. public Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
  44. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  45. //Messages
  46. public SocketMessage GetCachedMessage(ulong id)
  47. => _messages?.Get(id);
  48. public async Task<IMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  49. {
  50. IMessage msg = _messages?.Get(id);
  51. if (msg == null)
  52. msg = await ChannelHelper.GetMessageAsync(this, Discord, id, options).ConfigureAwait(false);
  53. return msg;
  54. }
  55. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  56. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload, options);
  57. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  58. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload, options);
  59. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  60. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload, options);
  61. public IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = DiscordConfig.MaxMessagesPerBatch)
  62. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, null, Direction.Before, limit);
  63. public IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  64. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessageId, dir, limit);
  65. public IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  66. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessage.Id, dir, limit);
  67. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  68. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  69. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  70. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  71. #if FILESYSTEM
  72. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  73. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  74. #endif
  75. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  76. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  77. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  78. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages.Select(x => x.Id), options);
  79. public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null)
  80. => ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options);
  81. public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
  82. => ChannelHelper.DeleteMessageAsync(this, Discord, messageId, options);
  83. public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
  84. => ChannelHelper.DeleteMessageAsync(this, Discord, message.Id, options);
  85. public Task TriggerTypingAsync(RequestOptions options = null)
  86. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  87. public IDisposable EnterTypingState(RequestOptions options = null)
  88. => ChannelHelper.EnterTypingState(this, Discord, options);
  89. internal void AddMessage(SocketMessage msg)
  90. => _messages?.Add(msg);
  91. internal SocketMessage RemoveMessage(ulong id)
  92. => _messages?.Remove(id);
  93. //Users
  94. public override SocketGuildUser GetUser(ulong id)
  95. {
  96. var user = Guild.GetUser(id);
  97. if (user != null)
  98. {
  99. var guildPerms = Permissions.ResolveGuild(Guild, user);
  100. var channelPerms = Permissions.ResolveChannel(Guild, user, this, guildPerms);
  101. if (Permissions.GetValue(channelPerms, ChannelPermission.ViewChannel))
  102. return user;
  103. }
  104. return null;
  105. }
  106. //Webhooks
  107. public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
  108. => ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
  109. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  110. => ChannelHelper.GetWebhookAsync(this, Discord, id, options);
  111. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  112. => ChannelHelper.GetWebhooksAsync(this, Discord, options);
  113. private string DebuggerDisplay => $"{Name} ({Id}, Text)";
  114. internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;
  115. //ITextChannel
  116. async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
  117. => await CreateWebhookAsync(name, avatar, options);
  118. async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
  119. => await GetWebhookAsync(id, options);
  120. async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
  121. => await GetWebhooksAsync(options);
  122. //IGuildChannel
  123. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  124. => Task.FromResult<IGuildUser>(GetUser(id));
  125. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  126. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  127. //IMessageChannel
  128. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  129. {
  130. if (mode == CacheMode.AllowDownload)
  131. return await GetMessageAsync(id, options).ConfigureAwait(false);
  132. else
  133. return GetCachedMessage(id);
  134. }
  135. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  136. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options);
  137. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  138. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options);
  139. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  140. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
  141. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  142. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  143. #if FILESYSTEM
  144. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  145. => await SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  146. #endif
  147. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  148. => await SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
  149. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  150. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  151. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  152. => EnterTypingState(options);
  153. }
  154. }