@velliajs/discord 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (317) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +133 -0
  3. package/package.json +82 -0
  4. package/src/client/BaseClient.js +83 -0
  5. package/src/client/Client.js +591 -0
  6. package/src/client/WebhookClient.js +103 -0
  7. package/src/client/actions/Action.js +119 -0
  8. package/src/client/actions/ActionsManager.js +79 -0
  9. package/src/client/actions/ApplicationCommandPermissionsUpdate.js +34 -0
  10. package/src/client/actions/AutoModerationActionExecution.js +26 -0
  11. package/src/client/actions/AutoModerationRuleCreate.js +27 -0
  12. package/src/client/actions/AutoModerationRuleDelete.js +31 -0
  13. package/src/client/actions/AutoModerationRuleUpdate.js +29 -0
  14. package/src/client/actions/ChannelCreate.js +23 -0
  15. package/src/client/actions/ChannelDelete.js +23 -0
  16. package/src/client/actions/ChannelUpdate.js +33 -0
  17. package/src/client/actions/GuildAuditLogEntryCreate.js +29 -0
  18. package/src/client/actions/GuildBanAdd.js +20 -0
  19. package/src/client/actions/GuildBanRemove.js +25 -0
  20. package/src/client/actions/GuildChannelsPositionUpdate.js +21 -0
  21. package/src/client/actions/GuildDelete.js +44 -0
  22. package/src/client/actions/GuildEmojiCreate.js +20 -0
  23. package/src/client/actions/GuildEmojiDelete.js +19 -0
  24. package/src/client/actions/GuildEmojiUpdate.js +20 -0
  25. package/src/client/actions/GuildEmojisUpdate.js +34 -0
  26. package/src/client/actions/GuildIntegrationsUpdate.js +19 -0
  27. package/src/client/actions/GuildMemberRemove.js +31 -0
  28. package/src/client/actions/GuildMemberUpdate.js +44 -0
  29. package/src/client/actions/GuildRoleCreate.js +25 -0
  30. package/src/client/actions/GuildRoleDelete.js +29 -0
  31. package/src/client/actions/GuildRoleUpdate.js +39 -0
  32. package/src/client/actions/GuildRolesPositionUpdate.js +21 -0
  33. package/src/client/actions/GuildScheduledEventCreate.js +27 -0
  34. package/src/client/actions/GuildScheduledEventDelete.js +31 -0
  35. package/src/client/actions/GuildScheduledEventUpdate.js +30 -0
  36. package/src/client/actions/GuildScheduledEventUserAdd.js +32 -0
  37. package/src/client/actions/GuildScheduledEventUserRemove.js +32 -0
  38. package/src/client/actions/GuildStickerCreate.js +20 -0
  39. package/src/client/actions/GuildStickerDelete.js +19 -0
  40. package/src/client/actions/GuildStickerUpdate.js +20 -0
  41. package/src/client/actions/GuildStickersUpdate.js +34 -0
  42. package/src/client/actions/GuildUpdate.js +33 -0
  43. package/src/client/actions/InteractionCreate.js +101 -0
  44. package/src/client/actions/InviteCreate.js +28 -0
  45. package/src/client/actions/InviteDelete.js +30 -0
  46. package/src/client/actions/MessageCreate.js +37 -0
  47. package/src/client/actions/MessageDelete.js +32 -0
  48. package/src/client/actions/MessageDeleteBulk.js +47 -0
  49. package/src/client/actions/MessageReactionAdd.js +55 -0
  50. package/src/client/actions/MessageReactionRemove.js +45 -0
  51. package/src/client/actions/MessageReactionRemoveAll.js +33 -0
  52. package/src/client/actions/MessageReactionRemoveEmoji.js +28 -0
  53. package/src/client/actions/MessageUpdate.js +26 -0
  54. package/src/client/actions/PresenceUpdate.js +42 -0
  55. package/src/client/actions/StageInstanceCreate.js +28 -0
  56. package/src/client/actions/StageInstanceDelete.js +31 -0
  57. package/src/client/actions/StageInstanceUpdate.js +30 -0
  58. package/src/client/actions/ThreadCreate.js +24 -0
  59. package/src/client/actions/ThreadDelete.js +26 -0
  60. package/src/client/actions/ThreadListSync.js +60 -0
  61. package/src/client/actions/ThreadMemberUpdate.js +30 -0
  62. package/src/client/actions/ThreadMembersUpdate.js +47 -0
  63. package/src/client/actions/TypingStart.js +29 -0
  64. package/src/client/actions/UserUpdate.js +36 -0
  65. package/src/client/actions/VoiceStateUpdate.js +43 -0
  66. package/src/client/actions/WebhooksUpdate.js +20 -0
  67. package/src/client/voice/ClientVoiceManager.js +44 -0
  68. package/src/client/websocket/WebSocketManager.js +393 -0
  69. package/src/client/websocket/WebSocketShard.js +231 -0
  70. package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +5 -0
  71. package/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js +5 -0
  72. package/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js +5 -0
  73. package/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js +5 -0
  74. package/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js +5 -0
  75. package/src/client/websocket/handlers/CHANNEL_CREATE.js +5 -0
  76. package/src/client/websocket/handlers/CHANNEL_DELETE.js +5 -0
  77. package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +22 -0
  78. package/src/client/websocket/handlers/CHANNEL_UPDATE.js +16 -0
  79. package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +5 -0
  80. package/src/client/websocket/handlers/GUILD_BAN_ADD.js +5 -0
  81. package/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +5 -0
  82. package/src/client/websocket/handlers/GUILD_CREATE.js +26 -0
  83. package/src/client/websocket/handlers/GUILD_DELETE.js +5 -0
  84. package/src/client/websocket/handlers/GUILD_EMOJIS_UPDATE.js +5 -0
  85. package/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js +5 -0
  86. package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +39 -0
  87. package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +20 -0
  88. package/src/client/websocket/handlers/GUILD_MEMBER_REMOVE.js +5 -0
  89. package/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js +5 -0
  90. package/src/client/websocket/handlers/GUILD_ROLE_CREATE.js +5 -0
  91. package/src/client/websocket/handlers/GUILD_ROLE_DELETE.js +5 -0
  92. package/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js +5 -0
  93. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js +5 -0
  94. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_DELETE.js +5 -0
  95. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js +5 -0
  96. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_ADD.js +5 -0
  97. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_REMOVE.js +5 -0
  98. package/src/client/websocket/handlers/GUILD_STICKERS_UPDATE.js +5 -0
  99. package/src/client/websocket/handlers/GUILD_UPDATE.js +5 -0
  100. package/src/client/websocket/handlers/INTERACTION_CREATE.js +5 -0
  101. package/src/client/websocket/handlers/INVITE_CREATE.js +5 -0
  102. package/src/client/websocket/handlers/INVITE_DELETE.js +5 -0
  103. package/src/client/websocket/handlers/MESSAGE_CREATE.js +5 -0
  104. package/src/client/websocket/handlers/MESSAGE_DELETE.js +5 -0
  105. package/src/client/websocket/handlers/MESSAGE_DELETE_BULK.js +5 -0
  106. package/src/client/websocket/handlers/MESSAGE_REACTION_ADD.js +5 -0
  107. package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE.js +5 -0
  108. package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_ALL.js +5 -0
  109. package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_EMOJI.js +5 -0
  110. package/src/client/websocket/handlers/MESSAGE_UPDATE.js +16 -0
  111. package/src/client/websocket/handlers/PRESENCE_UPDATE.js +5 -0
  112. package/src/client/websocket/handlers/READY.js +27 -0
  113. package/src/client/websocket/handlers/RESUMED.js +14 -0
  114. package/src/client/websocket/handlers/STAGE_INSTANCE_CREATE.js +5 -0
  115. package/src/client/websocket/handlers/STAGE_INSTANCE_DELETE.js +5 -0
  116. package/src/client/websocket/handlers/STAGE_INSTANCE_UPDATE.js +5 -0
  117. package/src/client/websocket/handlers/THREAD_CREATE.js +5 -0
  118. package/src/client/websocket/handlers/THREAD_DELETE.js +5 -0
  119. package/src/client/websocket/handlers/THREAD_LIST_SYNC.js +5 -0
  120. package/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.js +5 -0
  121. package/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js +5 -0
  122. package/src/client/websocket/handlers/THREAD_UPDATE.js +16 -0
  123. package/src/client/websocket/handlers/TYPING_START.js +5 -0
  124. package/src/client/websocket/handlers/USER_UPDATE.js +5 -0
  125. package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js +6 -0
  126. package/src/client/websocket/handlers/VOICE_STATE_UPDATE.js +5 -0
  127. package/src/client/websocket/handlers/WEBHOOKS_UPDATE.js +5 -0
  128. package/src/client/websocket/handlers/index.js +64 -0
  129. package/src/errors/DJSError.js +48 -0
  130. package/src/errors/ErrorCodes.js +319 -0
  131. package/src/errors/Messages.js +170 -0
  132. package/src/errors/index.js +5 -0
  133. package/src/index.js +213 -0
  134. package/src/managers/ApplicationCommandManager.js +263 -0
  135. package/src/managers/ApplicationCommandPermissionsManager.js +434 -0
  136. package/src/managers/AutoModerationRuleManager.js +288 -0
  137. package/src/managers/BaseGuildEmojiManager.js +80 -0
  138. package/src/managers/BaseManager.js +19 -0
  139. package/src/managers/CachedManager.js +57 -0
  140. package/src/managers/CategoryChannelChildManager.js +77 -0
  141. package/src/managers/ChannelManager.js +128 -0
  142. package/src/managers/DataManager.js +61 -0
  143. package/src/managers/GuildApplicationCommandManager.js +28 -0
  144. package/src/managers/GuildBanManager.js +204 -0
  145. package/src/managers/GuildChannelManager.js +494 -0
  146. package/src/managers/GuildEmojiManager.js +174 -0
  147. package/src/managers/GuildEmojiRoleManager.js +118 -0
  148. package/src/managers/GuildForumThreadManager.js +83 -0
  149. package/src/managers/GuildInviteManager.js +213 -0
  150. package/src/managers/GuildManager.js +283 -0
  151. package/src/managers/GuildMemberManager.js +540 -0
  152. package/src/managers/GuildMemberRoleManager.js +204 -0
  153. package/src/managers/GuildScheduledEventManager.js +297 -0
  154. package/src/managers/GuildStickerManager.js +182 -0
  155. package/src/managers/GuildTextThreadManager.js +91 -0
  156. package/src/managers/MessageManager.js +259 -0
  157. package/src/managers/PermissionOverwriteManager.js +168 -0
  158. package/src/managers/PresenceManager.js +58 -0
  159. package/src/managers/ReactionManager.js +68 -0
  160. package/src/managers/ReactionUserManager.js +77 -0
  161. package/src/managers/RoleManager.js +360 -0
  162. package/src/managers/StageInstanceManager.js +154 -0
  163. package/src/managers/ThreadManager.js +204 -0
  164. package/src/managers/ThreadMemberManager.js +182 -0
  165. package/src/managers/UserManager.js +139 -0
  166. package/src/managers/VoiceStateManager.js +37 -0
  167. package/src/sharding/Shard.js +457 -0
  168. package/src/sharding/ShardClientUtil.js +285 -0
  169. package/src/sharding/ShardingManager.js +326 -0
  170. package/src/structures/ActionRow.js +46 -0
  171. package/src/structures/ActionRowBuilder.js +35 -0
  172. package/src/structures/AnonymousGuild.js +97 -0
  173. package/src/structures/ApplicationCommand.js +606 -0
  174. package/src/structures/ApplicationRoleConnectionMetadata.js +46 -0
  175. package/src/structures/Attachment.js +140 -0
  176. package/src/structures/AttachmentBuilder.js +116 -0
  177. package/src/structures/AutoModerationActionExecution.js +116 -0
  178. package/src/structures/AutoModerationRule.js +284 -0
  179. package/src/structures/AutocompleteInteraction.js +97 -0
  180. package/src/structures/Base.js +43 -0
  181. package/src/structures/BaseChannel.js +160 -0
  182. package/src/structures/BaseGuild.js +119 -0
  183. package/src/structures/BaseGuildEmoji.js +56 -0
  184. package/src/structures/BaseGuildTextChannel.js +186 -0
  185. package/src/structures/BaseGuildVoiceChannel.js +234 -0
  186. package/src/structures/BaseInteraction.js +344 -0
  187. package/src/structures/BaseSelectMenuComponent.js +56 -0
  188. package/src/structures/ButtonBuilder.js +44 -0
  189. package/src/structures/ButtonComponent.js +65 -0
  190. package/src/structures/ButtonInteraction.js +11 -0
  191. package/src/structures/CategoryChannel.js +45 -0
  192. package/src/structures/ChannelSelectMenuBuilder.js +31 -0
  193. package/src/structures/ChannelSelectMenuComponent.js +20 -0
  194. package/src/structures/ChannelSelectMenuInteraction.js +33 -0
  195. package/src/structures/ChatInputCommandInteraction.js +41 -0
  196. package/src/structures/ClientApplication.js +193 -0
  197. package/src/structures/ClientPresence.js +82 -0
  198. package/src/structures/ClientUser.js +185 -0
  199. package/src/structures/CommandInteraction.js +224 -0
  200. package/src/structures/CommandInteractionOptionResolver.js +308 -0
  201. package/src/structures/Component.js +47 -0
  202. package/src/structures/ContextMenuCommandInteraction.js +64 -0
  203. package/src/structures/DMChannel.js +129 -0
  204. package/src/structures/DirectoryChannel.js +36 -0
  205. package/src/structures/Embed.js +220 -0
  206. package/src/structures/EmbedBuilder.js +41 -0
  207. package/src/structures/Emoji.js +108 -0
  208. package/src/structures/ForumChannel.js +264 -0
  209. package/src/structures/Guild.js +1361 -0
  210. package/src/structures/GuildAuditLogs.js +91 -0
  211. package/src/structures/GuildAuditLogsEntry.js +527 -0
  212. package/src/structures/GuildBan.js +59 -0
  213. package/src/structures/GuildChannel.js +457 -0
  214. package/src/structures/GuildEmoji.js +148 -0
  215. package/src/structures/GuildMember.js +518 -0
  216. package/src/structures/GuildPreview.js +193 -0
  217. package/src/structures/GuildPreviewEmoji.js +27 -0
  218. package/src/structures/GuildScheduledEvent.js +439 -0
  219. package/src/structures/GuildTemplate.js +241 -0
  220. package/src/structures/Integration.js +220 -0
  221. package/src/structures/IntegrationApplication.js +85 -0
  222. package/src/structures/InteractionCollector.js +263 -0
  223. package/src/structures/InteractionResponse.js +102 -0
  224. package/src/structures/InteractionWebhook.js +59 -0
  225. package/src/structures/Invite.js +322 -0
  226. package/src/structures/InviteGuild.js +22 -0
  227. package/src/structures/InviteStageInstance.js +87 -0
  228. package/src/structures/MentionableSelectMenuBuilder.js +32 -0
  229. package/src/structures/MentionableSelectMenuComponent.js +11 -0
  230. package/src/structures/MentionableSelectMenuInteraction.js +71 -0
  231. package/src/structures/Message.js +997 -0
  232. package/src/structures/MessageCollector.js +146 -0
  233. package/src/structures/MessageComponentInteraction.js +107 -0
  234. package/src/structures/MessageContextMenuCommandInteraction.js +20 -0
  235. package/src/structures/MessageMentions.js +297 -0
  236. package/src/structures/MessagePayload.js +299 -0
  237. package/src/structures/MessageReaction.js +142 -0
  238. package/src/structures/ModalBuilder.js +34 -0
  239. package/src/structures/ModalSubmitFields.js +55 -0
  240. package/src/structures/ModalSubmitInteraction.js +121 -0
  241. package/src/structures/NewsChannel.js +32 -0
  242. package/src/structures/OAuth2Guild.js +28 -0
  243. package/src/structures/PartialGroupDMChannel.js +60 -0
  244. package/src/structures/PermissionOverwrites.js +196 -0
  245. package/src/structures/Presence.js +372 -0
  246. package/src/structures/ReactionCollector.js +229 -0
  247. package/src/structures/ReactionEmoji.js +31 -0
  248. package/src/structures/Role.js +458 -0
  249. package/src/structures/RoleSelectMenuBuilder.js +31 -0
  250. package/src/structures/RoleSelectMenuComponent.js +11 -0
  251. package/src/structures/RoleSelectMenuInteraction.js +33 -0
  252. package/src/structures/SelectMenuBuilder.js +26 -0
  253. package/src/structures/SelectMenuComponent.js +26 -0
  254. package/src/structures/SelectMenuInteraction.js +26 -0
  255. package/src/structures/SelectMenuOptionBuilder.js +26 -0
  256. package/src/structures/StageChannel.js +112 -0
  257. package/src/structures/StageInstance.js +167 -0
  258. package/src/structures/Sticker.js +272 -0
  259. package/src/structures/StickerPack.js +95 -0
  260. package/src/structures/StringSelectMenuBuilder.js +79 -0
  261. package/src/structures/StringSelectMenuComponent.js +20 -0
  262. package/src/structures/StringSelectMenuInteraction.js +21 -0
  263. package/src/structures/StringSelectMenuOptionBuilder.js +49 -0
  264. package/src/structures/Team.js +117 -0
  265. package/src/structures/TeamMember.js +70 -0
  266. package/src/structures/TextChannel.js +33 -0
  267. package/src/structures/TextInputBuilder.js +31 -0
  268. package/src/structures/TextInputComponent.js +29 -0
  269. package/src/structures/ThreadChannel.js +606 -0
  270. package/src/structures/ThreadMember.js +113 -0
  271. package/src/structures/Typing.js +74 -0
  272. package/src/structures/User.js +331 -0
  273. package/src/structures/UserContextMenuCommandInteraction.js +29 -0
  274. package/src/structures/UserSelectMenuBuilder.js +31 -0
  275. package/src/structures/UserSelectMenuComponent.js +11 -0
  276. package/src/structures/UserSelectMenuInteraction.js +51 -0
  277. package/src/structures/VoiceChannel.js +96 -0
  278. package/src/structures/VoiceRegion.js +46 -0
  279. package/src/structures/VoiceState.js +303 -0
  280. package/src/structures/Webhook.js +481 -0
  281. package/src/structures/WelcomeChannel.js +60 -0
  282. package/src/structures/WelcomeScreen.js +49 -0
  283. package/src/structures/Widget.js +88 -0
  284. package/src/structures/WidgetMember.js +99 -0
  285. package/src/structures/interfaces/Application.js +108 -0
  286. package/src/structures/interfaces/Collector.js +335 -0
  287. package/src/structures/interfaces/InteractionResponses.js +320 -0
  288. package/src/structures/interfaces/TextBasedChannel.js +413 -0
  289. package/src/util/APITypes.js +456 -0
  290. package/src/util/ActivityFlagsBitField.js +26 -0
  291. package/src/util/ApplicationFlagsBitField.js +26 -0
  292. package/src/util/BitField.js +176 -0
  293. package/src/util/ChannelFlagsBitField.js +41 -0
  294. package/src/util/Channels.js +150 -0
  295. package/src/util/Colors.js +73 -0
  296. package/src/util/Components.js +152 -0
  297. package/src/util/Constants.js +230 -0
  298. package/src/util/DataResolver.js +140 -0
  299. package/src/util/Enums.js +13 -0
  300. package/src/util/Events.js +160 -0
  301. package/src/util/Formatters.js +413 -0
  302. package/src/util/GuildMemberFlagsBitField.js +41 -0
  303. package/src/util/IntentsBitField.js +34 -0
  304. package/src/util/LimitedCollection.js +68 -0
  305. package/src/util/MessageFlagsBitField.js +32 -0
  306. package/src/util/Options.js +216 -0
  307. package/src/util/Partials.js +44 -0
  308. package/src/util/PermissionsBitField.js +104 -0
  309. package/src/util/ShardEvents.js +27 -0
  310. package/src/util/Status.js +33 -0
  311. package/src/util/Sweepers.js +467 -0
  312. package/src/util/SystemChannelFlagsBitField.js +43 -0
  313. package/src/util/ThreadMemberFlagsBitField.js +31 -0
  314. package/src/util/Transformers.js +36 -0
  315. package/src/util/UserFlagsBitField.js +32 -0
  316. package/src/util/Util.js +394 -0
  317. package/src/util/WebSocketShardEvents.js +25 -0
