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.

ThreadHelper.cs 3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Discord.API.Rest;
  2. using System;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Model = Discord.API.Channel;
  6. namespace Discord.Rest
  7. {
  8. internal static class ThreadHelper
  9. {
  10. public static async Task<Model> CreateThreadAsync(BaseDiscordClient client, ITextChannel channel, string name, ThreadType type = ThreadType.PublicThread,
  11. ThreadArchiveDuration autoArchiveDuration = ThreadArchiveDuration.OneDay, IMessage message = null, RequestOptions options = null)
  12. {
  13. var features = channel.Guild.Features;
  14. if (autoArchiveDuration == ThreadArchiveDuration.OneWeek && !features.HasFeature(GuildFeature.SevenDayThreadArchive))
  15. throw new ArgumentException($"The guild {channel.Guild.Name} does not have the SEVEN_DAY_THREAD_ARCHIVE feature!", nameof(autoArchiveDuration));
  16. if (autoArchiveDuration == ThreadArchiveDuration.ThreeDays && !features.HasFeature(GuildFeature.ThreeDayThreadArchive))
  17. throw new ArgumentException($"The guild {channel.Guild.Name} does not have the THREE_DAY_THREAD_ARCHIVE feature!", nameof(autoArchiveDuration));
  18. if (type == ThreadType.PrivateThread && !features.HasFeature(GuildFeature.PrivateThreads))
  19. throw new ArgumentException($"The guild {channel.Guild.Name} does not have the PRIVATE_THREADS feature!", nameof(type));
  20. var args = new StartThreadParams
  21. {
  22. Name = name,
  23. Duration = autoArchiveDuration,
  24. Type = type
  25. };
  26. Model model;
  27. if (message != null)
  28. model = await client.ApiClient.StartThreadAsync(channel.Id, message.Id, args, options).ConfigureAwait(false);
  29. else
  30. model = await client.ApiClient.StartThreadAsync(channel.Id, args, options).ConfigureAwait(false);
  31. return model;
  32. }
  33. public static async Task<Model> ModifyAsync(IThreadChannel channel, BaseDiscordClient client,
  34. Action<TextChannelProperties> func,
  35. RequestOptions options)
  36. {
  37. var args = new TextChannelProperties();
  38. func(args);
  39. var apiArgs = new ModifyThreadParams
  40. {
  41. Name = args.Name,
  42. Archived = args.Archived,
  43. AutoArchiveDuration = args.AutoArchiveDuration,
  44. Locked = args.Locked,
  45. Slowmode = args.SlowModeInterval
  46. };
  47. return await client.ApiClient.ModifyThreadAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
  48. }
  49. public static async Task<RestThreadUser[]> GetUsersAsync(IThreadChannel channel, BaseDiscordClient client, RequestOptions options = null)
  50. {
  51. var users = await client.ApiClient.ListThreadMembersAsync(channel.Id, options);
  52. return users.Select(x => RestThreadUser.Create(client, channel.Guild, x, channel)).ToArray();
  53. }
  54. public static async Task<RestThreadUser> GetUserAsync(ulong userId, IThreadChannel channel, BaseDiscordClient client, RequestOptions options = null)
  55. {
  56. var model = await client.ApiClient.GetThreadMemberAsync(channel.Id, userId, options).ConfigureAwait(false);
  57. return RestThreadUser.Create(client, channel.Guild, model, channel);
  58. }
  59. }
  60. }