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.

RestGuild.cs 18 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using Discord.Audio;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using EmbedModel = Discord.API.GuildEmbed;
  9. using Model = Discord.API.Guild;
  10. namespace Discord.Rest
  11. {
  12. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  13. public class RestGuild : RestEntity<ulong>, IGuild, IUpdateable
  14. {
  15. private ImmutableDictionary<ulong, RestRole> _roles;
  16. private ImmutableArray<GuildEmote> _emotes;
  17. private ImmutableArray<string> _features;
  18. public string Name { get; private set; }
  19. public int AFKTimeout { get; private set; }
  20. public bool IsEmbeddable { get; private set; }
  21. public VerificationLevel VerificationLevel { get; private set; }
  22. public MfaLevel MfaLevel { get; private set; }
  23. public DefaultMessageNotifications DefaultMessageNotifications { get; private set; }
  24. public ulong? AFKChannelId { get; private set; }
  25. public ulong? EmbedChannelId { get; private set; }
  26. public ulong OwnerId { get; private set; }
  27. public string VoiceRegionId { get; private set; }
  28. public string IconId { get; private set; }
  29. public string SplashId { get; private set; }
  30. internal bool Available { get; private set; }
  31. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  32. public ulong DefaultChannelId => Id;
  33. public string IconUrl => CDN.GetGuildIconUrl(Id, IconId);
  34. public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId);
  35. public RestRole EveryoneRole => GetRole(Id);
  36. public IReadOnlyCollection<RestRole> Roles => _roles.ToReadOnlyCollection();
  37. public IReadOnlyCollection<GuildEmote> Emotes => _emotes;
  38. public IReadOnlyCollection<string> Features => _features;
  39. internal RestGuild(BaseDiscordClient client, ulong id)
  40. : base(client, id)
  41. {
  42. }
  43. internal static RestGuild Create(BaseDiscordClient discord, Model model)
  44. {
  45. var entity = new RestGuild(discord, model.Id);
  46. entity.Update(model);
  47. return entity;
  48. }
  49. internal void Update(Model model)
  50. {
  51. AFKChannelId = model.AFKChannelId;
  52. EmbedChannelId = model.EmbedChannelId;
  53. AFKTimeout = model.AFKTimeout;
  54. IsEmbeddable = model.EmbedEnabled;
  55. IconId = model.Icon;
  56. Name = model.Name;
  57. OwnerId = model.OwnerId;
  58. VoiceRegionId = model.Region;
  59. SplashId = model.Splash;
  60. VerificationLevel = model.VerificationLevel;
  61. MfaLevel = model.MfaLevel;
  62. DefaultMessageNotifications = model.DefaultMessageNotifications;
  63. if (model.Emojis != null)
  64. {
  65. var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length);
  66. for (int i = 0; i < model.Emojis.Length; i++)
  67. emotes.Add(model.Emojis[i].ToEntity());
  68. _emotes = emotes.ToImmutableArray();
  69. }
  70. else
  71. _emotes = ImmutableArray.Create<GuildEmote>();
  72. if (model.Features != null)
  73. _features = model.Features.ToImmutableArray();
  74. else
  75. _features = ImmutableArray.Create<string>();
  76. var roles = ImmutableDictionary.CreateBuilder<ulong, RestRole>();
  77. if (model.Roles != null)
  78. {
  79. for (int i = 0; i < model.Roles.Length; i++)
  80. roles[model.Roles[i].Id] = RestRole.Create(Discord, this, model.Roles[i]);
  81. }
  82. _roles = roles.ToImmutable();
  83. Available = true;
  84. }
  85. internal void Update(EmbedModel model)
  86. {
  87. EmbedChannelId = model.ChannelId;
  88. IsEmbeddable = model.Enabled;
  89. }
  90. //General
  91. public async Task UpdateAsync(RequestOptions options = null)
  92. => Update(await Discord.ApiClient.GetGuildAsync(Id, options).ConfigureAwait(false));
  93. public Task DeleteAsync(RequestOptions options = null)
  94. => GuildHelper.DeleteAsync(this, Discord, options);
  95. public async Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
  96. {
  97. var model = await GuildHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
  98. Update(model);
  99. }
  100. public async Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
  101. {
  102. var model = await GuildHelper.ModifyEmbedAsync(this, Discord, func, options).ConfigureAwait(false);
  103. Update(model);
  104. }
  105. public async Task ReorderChannelsAsync(IEnumerable<ReorderChannelProperties> args, RequestOptions options = null)
  106. {
  107. var arr = args.ToArray();
  108. await GuildHelper.ReorderChannelsAsync(this, Discord, arr, options);
  109. }
  110. public async Task ReorderRolesAsync(IEnumerable<ReorderRoleProperties> args, RequestOptions options = null)
  111. {
  112. var models = await GuildHelper.ReorderRolesAsync(this, Discord, args, options).ConfigureAwait(false);
  113. foreach (var model in models)
  114. {
  115. var role = GetRole(model.Id);
  116. if (role != null)
  117. role.Update(model);
  118. }
  119. }
  120. public Task LeaveAsync(RequestOptions options = null)
  121. => GuildHelper.LeaveAsync(this, Discord, options);
  122. //Bans
  123. public Task<IReadOnlyCollection<RestBan>> GetBansAsync(RequestOptions options = null)
  124. => GuildHelper.GetBansAsync(this, Discord, options);
  125. public Task AddBanAsync(IUser user, int pruneDays = 0, RequestOptions options = null)
  126. => GuildHelper.AddBanAsync(this, Discord, user.Id, pruneDays, options);
  127. public Task AddBanAsync(ulong userId, int pruneDays = 0, RequestOptions options = null)
  128. => GuildHelper.AddBanAsync(this, Discord, userId, pruneDays, options);
  129. public Task RemoveBanAsync(IUser user, RequestOptions options = null)
  130. => GuildHelper.RemoveBanAsync(this, Discord, user.Id, options);
  131. public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
  132. => GuildHelper.RemoveBanAsync(this, Discord, userId, options);
  133. //Channels
  134. public Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(RequestOptions options = null)
  135. => GuildHelper.GetChannelsAsync(this, Discord, options);
  136. public Task<RestGuildChannel> GetChannelAsync(ulong id, RequestOptions options = null)
  137. => GuildHelper.GetChannelAsync(this, Discord, id, options);
  138. public async Task<RestTextChannel> GetTextChannelAsync(ulong id, RequestOptions options = null)
  139. {
  140. var channel = await GuildHelper.GetChannelAsync(this, Discord, id, options).ConfigureAwait(false);
  141. return channel as RestTextChannel;
  142. }
  143. public async Task<IReadOnlyCollection<RestTextChannel>> GetTextChannelsAsync(RequestOptions options = null)
  144. {
  145. var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
  146. return channels.Select(x => x as RestTextChannel).Where(x => x != null).ToImmutableArray();
  147. }
  148. public async Task<RestVoiceChannel> GetVoiceChannelAsync(ulong id, RequestOptions options = null)
  149. {
  150. var channel = await GuildHelper.GetChannelAsync(this, Discord, id, options).ConfigureAwait(false);
  151. return channel as RestVoiceChannel;
  152. }
  153. public async Task<IReadOnlyCollection<RestVoiceChannel>> GetVoiceChannelsAsync(RequestOptions options = null)
  154. {
  155. var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
  156. return channels.Select(x => x as RestVoiceChannel).Where(x => x != null).ToImmutableArray();
  157. }
  158. public async Task<RestVoiceChannel> GetAFKChannelAsync(RequestOptions options = null)
  159. {
  160. var afkId = AFKChannelId;
  161. if (afkId.HasValue)
  162. {
  163. var channel = await GuildHelper.GetChannelAsync(this, Discord, afkId.Value, options).ConfigureAwait(false);
  164. return channel as RestVoiceChannel;
  165. }
  166. return null;
  167. }
  168. public async Task<RestTextChannel> GetDefaultChannelAsync(RequestOptions options = null)
  169. {
  170. var channel = await GuildHelper.GetChannelAsync(this, Discord, DefaultChannelId, options).ConfigureAwait(false);
  171. return channel as RestTextChannel;
  172. }
  173. public async Task<RestGuildChannel> GetEmbedChannelAsync(RequestOptions options = null)
  174. {
  175. var embedId = EmbedChannelId;
  176. if (embedId.HasValue)
  177. return await GuildHelper.GetChannelAsync(this, Discord, embedId.Value, options).ConfigureAwait(false);
  178. return null;
  179. }
  180. public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null)
  181. => GuildHelper.CreateTextChannelAsync(this, Discord, name, options);
  182. public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null)
  183. => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options);
  184. //Integrations
  185. public Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(RequestOptions options = null)
  186. => GuildHelper.GetIntegrationsAsync(this, Discord, options);
  187. public Task<RestGuildIntegration> CreateIntegrationAsync(ulong id, string type, RequestOptions options = null)
  188. => GuildHelper.CreateIntegrationAsync(this, Discord, id, type, options);
  189. //Invites
  190. public Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(RequestOptions options = null)
  191. => GuildHelper.GetInvitesAsync(this, Discord, options);
  192. //Roles
  193. public RestRole GetRole(ulong id)
  194. {
  195. if (_roles.TryGetValue(id, out RestRole value))
  196. return value;
  197. return null;
  198. }
  199. public async Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
  200. bool isHoisted = false, RequestOptions options = null)
  201. {
  202. var role = await GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options).ConfigureAwait(false);
  203. _roles = _roles.Add(role.Id, role);
  204. return role;
  205. }
  206. //Users
  207. public IAsyncEnumerable<RestGuildUser> GetUsersAsync(RequestOptions options = null)
  208. => GuildHelper.GetUsersAsync(this, Discord, null, null, options);
  209. public Task<RestGuildUser> GetUserAsync(ulong id, RequestOptions options = null)
  210. => GuildHelper.GetUserAsync(this, Discord, id, options);
  211. public Task<RestGuildUser> GetCurrentUserAsync(RequestOptions options = null)
  212. => GuildHelper.GetUserAsync(this, Discord, Discord.CurrentUser.Id, options);
  213. public Task<RestGuildUser> GetOwnerAsync(RequestOptions options = null)
  214. => GuildHelper.GetUserAsync(this, Discord, OwnerId, options);
  215. public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
  216. => GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
  217. public override string ToString() => Name;
  218. private string DebuggerDisplay => $"{Name} ({Id})";
  219. //IGuild
  220. bool IGuild.Available => Available;
  221. IAudioClient IGuild.AudioClient => null;
  222. IRole IGuild.EveryoneRole => EveryoneRole;
  223. IReadOnlyCollection<IRole> IGuild.Roles => Roles;
  224. async Task<IReadOnlyCollection<IBan>> IGuild.GetBansAsync(RequestOptions options)
  225. => await GetBansAsync(options).ConfigureAwait(false);
  226. async Task<IReadOnlyCollection<IGuildChannel>> IGuild.GetChannelsAsync(CacheMode mode, RequestOptions options)
  227. {
  228. if (mode == CacheMode.AllowDownload)
  229. return await GetChannelsAsync(options).ConfigureAwait(false);
  230. else
  231. return ImmutableArray.Create<IGuildChannel>();
  232. }
  233. async Task<IGuildChannel> IGuild.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  234. {
  235. if (mode == CacheMode.AllowDownload)
  236. return await GetChannelAsync(id, options).ConfigureAwait(false);
  237. else
  238. return null;
  239. }
  240. async Task<IReadOnlyCollection<ITextChannel>> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options)
  241. {
  242. if (mode == CacheMode.AllowDownload)
  243. return await GetTextChannelsAsync(options).ConfigureAwait(false);
  244. else
  245. return ImmutableArray.Create<ITextChannel>();
  246. }
  247. async Task<ITextChannel> IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  248. {
  249. if (mode == CacheMode.AllowDownload)
  250. return await GetTextChannelAsync(id, options).ConfigureAwait(false);
  251. else
  252. return null;
  253. }
  254. async Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options)
  255. {
  256. if (mode == CacheMode.AllowDownload)
  257. return await GetVoiceChannelsAsync(options).ConfigureAwait(false);
  258. else
  259. return ImmutableArray.Create<IVoiceChannel>();
  260. }
  261. async Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  262. {
  263. if (mode == CacheMode.AllowDownload)
  264. return await GetVoiceChannelAsync(id, options).ConfigureAwait(false);
  265. else
  266. return null;
  267. }
  268. async Task<IVoiceChannel> IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options)
  269. {
  270. if (mode == CacheMode.AllowDownload)
  271. return await GetAFKChannelAsync(options).ConfigureAwait(false);
  272. else
  273. return null;
  274. }
  275. async Task<ITextChannel> IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options)
  276. {
  277. if (mode == CacheMode.AllowDownload)
  278. return await GetDefaultChannelAsync(options).ConfigureAwait(false);
  279. else
  280. return null;
  281. }
  282. async Task<IGuildChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options)
  283. {
  284. if (mode == CacheMode.AllowDownload)
  285. return await GetEmbedChannelAsync(options).ConfigureAwait(false);
  286. else
  287. return null;
  288. }
  289. async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options)
  290. => await CreateTextChannelAsync(name, options).ConfigureAwait(false);
  291. async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
  292. => await CreateVoiceChannelAsync(name, options).ConfigureAwait(false);
  293. async Task<IReadOnlyCollection<IGuildIntegration>> IGuild.GetIntegrationsAsync(RequestOptions options)
  294. => await GetIntegrationsAsync(options).ConfigureAwait(false);
  295. async Task<IGuildIntegration> IGuild.CreateIntegrationAsync(ulong id, string type, RequestOptions options)
  296. => await CreateIntegrationAsync(id, type, options).ConfigureAwait(false);
  297. async Task<IReadOnlyCollection<IInviteMetadata>> IGuild.GetInvitesAsync(RequestOptions options)
  298. => await GetInvitesAsync(options).ConfigureAwait(false);
  299. IRole IGuild.GetRole(ulong id)
  300. => GetRole(id);
  301. async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  302. => await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
  303. async Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  304. {
  305. if (mode == CacheMode.AllowDownload)
  306. return await GetUserAsync(id, options).ConfigureAwait(false);
  307. else
  308. return null;
  309. }
  310. async Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options)
  311. {
  312. if (mode == CacheMode.AllowDownload)
  313. return await GetCurrentUserAsync(options).ConfigureAwait(false);
  314. else
  315. return null;
  316. }
  317. async Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
  318. {
  319. if (mode == CacheMode.AllowDownload)
  320. return await GetOwnerAsync(options).ConfigureAwait(false);
  321. else
  322. return null;
  323. }
  324. async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
  325. {
  326. if (mode == CacheMode.AllowDownload)
  327. return (await GetUsersAsync(options).ToArray().ConfigureAwait(false)).ToImmutableArray();
  328. else
  329. return ImmutableArray.Create<IGuildUser>();
  330. }
  331. Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); }
  332. }
  333. }