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

8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. public string Mention => MentionUtils.MentionChannel(Id);
  18. public IReadOnlyCollection<SocketMessage> CachedMessages => _messages?.Messages ?? ImmutableArray.Create<SocketMessage>();
  19. public override IReadOnlyCollection<SocketGuildUser> Users
  20. => Guild.Users.Where(x => Permissions.GetValue(
  21. Permissions.ResolveChannel(Guild, x, this, Permissions.ResolveGuild(Guild, x)),
  22. ChannelPermission.ReadMessages)).ToImmutableArray();
  23. internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
  24. : base(discord, id, guild)
  25. {
  26. if (Discord.MessageCacheSize > 0)
  27. _messages = new MessageCache(Discord, this);
  28. }
  29. internal new static SocketTextChannel Create(SocketGuild guild, ClientState state, Model model)
  30. {
  31. var entity = new SocketTextChannel(guild.Discord, model.Id, guild);
  32. entity.Update(state, model);
  33. return entity;
  34. }
  35. internal override void Update(ClientState state, Model model)
  36. {
  37. base.Update(state, model);
  38. Topic = model.Topic.Value;
  39. }
  40. public Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
  41. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  42. //Messages
  43. public SocketMessage GetCachedMessage(ulong id)
  44. => _messages?.Get(id);
  45. public async Task<IMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  46. {
  47. IMessage msg = _messages?.Get(id);
  48. if (msg == null)
  49. msg = await ChannelHelper.GetMessageAsync(this, Discord, id, options).ConfigureAwait(false);
  50. return msg;
  51. }
  52. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  53. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload, options);
  54. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  55. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload, options);
  56. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  57. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload, options);
  58. public IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = DiscordConfig.MaxMessagesPerBatch)
  59. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, null, Direction.Before, limit);
  60. public IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  61. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessageId, dir, limit);
  62. public IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  63. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessage.Id, dir, limit);
  64. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  65. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  66. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  67. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  68. #if FILESYSTEM
  69. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
  70. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  71. #endif
  72. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
  73. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
  74. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  75. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
  76. public Task TriggerTypingAsync(RequestOptions options = null)
  77. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  78. public IDisposable EnterTypingState(RequestOptions options = null)
  79. => ChannelHelper.EnterTypingState(this, Discord, options);
  80. internal void AddMessage(SocketMessage msg)
  81. => _messages?.Add(msg);
  82. internal SocketMessage RemoveMessage(ulong id)
  83. => _messages?.Remove(id);
  84. //Users
  85. public override SocketGuildUser GetUser(ulong id)
  86. {
  87. var user = Guild.GetUser(id);
  88. if (user != null)
  89. {
  90. var guildPerms = Permissions.ResolveGuild(Guild, user);
  91. var channelPerms = Permissions.ResolveChannel(Guild, user, this, guildPerms);
  92. if (Permissions.GetValue(channelPerms, ChannelPermission.ReadMessages))
  93. return user;
  94. }
  95. return null;
  96. }
  97. private string DebuggerDisplay => $"{Name} ({Id}, Text)";
  98. internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;
  99. //IGuildChannel
  100. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  101. => Task.FromResult<IGuildUser>(GetUser(id));
  102. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  103. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  104. //IMessageChannel
  105. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  106. {
  107. if (mode == CacheMode.AllowDownload)
  108. return await GetMessageAsync(id, options).ConfigureAwait(false);
  109. else
  110. return GetCachedMessage(id);
  111. }
  112. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  113. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options);
  114. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  115. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options);
  116. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  117. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
  118. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  119. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  120. #if FILESYSTEM
  121. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  122. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  123. #endif
  124. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  125. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  126. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  127. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  128. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  129. => EnterTypingState(options);
  130. }
  131. }