chapterjs 0.1.1 → 0.1.4

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 (320) hide show
  1. package/dist/assets/asset.d.ts +46 -0
  2. package/dist/assets/asset.js +86 -0
  3. package/dist/assets/public.d.ts +19 -0
  4. package/dist/assets/public.js +120 -0
  5. package/dist/cache/cache.d.ts +62 -0
  6. package/dist/cache/cache.js +80 -0
  7. package/dist/cache/store.d.ts +45 -0
  8. package/dist/cache/store.js +53 -0
  9. package/dist/cli/build.d.ts +41 -0
  10. package/dist/cli/build.js +206 -0
  11. package/dist/cli/cli.js +108 -25
  12. package/dist/cli/cluster.d.ts +70 -0
  13. package/dist/cli/cluster.js +213 -0
  14. package/dist/cli/dev.d.ts +18 -1
  15. package/dist/cli/dev.js +252 -198
  16. package/dist/cli/env.d.ts +31 -0
  17. package/dist/cli/env.js +93 -0
  18. package/dist/cli/log.d.ts +13 -0
  19. package/dist/cli/log.js +17 -0
  20. package/dist/cli/node-version.d.ts +8 -0
  21. package/dist/cli/node-version.js +13 -0
  22. package/dist/cli/preflight.d.ts +36 -0
  23. package/dist/cli/preflight.js +122 -0
  24. package/dist/cli/project.d.ts +122 -0
  25. package/dist/cli/project.js +452 -0
  26. package/dist/cli/start.d.ts +25 -0
  27. package/dist/cli/start.js +225 -0
  28. package/dist/commands/autocomplete.d.ts +13 -0
  29. package/dist/commands/autocomplete.js +159 -0
  30. package/dist/commands/command.d.ts +346 -0
  31. package/dist/commands/command.js +28 -0
  32. package/dist/commands/convention.d.ts +64 -0
  33. package/dist/commands/convention.js +464 -0
  34. package/dist/commands/register.d.ts +18 -0
  35. package/dist/commands/register.js +42 -0
  36. package/dist/commands/router.d.ts +20 -0
  37. package/dist/commands/router.js +228 -0
  38. package/dist/commands/tree.d.ts +43 -0
  39. package/dist/commands/tree.js +206 -0
  40. package/dist/commands/twins.d.ts +15 -0
  41. package/dist/commands/twins.js +78 -0
  42. package/dist/components/button.d.ts +79 -0
  43. package/dist/components/button.js +108 -0
  44. package/dist/components/component.d.ts +140 -0
  45. package/dist/components/component.js +39 -0
  46. package/dist/components/convention.d.ts +35 -0
  47. package/dist/components/convention.js +339 -0
  48. package/dist/components/custom-id.d.ts +72 -0
  49. package/dist/components/custom-id.js +128 -0
  50. package/dist/components/embed.d.ts +28 -0
  51. package/dist/components/embed.js +11 -0
  52. package/dist/components/file.d.ts +30 -0
  53. package/dist/components/file.js +52 -0
  54. package/dist/components/instance.d.ts +100 -0
  55. package/dist/components/instance.js +86 -0
  56. package/dist/components/layout.d.ts +103 -0
  57. package/dist/components/layout.js +279 -0
  58. package/dist/components/modal.d.ts +244 -0
  59. package/dist/components/modal.js +399 -0
  60. package/dist/components/options.d.ts +41 -0
  61. package/dist/components/options.js +101 -0
  62. package/dist/components/render.d.ts +12 -0
  63. package/dist/components/render.js +58 -0
  64. package/dist/components/router.d.ts +18 -0
  65. package/dist/components/router.js +256 -0
  66. package/dist/components/select.d.ts +144 -0
  67. package/dist/components/select.js +179 -0
  68. package/dist/core/bot.d.ts +99 -0
  69. package/dist/core/bot.js +174 -0
  70. package/dist/core/memory.d.ts +66 -0
  71. package/dist/core/memory.js +115 -0
  72. package/dist/discord/api.d.ts +112 -0
  73. package/dist/discord/api.js +115 -0
  74. package/dist/discord/cdn.d.ts +67 -0
  75. package/dist/discord/cdn.js +100 -0
  76. package/dist/discord/codes.d.ts +171 -0
  77. package/dist/discord/codes.js +421 -0
  78. package/dist/discord/endpoint.d.ts +56 -0
  79. package/dist/discord/endpoint.js +53 -0
  80. package/dist/discord/endpoints.d.ts +1529 -0
  81. package/dist/discord/endpoints.js +1139 -0
  82. package/dist/discord/formatting.d.ts +67 -0
  83. package/dist/discord/formatting.js +87 -0
  84. package/dist/discord/index.d.ts +10 -0
  85. package/dist/discord/index.js +13 -0
  86. package/dist/discord/intents.d.ts +48 -0
  87. package/dist/discord/intents.js +128 -0
  88. package/dist/discord/permissions.d.ts +94 -0
  89. package/dist/discord/permissions.js +185 -0
  90. package/dist/discord/snowflake.d.ts +21 -0
  91. package/dist/discord/snowflake.js +36 -0
  92. package/dist/discord/types/application-command.d.ts +332 -0
  93. package/dist/discord/types/application-command.js +48 -0
  94. package/dist/discord/types/application-role-connection-metadata.d.ts +42 -0
  95. package/dist/discord/types/application-role-connection-metadata.js +22 -0
  96. package/dist/discord/types/application.d.ts +208 -0
  97. package/dist/discord/types/application.js +48 -0
  98. package/dist/discord/types/audit-log.d.ts +255 -0
  99. package/dist/discord/types/audit-log.js +144 -0
  100. package/dist/discord/types/auto-moderation.d.ts +167 -0
  101. package/dist/discord/types/auto-moderation.js +52 -0
  102. package/dist/discord/types/channel.d.ts +557 -0
  103. package/dist/discord/types/channel.js +88 -0
  104. package/dist/discord/types/common.d.ts +54 -0
  105. package/dist/discord/types/common.js +38 -0
  106. package/dist/discord/types/component.d.ts +775 -0
  107. package/dist/discord/types/component.js +76 -0
  108. package/dist/discord/types/emoji.d.ts +74 -0
  109. package/dist/discord/types/emoji.js +1 -0
  110. package/dist/discord/types/entitlement.d.ts +84 -0
  111. package/dist/discord/types/entitlement.js +22 -0
  112. package/dist/discord/types/gateway-events.d.ts +1148 -0
  113. package/dist/discord/types/gateway-events.js +54 -0
  114. package/dist/discord/types/gateway.d.ts +38 -0
  115. package/dist/discord/types/gateway.js +1 -0
  116. package/dist/discord/types/guild-scheduled-event.d.ts +257 -0
  117. package/dist/discord/types/guild-scheduled-event.js +68 -0
  118. package/dist/discord/types/guild-template.d.ts +51 -0
  119. package/dist/discord/types/guild-template.js +1 -0
  120. package/dist/discord/types/guild.d.ts +910 -0
  121. package/dist/discord/types/guild.js +153 -0
  122. package/dist/discord/types/index.d.ts +31 -0
  123. package/dist/discord/types/index.js +31 -0
  124. package/dist/discord/types/interaction.d.ts +321 -0
  125. package/dist/discord/types/interaction.js +47 -0
  126. package/dist/discord/types/invite.d.ts +158 -0
  127. package/dist/discord/types/invite.js +39 -0
  128. package/dist/discord/types/message.d.ts +834 -0
  129. package/dist/discord/types/message.js +159 -0
  130. package/dist/discord/types/oauth2.d.ts +22 -0
  131. package/dist/discord/types/oauth2.js +1 -0
  132. package/dist/discord/types/permissions.d.ts +183 -0
  133. package/dist/discord/types/permissions.js +118 -0
  134. package/dist/discord/types/poll.d.ts +106 -0
  135. package/dist/discord/types/poll.js +8 -0
  136. package/dist/discord/types/sku.d.ts +47 -0
  137. package/dist/discord/types/sku.js +26 -0
  138. package/dist/discord/types/soundboard.d.ts +71 -0
  139. package/dist/discord/types/soundboard.js +1 -0
  140. package/dist/discord/types/stage-instance.d.ts +58 -0
  141. package/dist/discord/types/stage-instance.js +10 -0
  142. package/dist/discord/types/sticker.d.ts +115 -0
  143. package/dist/discord/types/sticker.js +20 -0
  144. package/dist/discord/types/subscription.d.ts +54 -0
  145. package/dist/discord/types/subscription.js +12 -0
  146. package/dist/discord/types/team.d.ts +55 -0
  147. package/dist/discord/types/team.js +21 -0
  148. package/dist/discord/types/user.d.ts +273 -0
  149. package/dist/discord/types/user.js +83 -0
  150. package/dist/discord/types/voice.d.ts +72 -0
  151. package/dist/discord/types/voice.js +1 -0
  152. package/dist/discord/types/webhook.d.ts +184 -0
  153. package/dist/discord/types/webhook.js +12 -0
  154. package/dist/events/convention.d.ts +25 -0
  155. package/dist/events/convention.js +97 -0
  156. package/dist/events/event.d.ts +8 -0
  157. package/dist/events/event.js +20 -0
  158. package/dist/events/registry.d.ts +706 -0
  159. package/dist/events/registry.js +839 -0
  160. package/dist/events/router.d.ts +45 -0
  161. package/dist/events/router.js +133 -0
  162. package/dist/events/types.d.ts +7 -0
  163. package/dist/events/types.js +48 -0
  164. package/dist/gateway/errors.d.ts +18 -0
  165. package/dist/gateway/errors.js +47 -0
  166. package/dist/gateway/gateway.d.ts +64 -0
  167. package/dist/gateway/gateway.js +117 -0
  168. package/dist/gateway/identify-queue.d.ts +25 -0
  169. package/dist/gateway/identify-queue.js +29 -0
  170. package/dist/gateway/shard.d.ts +94 -0
  171. package/dist/gateway/shard.js +414 -0
  172. package/dist/gateway/state.d.ts +11 -0
  173. package/dist/gateway/state.js +184 -0
  174. package/dist/index.d.ts +55 -18
  175. package/dist/index.js +38 -12
  176. package/dist/interactions/cooldown.d.ts +33 -0
  177. package/dist/interactions/cooldown.js +74 -0
  178. package/dist/interactions/dispatch.d.ts +91 -0
  179. package/dist/interactions/dispatch.js +110 -0
  180. package/dist/loader/generated.d.ts +37 -0
  181. package/dist/loader/generated.js +114 -0
  182. package/dist/loader/hot.d.ts +13 -0
  183. package/dist/loader/hot.js +85 -0
  184. package/dist/loader/loader.d.ts +72 -0
  185. package/dist/loader/loader.js +135 -0
  186. package/dist/loader/locate.d.ts +10 -0
  187. package/dist/loader/locate.js +36 -0
  188. package/dist/loader/watch.d.ts +17 -0
  189. package/dist/loader/watch.js +66 -0
  190. package/dist/main.d.ts +3 -0
  191. package/dist/main.js +6 -0
  192. package/dist/messages/commands.d.ts +23 -0
  193. package/dist/messages/commands.js +123 -0
  194. package/dist/messages/convention.d.ts +89 -0
  195. package/dist/messages/convention.js +347 -0
  196. package/dist/messages/messages.d.ts +219 -0
  197. package/dist/messages/messages.js +30 -0
  198. package/dist/messages/phrases.d.ts +23 -0
  199. package/dist/messages/phrases.js +44 -0
  200. package/dist/messages/translate.d.ts +100 -0
  201. package/dist/messages/translate.js +249 -0
  202. package/dist/presence/convention.d.ts +26 -0
  203. package/dist/presence/convention.js +133 -0
  204. package/dist/presence/presence.d.ts +57 -0
  205. package/dist/presence/presence.js +27 -0
  206. package/dist/rest/errors.d.ts +55 -0
  207. package/dist/rest/errors.js +92 -0
  208. package/dist/rest/rate-limiter.d.ts +46 -0
  209. package/dist/rest/rate-limiter.js +153 -0
  210. package/dist/rest/rest.d.ts +63 -0
  211. package/dist/rest/rest.js +194 -0
  212. package/dist/structures/base.d.ts +48 -0
  213. package/dist/structures/base.js +84 -0
  214. package/dist/structures/channel.d.ts +380 -0
  215. package/dist/structures/channel.js +627 -0
  216. package/dist/structures/context.d.ts +30 -0
  217. package/dist/structures/context.js +7 -0
  218. package/dist/structures/emoji.d.ts +46 -0
  219. package/dist/structures/emoji.js +65 -0
  220. package/dist/structures/entities.d.ts +62 -0
  221. package/dist/structures/entities.js +226 -0
  222. package/dist/structures/guild.d.ts +348 -0
  223. package/dist/structures/guild.js +466 -0
  224. package/dist/structures/interaction.d.ts +197 -0
  225. package/dist/structures/interaction.js +396 -0
  226. package/dist/structures/invite.d.ts +38 -0
  227. package/dist/structures/invite.js +57 -0
  228. package/dist/structures/known.d.ts +28 -0
  229. package/dist/structures/known.js +58 -0
  230. package/dist/structures/member.d.ts +140 -0
  231. package/dist/structures/member.js +260 -0
  232. package/dist/structures/message.d.ts +197 -0
  233. package/dist/structures/message.js +268 -0
  234. package/dist/structures/payload.d.ts +91 -0
  235. package/dist/structures/payload.js +184 -0
  236. package/dist/structures/role.d.ts +69 -0
  237. package/dist/structures/role.js +112 -0
  238. package/dist/structures/user.d.ts +59 -0
  239. package/dist/structures/user.js +94 -0
  240. package/dist/structures/webhook.d.ts +46 -0
  241. package/dist/structures/webhook.js +79 -0
  242. package/dist/tasks/convention.d.ts +26 -0
  243. package/dist/tasks/convention.js +78 -0
  244. package/dist/tasks/schedule.d.ts +54 -0
  245. package/dist/tasks/schedule.js +236 -0
  246. package/dist/tasks/scheduler.d.ts +33 -0
  247. package/dist/tasks/scheduler.js +107 -0
  248. package/dist/tasks/task.d.ts +76 -0
  249. package/dist/tasks/task.js +28 -0
  250. package/dist/util/case.d.ts +13 -0
  251. package/dist/util/case.js +54 -0
  252. package/dist/util/duration.d.ts +8 -0
  253. package/dist/util/duration.js +29 -0
  254. package/dist/util/websocket.d.ts +9 -0
  255. package/dist/util/websocket.js +31 -0
  256. package/dist/voice/connection.d.ts +90 -0
  257. package/dist/voice/connection.js +494 -0
  258. package/dist/voice/dave.d.ts +109 -0
  259. package/dist/voice/dave.js +245 -0
  260. package/dist/voice/opus.d.ts +19 -0
  261. package/dist/voice/opus.js +273 -0
  262. package/dist/voice/session.d.ts +55 -0
  263. package/dist/voice/session.js +405 -0
  264. package/dist/voice/source.d.ts +22 -0
  265. package/dist/voice/source.js +195 -0
  266. package/dist/voice/transport.d.ts +43 -0
  267. package/dist/voice/transport.js +140 -0
  268. package/dist/voice/usage.d.ts +4 -0
  269. package/dist/voice/usage.js +33 -0
  270. package/package.json +26 -7
  271. package/vendor/dave/LICENSE +231 -0
  272. package/vendor/dave/libdave.d.mts +99 -0
  273. package/vendor/dave/libdave.mjs +2 -0
  274. package/vendor/dave/libdave.wasm +0 -0
  275. package/dist/components.d.ts +0 -97
  276. package/dist/components.js +0 -260
  277. package/dist/context.d.ts +0 -72
  278. package/dist/context.js +0 -273
  279. package/dist/discord/client.d.ts +0 -35
  280. package/dist/discord/client.js +0 -66
  281. package/dist/discord/constants.d.ts +0 -49
  282. package/dist/discord/constants.js +0 -61
  283. package/dist/discord/events.d.ts +0 -129
  284. package/dist/discord/events.js +0 -102
  285. package/dist/discord/gateway.d.ts +0 -61
  286. package/dist/discord/gateway.js +0 -320
  287. package/dist/discord/payloads.d.ts +0 -275
  288. package/dist/discord/payloads.js +0 -3
  289. package/dist/discord/presence.d.ts +0 -42
  290. package/dist/discord/presence.js +0 -51
  291. package/dist/discord/rest.d.ts +0 -49
  292. package/dist/discord/rest.js +0 -271
  293. package/dist/discord/types.d.ts +0 -334
  294. package/dist/discord/types.js +0 -4
  295. package/dist/events.d.ts +0 -94
  296. package/dist/events.js +0 -255
  297. package/dist/presence.d.ts +0 -92
  298. package/dist/presence.js +0 -250
  299. package/dist/structures/Channel.d.ts +0 -110
  300. package/dist/structures/Channel.js +0 -171
  301. package/dist/structures/Guild.d.ts +0 -70
  302. package/dist/structures/Guild.js +0 -105
  303. package/dist/structures/Interaction.d.ts +0 -113
  304. package/dist/structures/Interaction.js +0 -274
  305. package/dist/structures/Member.d.ts +0 -70
  306. package/dist/structures/Member.js +0 -124
  307. package/dist/structures/Message.d.ts +0 -87
  308. package/dist/structures/Message.js +0 -182
  309. package/dist/structures/Reaction.d.ts +0 -38
  310. package/dist/structures/Reaction.js +0 -64
  311. package/dist/structures/Role.d.ts +0 -43
  312. package/dist/structures/Role.js +0 -77
  313. package/dist/structures/User.d.ts +0 -41
  314. package/dist/structures/User.js +0 -79
  315. package/dist/structures/utils.d.ts +0 -72
  316. package/dist/structures/utils.js +0 -59
  317. package/dist/utils/errors.d.ts +0 -5
  318. package/dist/utils/errors.js +0 -28
  319. package/dist/utils/modules.d.ts +0 -39
  320. package/dist/utils/modules.js +0 -156
