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 10 kB

8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, RequestOptions options = null)
  73. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  74. #endif
  75. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
  76. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, 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 TriggerTypingAsync(RequestOptions options = null)
  82. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  83. public IDisposable EnterTypingState(RequestOptions options = null)
  84. => ChannelHelper.EnterTypingState(this, Discord, options);
  85. internal void AddMessage(SocketMessage msg)
  86. => _messages?.Add(msg);
  87. internal SocketMessage RemoveMessage(ulong id)
  88. => _messages?.Remove(id);
  89. //Users
  90. public override SocketGuildUser GetUser(ulong id)
  91. {
  92. var user = Guild.GetUser(id);
  93. if (user != null)
  94. {
  95. var guildPerms = Permissions.ResolveGuild(Guild, user);
  96. var channelPerms = Permissions.ResolveChannel(Guild, user, this, guildPerms);
  97. if (Permissions.GetValue(channelPerms, ChannelPermission.ViewChannel))
  98. return user;
  99. }
  100. return null;
  101. }
  102. //Webhooks
  103. public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
  104. => ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
  105. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  106. => ChannelHelper.GetWebhookAsync(this, Discord, id, options);
  107. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  108. => ChannelHelper.GetWebhooksAsync(this, Discord, options);
  109. private string DebuggerDisplay => $"{Name} ({Id}, Text)";
  110. internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;
  111. //ITextChannel
  112. async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
  113. => await CreateWebhookAsync(name, avatar, options);
  114. async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
  115. => await GetWebhookAsync(id, options);
  116. async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
  117. => await GetWebhooksAsync(options);
  118. //IGuildChannel
  119. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  120. => Task.FromResult<IGuildUser>(GetUser(id));
  121. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  122. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  123. //IMessageChannel
  124. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  125. {
  126. if (mode == CacheMode.AllowDownload)
  127. return await GetMessageAsync(id, options).ConfigureAwait(false);
  128. else
  129. return GetCachedMessage(id);
  130. }
  131. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  132. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options);
  133. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  134. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options);
  135. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  136. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
  137. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  138. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  139. #if FILESYSTEM
  140. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  141. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  142. #endif
  143. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  144. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  145. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  146. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  147. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  148. => EnterTypingState(options);
  149. }
  150. }