@@ -0,0 +1,606 @@
1
+ 'use strict';
2
+
3
+ const { ChannelType, PermissionFlagsBits, Routes, ChannelFlags } = require('discord-api-types/v10');
4
+ const { BaseChannel } = require('./BaseChannel');
5
+ const TextBasedChannel = require('./interfaces/TextBasedChannel');
6
+ const { DiscordjsRangeError, ErrorCodes } = require('../errors');
7
+ const MessageManager = require('../managers/MessageManager');
8
+ const ThreadMemberManager = require('../managers/ThreadMemberManager');
9
+ const ChannelFlagsBitField = require('../util/ChannelFlagsBitField');
10
+
11
+ /**
12
+ * Represents a thread channel on Discord.
13
+ * @extends {BaseChannel}
14
+ * @implements {TextBasedChannel}
15
+ */
16
+ class ThreadChannel extends BaseChannel {
17
+ constructor(guild, data, client) {
18
+ super(guild?.client ?? client, data, false);
19
+
20
+ /**
21
+ * The guild the thread is in
22
+ * @type {Guild}
23
+ */
24
+ this.guild = guild;
25
+
26
+ /**
27
+ * The id of the guild the channel is in
28
+ * @type {Snowflake}
29
+ */
30
+ this.guildId = guild?.id ?? data.guild_id;
31
+
32
+ /**
33
+ * A manager of the messages sent to this thread
34
+ * @type {MessageManager}
35
+ */
36
+ this.messages = new MessageManager(this);
37
+
38
+ /**
39
+ * A manager of the members that are part of this thread
40
+ * @type {ThreadMemberManager}
41
+ */
42
+ this.members = new ThreadMemberManager(this);
43
+ if (data) this._patch(data);
44
+ }
45
+
46
+ _patch(data) {
47
+ super._patch(data);
48
+
49
+ if ('message' in data) this.messages._add(data.message);
50
+
51
+ if ('name' in data) {
52
+ /**
53
+ * The name of the thread
54
+ * @type {string}
55
+ */
56
+ this.name = data.name;
57
+ }
58
+
59
+ if ('guild_id' in data) {
60
+ this.guildId = data.guild_id;
61
+ }
62
+
63
+ if ('parent_id' in data) {
64
+ /**
65
+ * The id of the parent channel of this thread
66
+ * @type {?Snowflake}
67
+ */
68
+ this.parentId = data.parent_id;
69
+ } else {
70
+ this.parentId ??= null;
71
+ }
72
+
73
+ if ('thread_metadata' in data) {
74
+ /**
75
+ * Whether the thread is locked
76
+ * @type {?boolean}
77
+ */
78
+ this.locked = data.thread_metadata.locked ?? false;
79
+
80
+ /**
81
+ * Whether members without the {@link PermissionFlagsBits.ManageThreads} permission
82
+ * can invite other members to this thread.
83
+ * <info>This property is always `null` in public threads.</info>
84
+ * @type {?boolean}
85
+ */
86
+ this.invitable = this.type === ChannelType.PrivateThread ? data.thread_metadata.invitable ?? false : null;
87
+
88
+ /**
89
+ * Whether the thread is archived
90
+ * @type {?boolean}
91
+ */
92
+ this.archived = data.thread_metadata.archived;
93
+
94
+ /**
95
+ * The amount of time (in minutes) after which the thread will automatically archive in case of no recent activity
96
+ * @type {?ThreadAutoArchiveDuration}
97
+ */
98
+ this.autoArchiveDuration = data.thread_metadata.auto_archive_duration;
99
+
100
+ /**
101
+ * The timestamp when the thread's archive status was last changed
102
+ * <info>If the thread was never archived or unarchived, this is the timestamp at which the thread was
103
+ * created</info>
104
+ * @type {?number}
105
+ */
106
+ this.archiveTimestamp = Date.parse(data.thread_metadata.archive_timestamp);
107
+
108
+ if ('create_timestamp' in data.thread_metadata) {
109
+ // Note: this is needed because we can't assign directly to getters
110
+ this._createdTimestamp = Date.parse(data.thread_metadata.create_timestamp);
111
+ }
112
+ } else {
113
+ this.locked ??= null;
114
+ this.archived ??= null;
115
+ this.autoArchiveDuration ??= null;
116
+ this.archiveTimestamp ??= null;
117
+ this.invitable ??= null;
118
+ }
119
+
120
+ this._createdTimestamp ??= this.type === ChannelType.PrivateThread ? super.createdTimestamp : null;
121
+
122
+ if ('owner_id' in data) {
123
+ /**
124
+ * The id of the member who created this thread
125
+ * @type {?Snowflake}
126
+ */
127
+ this.ownerId = data.owner_id;
128
+ } else {
129
+ this.ownerId ??= null;
130
+ }
131
+
132
+ if ('last_message_id' in data) {
133
+ /**
134
+ * The last message id sent in this thread, if one was sent
135
+ * @type {?Snowflake}
136
+ */
137
+ this.lastMessageId = data.last_message_id;
138
+ } else {
139
+ this.lastMessageId ??= null;
140
+ }
141
+
142
+ if ('last_pin_timestamp' in data) {
143
+ /**
144
+ * The timestamp when the last pinned message was pinned, if there was one
145
+ * @type {?number}
146
+ */
147
+ this.lastPinTimestamp = data.last_pin_timestamp ? Date.parse(data.last_pin_timestamp) : null;
148
+ } else {
149
+ this.lastPinTimestamp ??= null;
150
+ }
151
+
152
+ if ('rate_limit_per_user' in data) {
153
+ /**
154
+ * The rate limit per user (slowmode) for this thread in seconds
155
+ * @type {?number}
156
+ */
157
+ this.rateLimitPerUser = data.rate_limit_per_user ?? 0;
158
+ } else {
159
+ this.rateLimitPerUser ??= null;
160
+ }
161
+
162
+ if ('message_count' in data) {
163
+ /**
164
+ * The approximate count of messages in this thread
165
+ * <info>Threads created before July 1, 2022 may have an inaccurate count.
166
+ * If you need an approximate value higher than that, use `ThreadChannel#messages.cache.size`</info>
167
+ * @type {?number}
168
+ */
169
+ this.messageCount = data.message_count;
170
+ } else {
171
+ this.messageCount ??= null;
172
+ }
173
+
174
+ if ('member_count' in data) {
175
+ /**
176
+ * The approximate count of users in this thread
177
+ * <info>This stops counting at 50. If you need an approximate value higher than that, use
178
+ * `ThreadChannel#members.cache.size`</info>
179
+ * @type {?number}
180
+ */
181
+ this.memberCount = data.member_count;
182
+ } else {
183
+ this.memberCount ??= null;
184
+ }
185
+
186
+ if ('total_message_sent' in data) {
187
+ /**
188
+ * The number of messages ever sent in a thread, similar to {@link ThreadChannel#messageCount} except it
189
+ * will not decrement whenever a message is deleted
190
+ * @type {?number}
191
+ */
192
+ this.totalMessageSent = data.total_message_sent;
193
+ } else {
194
+ this.totalMessageSent ??= null;
195
+ }
196
+
197
+ if (data.member && this.client.user) this.members._add({ user_id: this.client.user.id, ...data.member });
198
+ if (data.messages) for (const message of data.messages) this.messages._add(message);
199
+
200
+ if ('applied_tags' in data) {
201
+ /**
202
+ * The tags applied to this thread
203
+ * @type {Snowflake[]}
204
+ */
205
+ this.appliedTags = data.applied_tags;
206
+ } else {
207
+ this.appliedTags ??= [];
208
+ }
209
+ }
210
+
211
+ /**
212
+ * The timestamp when this thread was created. This isn't available for threads
213
+ * created before 2022-01-09
214
+ * @type {?number}
215
+ * @readonly
216
+ */
217
+ get createdTimestamp() {
218
+ return this._createdTimestamp;
219
+ }
220
+
221
+ /**
222
+ * A collection of associated guild member objects of this thread's members
223
+ * @type {Collection<Snowflake, GuildMember>}
224
+ * @readonly
225
+ */
226
+ get guildMembers() {
227
+ return this.members.cache.mapValues(member => member.guildMember);
228
+ }
229
+
230
+ /**
231
+ * The time at which this thread's archive status was last changed
232
+ * <info>If the thread was never archived or unarchived, this is the time at which the thread was created</info>
233
+ * @type {?Date}
234
+ * @readonly
235
+ */
236
+ get archivedAt() {
237
+ return this.archiveTimestamp && new Date(this.archiveTimestamp);
238
+ }
239
+
240
+ /**
241
+ * The time the thread was created at
242
+ * @type {?Date}
243
+ * @readonly
244
+ */
245
+ get createdAt() {
246
+ return this.createdTimestamp && new Date(this.createdTimestamp);
247
+ }
248
+
249
+ /**
250
+ * The parent channel of this thread
251
+ * @type {?(NewsChannel|TextChannel|ForumChannel)}
252
+ * @readonly
253
+ */
254
+ get parent() {
255
+ return this.guild.channels.resolve(this.parentId);
256
+ }
257
+
258
+ /**
259
+ * Makes the client user join the thread.
260
+ * @returns {Promise<ThreadChannel>}
261
+ */
262
+ async join() {
263
+ await this.members.add('@me');
264
+ return this;
265
+ }
266
+
267
+ /**
268
+ * Makes the client user leave the thread.
269
+ * @returns {Promise<ThreadChannel>}
270
+ */
271
+ async leave() {
272
+ await this.members.remove('@me');
273
+ return this;
274
+ }
275
+
276
+ /**
277
+ * Gets the overall set of permissions for a member or role in this thread's parent channel, taking overwrites into
278
+ * account.
279
+ * @param {GuildMemberResolvable|RoleResolvable} memberOrRole The member or role to obtain the overall permissions for
280
+ * @param {boolean} [checkAdmin=true] Whether having the {@link PermissionFlagsBits.Administrator} permission
281
+ * will return all permissions
282
+ * @returns {?Readonly<PermissionsBitField>}
283
+ */
284
+ permissionsFor(memberOrRole, checkAdmin) {
285
+ return this.parent?.permissionsFor(memberOrRole, checkAdmin) ?? null;
286
+ }
287
+
288
+ /**
289
+ * Fetches the owner of this thread. If the thread member object isn't needed,
290
+ * use {@link ThreadChannel#ownerId} instead.
291
+ * @param {BaseFetchOptions} [options] The options for fetching the member
292
+ * @returns {Promise<?ThreadMember>}
293
+ */
294
+ async fetchOwner({ cache = true, force = false } = {}) {
295
+ if (!force) {
296
+ const existing = this.members.cache.get(this.ownerId);
297
+ if (existing) return existing;
298
+ }
299
+
300
+ // We cannot fetch a single thread member, as of this commit's date, Discord API responds with 405
301
+ const members = await this.members.fetch({ cache });
302
+ return members.get(this.ownerId) ?? null;
303
+ }
304
+
305
+ /**
306
+ * Fetches the message that started this thread, if any.
307
+ * <info>The `Promise` will reject if the original message in a forum post is deleted
308
+ * or when the original message in the parent channel is deleted.
309
+ * If you just need the id of that message, use {@link ThreadChannel#id} instead.</info>
310
+ * @param {BaseFetchOptions} [options] Additional options for this fetch
311
+ * @returns {Promise<Message<true>|null>}
312
+ */
313
+ // eslint-disable-next-line require-await
314
+ async fetchStarterMessage(options) {
315
+ const channel = this.parent?.type === ChannelType.GuildForum ? this : this.parent;
316
+ return channel?.messages.fetch({ message: this.id, ...options }) ?? null;
317
+ }
318
+
319
+ /**
320
+ * The options used to edit a thread channel
321
+ * @typedef {Object} ThreadEditOptions
322
+ * @property {string} [name] The new name for the thread
323
+ * @property {boolean} [archived] Whether the thread is archived
324
+ * @property {ThreadAutoArchiveDuration} [autoArchiveDuration] The amount of time after which the thread
325
+ * should automatically archive in case of no recent activity
326
+ * @property {number} [rateLimitPerUser] The rate limit per user (slowmode) for the thread in seconds
327
+ * @property {boolean} [locked] Whether the thread is locked
328
+ * @property {boolean} [invitable] Whether non-moderators can add other non-moderators to a thread
329
+ * <info>Can only be edited on {@link ChannelType.PrivateThread}</info>
330
+ * @property {Snowflake[]} [appliedTags] The tags to apply to the thread
331
+ * @property {ChannelFlagsResolvable} [flags] The flags to set on the channel
332
+ * @property {string} [reason] Reason for editing the thread
333
+ */
334
+
335
+ /**
336
+ * Edits this thread.
337
+ * @param {ThreadEditOptions} options The options to provide
338
+ * @returns {Promise<ThreadChannel>}
339
+ * @example
340
+ * // Edit a thread
341
+ * thread.edit({ name: 'new-thread' })
342
+ * .then(editedThread => console.log(editedThread))
343
+ * .catch(console.error);
344
+ */
345
+ async edit(options) {
346
+ const newData = await this.client.rest.patch(Routes.channel(this.id), {
347
+ body: {
348
+ name: (options.name ?? this.name).trim(),
349
+ archived: options.archived,
350
+ auto_archive_duration: options.autoArchiveDuration,
351
+ rate_limit_per_user: options.rateLimitPerUser,
352
+ locked: options.locked,
353
+ invitable: this.type === ChannelType.PrivateThread ? options.invitable : undefined,
354
+ applied_tags: options.appliedTags,
355
+ flags: 'flags' in options ? ChannelFlagsBitField.resolve(options.flags) : undefined,
356
+ },
357
+ reason: options.reason,
358
+ });
359
+
360
+ return this.client.actions.ChannelUpdate.handle(newData).updated;
361
+ }
362
+
363
+ /**
364
+ * Sets whether the thread is archived.
365
+ * @param {boolean} [archived=true] Whether the thread is archived
366
+ * @param {string} [reason] Reason for archiving or unarchiving
367
+ * @returns {Promise<ThreadChannel>}
368
+ * @example
369
+ * // Archive the thread
370
+ * thread.setArchived(true)
371
+ * .then(newThread => console.log(`Thread is now ${newThread.archived ? 'archived' : 'active'}`))
372
+ * .catch(console.error);
373
+ */
374
+ setArchived(archived = true, reason) {
375
+ return this.edit({ archived, reason });
376
+ }
377
+
378
+ /**
379
+ * Sets the duration after which the thread will automatically archive in case of no recent activity.
380
+ * @param {ThreadAutoArchiveDuration} autoArchiveDuration The amount of time after which the thread
381
+ * should automatically archive in case of no recent activity
382
+ * @param {string} [reason] Reason for changing the auto archive duration
383
+ * @returns {Promise<ThreadChannel>}
384
+ * @example
385
+ * // Set the thread's auto archive time to 1 hour
386
+ * thread.setAutoArchiveDuration(ThreadAutoArchiveDuration.OneHour)
387
+ * .then(newThread => {
388
+ * console.log(`Thread will now archive after ${newThread.autoArchiveDuration} minutes of inactivity`);
389
+ * });
390
+ * .catch(console.error);
391
+ */
392
+ setAutoArchiveDuration(autoArchiveDuration, reason) {
393
+ return this.edit({ autoArchiveDuration, reason });
394
+ }
395
+
396
+ /**
397
+ * Sets whether members without the {@link PermissionFlagsBits.ManageThreads} permission
398
+ * can invite other members to this thread.
399
+ * @param {boolean} [invitable=true] Whether non-moderators can invite non-moderators to this thread
400
+ * @param {string} [reason] Reason for changing invite
401
+ * @returns {Promise<ThreadChannel>}
402
+ */
403
+ setInvitable(invitable = true, reason) {
404
+ if (this.type !== ChannelType.PrivateThread) {
405
+ return Promise.reject(new DiscordjsRangeError(ErrorCodes.ThreadInvitableType, this.type));
406
+ }
407
+ return this.edit({ invitable, reason });
408
+ }
409
+
410
+ /**
411
+ * Sets whether the thread can be **unarchived** by anyone with the
412
+ * {@link PermissionFlagsBits.SendMessages} permission. When a thread is locked, only members with the
413
+ * {@link PermissionFlagsBits.ManageThreads} permission can unarchive it.
414
+ * @param {boolean} [locked=true] Whether the thread is locked
415
+ * @param {string} [reason] Reason for locking or unlocking the thread
416
+ * @returns {Promise<ThreadChannel>}
417
+ * @example
418
+ * // Set the thread to locked
419
+ * thread.setLocked(true)
420
+ * .then(newThread => console.log(`Thread is now ${newThread.locked ? 'locked' : 'unlocked'}`))
421
+ * .catch(console.error);
422
+ */
423
+ setLocked(locked = true, reason) {
424
+ return this.edit({ locked, reason });
425
+ }
426
+
427
+ /**
428
+ * Sets a new name for this thread.
429
+ * @param {string} name The new name for the thread
430
+ * @param {string} [reason] Reason for changing the thread's name
431
+ * @returns {Promise<ThreadChannel>}
432
+ * @example
433
+ * // Change the thread's name
434
+ * thread.setName('not_general')
435
+ * .then(newThread => console.log(`Thread's new name is ${newThread.name}`))
436
+ * .catch(console.error);
437
+ */
438
+ setName(name, reason) {
439
+ return this.edit({ name, reason });
440
+ }
441
+
442
+ /**
443
+ * Sets the rate limit per user (slowmode) for this thread.
444
+ * @param {number} rateLimitPerUser The new rate limit in seconds
445
+ * @param {string} [reason] Reason for changing the thread's rate limit
446
+ * @returns {Promise<ThreadChannel>}
447
+ */
448
+ setRateLimitPerUser(rateLimitPerUser, reason) {
449
+ return this.edit({ rateLimitPerUser, reason });
450
+ }
451
+
452
+ /**
453
+ * Set the applied tags for this channel (only applicable to forum threads)
454
+ * @param {Snowflake[]} appliedTags The tags to set for this channel
455
+ * @param {string} [reason] Reason for changing the thread's applied tags
456
+ * @returns {Promise<ThreadChannel>}
457
+ */
458
+ setAppliedTags(appliedTags, reason) {
459
+ return this.edit({ appliedTags, reason });
460
+ }
461
+
462
+ /**
463
+ * Pins this thread from the forum channel (only applicable to forum threads).
464
+ * @param {string} [reason] Reason for pinning
465
+ * @returns {Promise<ThreadChannel>}
466
+ */
467
+ pin(reason) {
468
+ return this.edit({ flags: this.flags.add(ChannelFlags.Pinned), reason });
469
+ }
470
+
471
+ /**
472
+ * Unpins this thread from the forum channel (only applicable to forum threads).
473
+ * @param {string} [reason] Reason for unpinning
474
+ * @returns {Promise<ThreadChannel>}
475
+ */
476
+ unpin(reason) {
477
+ return this.edit({ flags: this.flags.remove(ChannelFlags.Pinned), reason });
478
+ }
479
+
480
+ /**
481
+ * Whether the client user is a member of the thread.
482
+ * @type {boolean}
483
+ * @readonly
484
+ */
485
+ get joined() {
486
+ return this.members.cache.has(this.client.user?.id);
487
+ }
488
+
489
+ /**
490
+ * Whether the thread is editable by the client user (name, archived, autoArchiveDuration)
491
+ * @type {boolean}
492
+ * @readonly
493
+ */
494
+ get editable() {
495
+ return (
496
+ (this.ownerId === this.client.user.id && (this.type !== ChannelType.PrivateThread || this.joined)) ||
497
+ this.manageable
498
+ );
499
+ }
500
+
501
+ /**
502
+ * Whether the thread is joinable by the client user
503
+ * @type {boolean}
504
+ * @readonly
505
+ */
506
+ get joinable() {
507
+ return (
508
+ !this.archived &&
509
+ !this.joined &&
510
+ this.permissionsFor(this.client.user)?.has(
511
+ this.type === ChannelType.PrivateThread ? PermissionFlagsBits.ManageThreads : PermissionFlagsBits.ViewChannel,
512
+ false,
513
+ )
514
+ );
515
+ }
516
+
517
+ /**
518
+ * Whether the thread is manageable by the client user, for deleting or editing rateLimitPerUser or locked.
519
+ * @type {boolean}
520
+ * @readonly
521
+ */
522
+ get manageable() {
523
+ const permissions = this.permissionsFor(this.client.user);
524
+ if (!permissions) return false;
525
+ // This flag allows managing even if timed out
526
+ if (permissions.has(PermissionFlagsBits.Administrator, false)) return true;
527
+
528
+ return (
529
+ this.guild.members.me.communicationDisabledUntilTimestamp < Date.now() &&
530
+ permissions.has(PermissionFlagsBits.ManageThreads, false)
531
+ );
532
+ }
533
+
534
+ /**
535
+ * Whether the thread is viewable by the client user
536
+ * @type {boolean}
537
+ * @readonly
538
+ */
539
+ get viewable() {
540
+ if (this.client.user.id === this.guild.ownerId) return true;
541
+ const permissions = this.permissionsFor(this.client.user);
542
+ if (!permissions) return false;
543
+ return permissions.has(PermissionFlagsBits.ViewChannel, false);
544
+ }
545
+
546
+ /**
547
+ * Whether the client user can send messages in this thread
548
+ * @type {boolean}
549
+ * @readonly
550
+ */
551
+ get sendable() {
552
+ const permissions = this.permissionsFor(this.client.user);
553
+ if (!permissions) return false;
554
+ // This flag allows sending even if timed out
555
+ if (permissions.has(PermissionFlagsBits.Administrator, false)) return true;
556
+
557
+ return (
558
+ !(this.archived && this.locked && !this.manageable) &&
559
+ (this.type !== ChannelType.PrivateThread || this.joined || this.manageable) &&
560
+ permissions.has(PermissionFlagsBits.SendMessagesInThreads, false) &&
561
+ this.guild.members.me.communicationDisabledUntilTimestamp < Date.now()
562
+ );
563
+ }
564
+
565
+ /**
566
+ * Whether the thread is unarchivable by the client user
567
+ * @type {boolean}
568
+ * @readonly
569
+ */
570
+ get unarchivable() {
571
+ return this.archived && this.sendable && (!this.locked || this.manageable);
572
+ }
573
+
574
+ /**
575
+ * Deletes this thread.
576
+ * @param {string} [reason] Reason for deleting this thread
577
+ * @returns {Promise<ThreadChannel>}
578
+ * @example
579
+ * // Delete the thread
580
+ * thread.delete('cleaning out old threads')
581
+ * .then(deletedThread => console.log(deletedThread))
582
+ * .catch(console.error);
583
+ */
584
+ async delete(reason) {
585
+ await this.guild.channels.delete(this.id, reason);
586
+ return this;
587
+ }
588
+
589
+ // These are here only for documentation purposes - they are implemented by TextBasedChannel
590
+ /* eslint-disable no-empty-function */
591
+ get lastMessage() {}
592
+ get lastPinAt() {}
593
+ send() {}
594
+ sendTyping() {}
595
+ createMessageCollector() {}
596
+ awaitMessages() {}
597
+ createMessageComponentCollector() {}
598
+ awaitMessageComponent() {}
599
+ bulkDelete() {}
600
+ // Doesn't work on Thread channels; setRateLimitPerUser() {}
601
+ // Doesn't work on Thread channels; setNSFW() {}
602
+ }
603
+
604
+ TextBasedChannel.applyToClass(ThreadChannel, true, ['fetchWebhooks', 'setRateLimitPerUser', 'setNSFW']);
605
+
606
+ module.exports = ThreadChannel;
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ const Base = require('./Base');
4
+ const ThreadMemberFlagsBitField = require('../util/ThreadMemberFlagsBitField');
5
+
6
+ /**
7
+ * Represents a Member for a Thread.
8
+ * @extends {Base}
9
+ */
10
+ class ThreadMember extends Base {
11
+ constructor(thread, data, extra = {}) {
12
+ super(thread.client);
13
+
14
+ /**
15
+ * The thread that this member is a part of
16
+ * @type {ThreadChannel}
17
+ */
18
+ this.thread = thread;
19
+
20
+ /**
21
+ * The timestamp the member last joined the thread at
22
+ * @type {?number}
23
+ */
24
+ this.joinedTimestamp = null;
25
+
26
+ /**
27
+ * The flags for this thread member. This will be `null` if partial.
28
+ * @type {?ThreadMemberFlagsBitField}
29
+ */
30
+ this.flags = null;
31
+
32
+ /**
33
+ * The id of the thread member
34
+ * @type {Snowflake}
35
+ */
36
+ this.id = data.user_id;
37
+
38
+ this._patch(data, extra);
39
+ }
40
+
41
+ _patch(data, extra = {}) {
42
+ if ('join_timestamp' in data) this.joinedTimestamp = Date.parse(data.join_timestamp);
43
+ if ('flags' in data) this.flags = new ThreadMemberFlagsBitField(data.flags).freeze();
44
+
45
+ if ('member' in data) {
46
+ /**
47
+ * The guild member associated with this thread member.
48
+ * @type {?GuildMember}
49
+ * @private
50
+ */
51
+ this.member = this.thread.guild.members._add(data.member, extra.cache);
52
+ } else {
53
+ this.member ??= null;
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Whether this thread member is a partial
59
+ * @type {boolean}
60
+ * @readonly
61
+ */
62
+ get partial() {
63
+ return this.flags === null;
64
+ }
65
+
66
+ /**
67
+ * The guild member associated with this thread member
68
+ * @type {?GuildMember}
69
+ * @readonly
70
+ */
71
+ get guildMember() {
72
+ return this.member ?? this.thread.guild.members.resolve(this.id);
73
+ }
74
+
75
+ /**
76
+ * The last time this member joined the thread
77
+ * @type {?Date}
78
+ * @readonly
79
+ */
80
+ get joinedAt() {
81
+ return this.joinedTimestamp && new Date(this.joinedTimestamp);
82
+ }
83
+
84
+ /**
85
+ * The user associated with this thread member
86
+ * @type {?User}
87
+ * @readonly
88
+ */
89
+ get user() {
90
+ return this.client.users.resolve(this.id);
91
+ }
92
+
93
+ /**
94
+ * Whether the client user can manage this thread member
95
+ * @type {boolean}
96
+ * @readonly
97
+ */
98
+ get manageable() {
99
+ return !this.thread.archived && this.thread.editable;
100
+ }
101
+
102
+ /**
103
+ * Removes this member from the thread.
104
+ * @param {string} [reason] Reason for removing the member
105
+ * @returns {ThreadMember}
106
+ */
107
+ async remove(reason) {
108
+ await this.thread.members.remove(this.id, reason);
109
+ return this;
110
+ }
111
+ }
112
+
113
+ module.exports = ThreadMember;