@@ -1,102 +0,0 @@
1
- import { GatewayIntents } from './constants.js';
2
- const { Guilds, GuildMembers, GuildModeration, GuildExpressions, GuildIntegrations, GuildWebhooks, GuildInvites, GuildVoiceStates, GuildPresences, GuildMessages, GuildMessageReactions, GuildMessageTyping, DirectMessages, DirectMessageReactions, DirectMessageTyping, GuildScheduledEvents, AutoModerationConfiguration, AutoModerationExecution, GuildMessagePolls, DirectMessagePolls, } = GatewayIntents;
3
- /**
4
- * Every dispatch event an app can receive, by camelCase name, with the intents
5
- * that make Discord send it (0 when it is always sent). An event listed under
6
- * several intents (guild and DM) needs any of them; all are requested.
7
- *
8
- * https://docs.discord.com/developers/events/gateway-events#receive-events
9
- * https://docs.discord.com/developers/events/gateway#list-of-intents
10
- */
11
- export const GATEWAY_EVENTS = {
12
- ready: 0,
13
- resumed: 0,
14
- rateLimited: 0,
15
- applicationCommandPermissionsUpdate: 0,
16
- autoModerationRuleCreate: AutoModerationConfiguration,
17
- autoModerationRuleUpdate: AutoModerationConfiguration,
18
- autoModerationRuleDelete: AutoModerationConfiguration,
19
- autoModerationActionExecution: AutoModerationExecution,
20
- channelCreate: Guilds,
21
- channelUpdate: Guilds,
22
- channelDelete: Guilds,
23
- channelInfo: 0,
24
- channelPinsUpdate: Guilds | DirectMessages,
25
- voiceChannelStatusUpdate: Guilds,
26
- voiceChannelStartTimeUpdate: Guilds,
27
- threadCreate: Guilds,
28
- threadUpdate: Guilds,
29
- threadDelete: Guilds,
30
- threadListSync: Guilds,
31
- threadMemberUpdate: Guilds,
32
- // Other members' updates also need the privileged GuildMembers intent
33
- threadMembersUpdate: Guilds,
34
- entitlementCreate: 0,
35
- entitlementUpdate: 0,
36
- entitlementDelete: 0,
37
- guildCreate: Guilds,
38
- guildUpdate: Guilds,
39
- guildDelete: Guilds,
40
- guildAuditLogEntryCreate: GuildModeration,
41
- guildBanAdd: GuildModeration,
42
- guildBanRemove: GuildModeration,
43
- guildEmojisUpdate: GuildExpressions,
44
- guildStickersUpdate: GuildExpressions,
45
- guildIntegrationsUpdate: GuildIntegrations,
46
- guildMemberAdd: GuildMembers,
47
- guildMemberRemove: GuildMembers,
48
- guildMemberUpdate: GuildMembers,
49
- guildMembersChunk: 0,
50
- guildRoleCreate: Guilds,
51
- guildRoleUpdate: Guilds,
52
- guildRoleDelete: Guilds,
53
- guildScheduledEventCreate: GuildScheduledEvents,
54
- guildScheduledEventUpdate: GuildScheduledEvents,
55
- guildScheduledEventDelete: GuildScheduledEvents,
56
- guildScheduledEventUserAdd: GuildScheduledEvents,
57
- guildScheduledEventUserRemove: GuildScheduledEvents,
58
- guildSoundboardSoundCreate: GuildExpressions,
59
- guildSoundboardSoundUpdate: GuildExpressions,
60
- guildSoundboardSoundDelete: GuildExpressions,
61
- guildSoundboardSoundsUpdate: GuildExpressions,
62
- soundboardSounds: 0,
63
- integrationCreate: GuildIntegrations,
64
- integrationUpdate: GuildIntegrations,
65
- integrationDelete: GuildIntegrations,
66
- interactionCreate: 0,
67
- inviteCreate: GuildInvites,
68
- inviteDelete: GuildInvites,
69
- messageCreate: GuildMessages | DirectMessages,
70
- messageUpdate: GuildMessages | DirectMessages,
71
- messageDelete: GuildMessages | DirectMessages,
72
- messageDeleteBulk: GuildMessages,
73
- messageReactionAdd: GuildMessageReactions | DirectMessageReactions,
74
- messageReactionRemove: GuildMessageReactions | DirectMessageReactions,
75
- messageReactionRemoveAll: GuildMessageReactions | DirectMessageReactions,
76
- messageReactionRemoveEmoji: GuildMessageReactions | DirectMessageReactions,
77
- presenceUpdate: GuildPresences,
78
- stageInstanceCreate: Guilds,
79
- stageInstanceUpdate: Guilds,
80
- stageInstanceDelete: Guilds,
81
- subscriptionCreate: 0,
82
- subscriptionUpdate: 0,
83
- subscriptionDelete: 0,
84
- typingStart: GuildMessageTyping | DirectMessageTyping,
85
- userUpdate: 0,
86
- voiceChannelEffectSend: GuildVoiceStates,
87
- voiceStateUpdate: GuildVoiceStates,
88
- voiceServerUpdate: 0,
89
- webhooksUpdate: GuildWebhooks,
90
- messagePollVoteAdd: GuildMessagePolls | DirectMessagePolls,
91
- messagePollVoteRemove: GuildMessagePolls | DirectMessagePolls,
92
- };
93
- /** `messageCreate` → `MESSAGE_CREATE`, as sent by Discord. */
94
- export function toDispatchName(name) {
95
- return name.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase();
96
- }
97
- /** Intents only granted once enabled in the Developer Portal (Bot > Privileged Gateway Intents). */
98
- export const PRIVILEGED_INTENTS = {
99
- GuildMembers: GatewayIntents.GuildMembers,
100
- GuildPresences: GatewayIntents.GuildPresences,
101
- MessageContent: GatewayIntents.MessageContent,
102
- };
@@ -1,61 +0,0 @@
1
- import { EventEmitter } from 'node:events';
2
- import { type Presence } from './presence.js';
3
- import { RestClient } from './rest.js';
4
- import { type Client } from './client.js';
5
- import { User } from '../structures/User.js';
6
- /** Error emitted with `fatal` when Discord closed the connection for good. */
7
- export declare class GatewayCloseError extends Error {
8
- /** https://docs.discord.com/developers/topics/opcodes-and-status-codes#gateway-gateway-close-event-codes */
9
- readonly code: number;
10
- constructor(message: string,
11
- /** https://docs.discord.com/developers/topics/opcodes-and-status-codes#gateway-gateway-close-event-codes */
12
- code: number);
13
- }
14
- export interface GatewayOptions {
15
- token: string;
16
- /** Bitfield of gateway intents, see `GatewayIntents`. Defaults to `Guilds`. */
17
- intents?: number;
18
- /** Guild reserved for development, see `dev`. */
19
- devGuildId?: string;
20
- /**
21
- * In dev mode, only events from `devGuildId` reach your handlers.
22
- * Otherwise, events from `devGuildId` are ignored so a dev process and a
23
- * production process can share the same bot.
24
- */
25
- dev?: boolean;
26
- /** Status and activity shown when the bot connects, see `setPresence`. */
27
- presence?: Presence;
28
- }
29
- /**
30
- * The gateway connection, internal to the framework: only `chapterjs dev`
31
- * drives it (login, intents, presence, shutdown). User code gets its
32
- * read-only `client`.
33
- */
34
- export declare class Gateway extends EventEmitter {
35
- #private;
36
- readonly rest: RestClient;
37
- /** What handlers receive, see `Client`. */
38
- readonly client: Client;
39
- /** The bot's user, set once connected. */
40
- user: User | null;
41
- applicationId: string | null;
42
- /** Ids of the guilds the bot is in, as sent in READY. */
43
- guildIds: Set<string>;
44
- constructor(options: GatewayOptions);
45
- /** Connects to the gateway and resolves once the bot is ready. */
46
- login(): Promise<User>;
47
- /** Bitfield of the gateway intents of the current session. */
48
- get intents(): number;
49
- /**
50
- * Changes the gateway intents. Intents are only sent when identifying, so a
51
- * connected client starts a new session (READY is dispatched again).
52
- */
53
- setIntents(intents: number): void;
54
- /** Updates the bot's status and activity, e.g. `{ activity: { type: 'playing', name: 'Minecraft' } }`. */
55
- setPresence(presence: Presence): void;
56
- /**
57
- * Closes the connection. Resolves once Discord acknowledged the close, so the
58
- * session ends right away instead of lingering online until it times out.
59
- */
60
- destroy(): Promise<void>;
61
- }
@@ -1,320 +0,0 @@
1
- import { EventEmitter } from 'node:events';
2
- import { API_VERSION, FATAL_CLOSE_CODES, GatewayCloseCodes, GatewayIntents, GatewayOpcodes, } from './constants.js';
3
- import { toGatewayPresence } from './presence.js';
4
- import { RestClient } from './rest.js';
5
- import { createClient } from './client.js';
6
- import { User } from '../structures/User.js';
7
- /** Error emitted with `fatal` when Discord closed the connection for good. */
8
- export class GatewayCloseError extends Error {
9
- code;
10
- constructor(message,
11
- /** https://docs.discord.com/developers/topics/opcodes-and-status-codes#gateway-gateway-close-event-codes */
12
- code) {
13
- super(message);
14
- this.code = code;
15
- }
16
- }
17
- /** Events that don't belong to any guild and must always go through. */
18
- const SYSTEM_EVENTS = new Set([
19
- 'READY',
20
- 'RESUMED',
21
- 'USER_UPDATE',
22
- 'RATE_LIMITED',
23
- ]);
24
- /** Guild events carry the guild id in `id` instead of `guild_id`. */
25
- const GUILD_EVENTS = new Set(['GUILD_CREATE', 'GUILD_UPDATE', 'GUILD_DELETE']);
26
- /**
27
- * Minimum delay between two presence updates. The gateway allows 120 events
28
- * per minute per connection and disconnects apps going over it.
29
- */
30
- const PRESENCE_UPDATE_INTERVAL = 5_000;
31
- /** Any non-1000/1001 close code keeps the session resumable. */
32
- const RECONNECT_CLOSE_CODE = 4000;
33
- /**
34
- * The gateway connection, internal to the framework: only `chapterjs dev`
35
- * drives it (login, intents, presence, shutdown). User code gets its
36
- * read-only `client`.
37
- */
38
- export class Gateway extends EventEmitter {
39
- rest;
40
- /** What handlers receive, see `Client`. */
41
- client;
42
- /** The bot's user, set once connected. */
43
- user = null;
44
- applicationId = null;
45
- /** Ids of the guilds the bot is in, as sent in READY. */
46
- guildIds = new Set();
47
- #token;
48
- #intents;
49
- #devGuildId;
50
- #dev;
51
- #presence;
52
- /** Last presence Discord received, to skip identical updates. */
53
- #sentPresence = null;
54
- #lastPresenceUpdate = 0;
55
- #presenceTimeout = null;
56
- #ws = null;
57
- #gatewayUrl = '';
58
- #resumeGatewayUrl = null;
59
- #sessionId = null;
60
- #sequence = null;
61
- #heartbeatTimeout = null;
62
- #heartbeatInterval = null;
63
- #heartbeatAcked = true;
64
- #destroyed = false;
65
- constructor(options) {
66
- super();
67
- this.#token = options.token;
68
- this.#intents = options.intents ?? GatewayIntents.Guilds;
69
- this.#devGuildId = options.devGuildId ?? null;
70
- this.#dev = options.dev ?? false;
71
- this.#presence = options.presence
72
- ? toGatewayPresence(options.presence)
73
- : null;
74
- this.rest = new RestClient(this.#token);
75
- this.client = createClient(this);
76
- }
77
- /** Connects to the gateway and resolves once the bot is ready. */
78
- async login() {
79
- // https://docs.discord.com/developers/events/gateway#get-gateway-bot
80
- const { url } = await this.rest.get('/gateway/bot');
81
- this.#gatewayUrl = url;
82
- return new Promise((resolve, reject) => {
83
- const onReady = (user) => {
84
- this.off('fatal', onFatal);
85
- resolve(user);
86
- };
87
- const onFatal = (error) => {
88
- this.off('ready', onReady);
89
- reject(error);
90
- };
91
- this.once('ready', onReady);
92
- this.once('fatal', onFatal);
93
- this.#connect(url);
94
- });
95
- }
96
- /** Bitfield of the gateway intents of the current session. */
97
- get intents() {
98
- return this.#intents;
99
- }
100
- /**
101
- * Changes the gateway intents. Intents are only sent when identifying, so a
102
- * connected client starts a new session (READY is dispatched again).
103
- */
104
- setIntents(intents) {
105
- if (intents === this.#intents)
106
- return;
107
- this.#intents = intents;
108
- if (this.#ws)
109
- this.#reconnect(false);
110
- }
111
- /** Updates the bot's status and activity, e.g. `{ activity: { type: 'playing', name: 'Minecraft' } }`. */
112
- setPresence(presence) {
113
- this.#presence = toGatewayPresence(presence);
114
- this.#flushPresence();
115
- }
116
- /** Sends the latest presence, at most once every `PRESENCE_UPDATE_INTERVAL`. */
117
- #flushPresence() {
118
- if (!this.#sessionId || this.#presenceTimeout)
119
- return;
120
- const wait = this.#lastPresenceUpdate + PRESENCE_UPDATE_INTERVAL - Date.now();
121
- if (wait > 0) {
122
- this.#presenceTimeout = setTimeout(() => {
123
- this.#presenceTimeout = null;
124
- this.#flushPresence();
125
- }, wait);
126
- return;
127
- }
128
- const presence = JSON.stringify(this.#presence);
129
- if (presence === this.#sentPresence)
130
- return;
131
- if (this.#send(GatewayOpcodes.PresenceUpdate, this.#presence)) {
132
- this.#sentPresence = presence;
133
- this.#lastPresenceUpdate = Date.now();
134
- this.emit('presenceUpdate', this.#presence);
135
- }
136
- }
137
- /**
138
- * Closes the connection. Resolves once Discord acknowledged the close, so the
139
- * session ends right away instead of lingering online until it times out.
140
- */
141
- destroy() {
142
- this.#destroyed = true;
143
- this.#stopHeartbeat();
144
- if (this.#presenceTimeout)
145
- clearTimeout(this.#presenceTimeout);
146
- const ws = this.#ws;
147
- this.#ws = null;
148
- if (!ws || ws.readyState === WebSocket.CLOSED)
149
- return Promise.resolve();
150
- return new Promise(resolve => {
151
- const timeout = setTimeout(resolve, 1000);
152
- ws.addEventListener('close', () => {
153
- clearTimeout(timeout);
154
- resolve();
155
- });
156
- ws.close(1000);
157
- });
158
- }
159
- #connect(baseUrl) {
160
- const ws = new WebSocket(`${baseUrl}/?v=${API_VERSION}&encoding=json`);
161
- this.#ws = ws;
162
- ws.addEventListener('message', event => {
163
- if (ws !== this.#ws)
164
- return;
165
- this.#onPayload(JSON.parse(String(event.data)));
166
- });
167
- ws.addEventListener('close', event => {
168
- if (ws !== this.#ws)
169
- return;
170
- this.#onClose(event.code);
171
- });
172
- }
173
- #send(op, d) {
174
- if (this.#ws?.readyState !== WebSocket.OPEN)
175
- return false;
176
- this.#ws.send(JSON.stringify({ op, d }));
177
- return true;
178
- }
179
- #onPayload(payload) {
180
- if (payload.s !== null)
181
- this.#sequence = payload.s;
182
- switch (payload.op) {
183
- case GatewayOpcodes.Hello:
184
- this.#startHeartbeat(payload.d.heartbeat_interval);
185
- if (this.#sessionId)
186
- this.#resume();
187
- else
188
- this.#identify();
189
- break;
190
- case GatewayOpcodes.Heartbeat:
191
- this.#heartbeat();
192
- break;
193
- case GatewayOpcodes.HeartbeatAck:
194
- this.#heartbeatAcked = true;
195
- break;
196
- case GatewayOpcodes.Reconnect:
197
- this.#reconnect(true);
198
- break;
199
- case GatewayOpcodes.InvalidSession:
200
- // d tells whether the session can be resumed; wait 1-5s before re-identifying
201
- if (payload.d)
202
- this.#reconnect(true);
203
- else
204
- setTimeout(() => this.#reconnect(false), 1000 + Math.random() * 4000);
205
- break;
206
- case GatewayOpcodes.Dispatch:
207
- this.#onDispatch(payload.t, payload.d);
208
- break;
209
- }
210
- }
211
- #onDispatch(event, data) {
212
- if (event === 'READY') {
213
- this.#sessionId = data.session_id;
214
- this.#resumeGatewayUrl = data.resume_gateway_url;
215
- this.user = new User(this.client, data.user);
216
- this.applicationId = data.application.id;
217
- this.guildIds = new Set(data.guilds.map((guild) => guild.id));
218
- this.emit('ready', this.user);
219
- }
220
- else if (event === 'RESUMED') {
221
- // An update may have been dropped while disconnected
222
- this.#flushPresence();
223
- this.emit('resumed');
224
- }
225
- if (this.#shouldDispatch(event, data))
226
- this.emit('dispatch', event, data);
227
- }
228
- #shouldDispatch(event, data) {
229
- if (!this.#devGuildId || SYSTEM_EVENTS.has(event))
230
- return true;
231
- const guildId = GUILD_EVENTS.has(event)
232
- ? data?.id
233
- : data?.guild_id;
234
- // In dev, events without a guild (DMs) are left to the production process
235
- return this.#dev
236
- ? guildId === this.#devGuildId
237
- : guildId !== this.#devGuildId;
238
- }
239
- // https://docs.discord.com/developers/events/gateway#identifying
240
- #identify() {
241
- this.#send(GatewayOpcodes.Identify, {
242
- token: this.#token,
243
- intents: this.#intents,
244
- properties: {
245
- os: process.platform,
246
- browser: 'chapterjs',
247
- device: 'chapterjs',
248
- },
249
- ...(this.#presence && { presence: this.#presence }),
250
- });
251
- this.#sentPresence = JSON.stringify(this.#presence);
252
- }
253
- // https://docs.discord.com/developers/events/gateway#resuming
254
- #resume() {
255
- this.#send(GatewayOpcodes.Resume, {
256
- token: this.#token,
257
- session_id: this.#sessionId,
258
- seq: this.#sequence,
259
- });
260
- }
261
- // https://docs.discord.com/developers/events/gateway#sending-heartbeats
262
- #startHeartbeat(interval) {
263
- this.#stopHeartbeat();
264
- this.#heartbeatAcked = true;
265
- this.#heartbeatTimeout = setTimeout(() => {
266
- this.#heartbeat();
267
- this.#heartbeatInterval = setInterval(() => this.#heartbeat(), interval);
268
- }, interval * Math.random());
269
- }
270
- #stopHeartbeat() {
271
- if (this.#heartbeatTimeout)
272
- clearTimeout(this.#heartbeatTimeout);
273
- if (this.#heartbeatInterval)
274
- clearInterval(this.#heartbeatInterval);
275
- this.#heartbeatTimeout = this.#heartbeatInterval = null;
276
- }
277
- #heartbeat() {
278
- if (!this.#heartbeatAcked) {
279
- // No ACK since the last heartbeat: the connection is zombied
280
- this.#reconnect(true);
281
- return;
282
- }
283
- this.#heartbeatAcked = false;
284
- this.#send(GatewayOpcodes.Heartbeat, this.#sequence);
285
- }
286
- #reconnect(resume) {
287
- if (this.#destroyed)
288
- return;
289
- this.#stopHeartbeat();
290
- if (!resume)
291
- this.#resetSession();
292
- const previous = this.#ws;
293
- this.#ws = null;
294
- // https://docs.discord.com/developers/events/gateway#initiating-a-disconnect
295
- // 1000 ends the session right away, so it doesn't linger next to the new one
296
- previous?.close(resume ? RECONNECT_CLOSE_CODE : 1000);
297
- this.#connect(this.#sessionId && this.#resumeGatewayUrl
298
- ? this.#resumeGatewayUrl
299
- : this.#gatewayUrl);
300
- }
301
- #resetSession() {
302
- this.#sessionId = null;
303
- this.#resumeGatewayUrl = null;
304
- this.#sequence = null;
305
- }
306
- #onClose(code) {
307
- this.#stopHeartbeat();
308
- if (this.#destroyed)
309
- return;
310
- const fatal = FATAL_CLOSE_CODES[code];
311
- if (fatal) {
312
- this.#ws = null;
313
- this.emit('fatal', new GatewayCloseError(`${fatal} (close code ${code})`, code));
314
- return;
315
- }
316
- const resumable = code !== GatewayCloseCodes.InvalidSeq &&
317
- code !== GatewayCloseCodes.SessionTimedOut;
318
- setTimeout(() => this.#reconnect(resumable), 1000);
319
- }
320
- }