seyfert 0.1.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 (345) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +57 -0
  3. package/lib/api/CDN.d.ts +220 -0
  4. package/lib/api/CDN.js +236 -0
  5. package/lib/api/REST.d.ts +127 -0
  6. package/lib/api/REST.js +424 -0
  7. package/lib/api/Router.d.ts +17 -0
  8. package/lib/api/Router.js +63 -0
  9. package/lib/api/Routes/applications.d.ts +46 -0
  10. package/lib/api/Routes/applications.js +2 -0
  11. package/lib/api/Routes/cdn.d.ts +66 -0
  12. package/lib/api/Routes/cdn.js +2 -0
  13. package/lib/api/Routes/channels.d.ts +109 -0
  14. package/lib/api/Routes/channels.js +2 -0
  15. package/lib/api/Routes/gateway.d.ts +11 -0
  16. package/lib/api/Routes/gateway.js +2 -0
  17. package/lib/api/Routes/guilds.d.ts +171 -0
  18. package/lib/api/Routes/guilds.js +2 -0
  19. package/lib/api/Routes/index.d.ts +13 -0
  20. package/lib/api/Routes/index.js +17 -0
  21. package/lib/api/Routes/interactions.d.ts +14 -0
  22. package/lib/api/Routes/interactions.js +2 -0
  23. package/lib/api/Routes/invites.d.ts +9 -0
  24. package/lib/api/Routes/invites.js +2 -0
  25. package/lib/api/Routes/stage-instances.d.ts +13 -0
  26. package/lib/api/Routes/stage-instances.js +2 -0
  27. package/lib/api/Routes/stickers.d.ts +11 -0
  28. package/lib/api/Routes/stickers.js +2 -0
  29. package/lib/api/Routes/users.d.ts +35 -0
  30. package/lib/api/Routes/users.js +2 -0
  31. package/lib/api/Routes/voice.d.ts +10 -0
  32. package/lib/api/Routes/voice.js +2 -0
  33. package/lib/api/Routes/webhooks.d.ts +29 -0
  34. package/lib/api/Routes/webhooks.js +2 -0
  35. package/lib/api/errors/DiscordAPIError.d.ts +51 -0
  36. package/lib/api/errors/DiscordAPIError.js +81 -0
  37. package/lib/api/errors/HTTPError.d.ts +20 -0
  38. package/lib/api/errors/HTTPError.js +28 -0
  39. package/lib/api/errors/RateLimitError.d.ts +19 -0
  40. package/lib/api/errors/RateLimitError.js +37 -0
  41. package/lib/api/handlers/BurstHandler.d.ts +51 -0
  42. package/lib/api/handlers/BurstHandler.js +124 -0
  43. package/lib/api/handlers/SequentialHandler.d.ts +81 -0
  44. package/lib/api/handlers/SequentialHandler.js +365 -0
  45. package/lib/api/handlers/Shared.d.ts +14 -0
  46. package/lib/api/handlers/Shared.js +125 -0
  47. package/lib/api/index.d.ts +5 -0
  48. package/lib/api/index.js +21 -0
  49. package/lib/api/interfaces/Handler.d.ts +21 -0
  50. package/lib/api/interfaces/Handler.js +2 -0
  51. package/lib/api/shared.d.ts +7 -0
  52. package/lib/api/shared.js +27 -0
  53. package/lib/api/utils/constants.d.ts +39 -0
  54. package/lib/api/utils/constants.js +50 -0
  55. package/lib/api/utils/types.d.ts +335 -0
  56. package/lib/api/utils/types.js +14 -0
  57. package/lib/api/utils/utils.d.ts +52 -0
  58. package/lib/api/utils/utils.js +159 -0
  59. package/lib/builders/ActionRow.d.ts +40 -0
  60. package/lib/builders/ActionRow.js +58 -0
  61. package/lib/builders/Attachment.d.ts +106 -0
  62. package/lib/builders/Attachment.js +196 -0
  63. package/lib/builders/Base.d.ts +10 -0
  64. package/lib/builders/Base.js +13 -0
  65. package/lib/builders/Button.d.ts +59 -0
  66. package/lib/builders/Button.js +94 -0
  67. package/lib/builders/MessageEmbed.d.ts +116 -0
  68. package/lib/builders/MessageEmbed.js +159 -0
  69. package/lib/builders/Modal.d.ts +111 -0
  70. package/lib/builders/Modal.js +162 -0
  71. package/lib/builders/SelectMenu.d.ts +201 -0
  72. package/lib/builders/SelectMenu.js +293 -0
  73. package/lib/builders/index.d.ts +12 -0
  74. package/lib/builders/index.js +54 -0
  75. package/lib/builders/types.d.ts +21 -0
  76. package/lib/builders/types.js +2 -0
  77. package/lib/cache/adapters/default.d.ts +24 -0
  78. package/lib/cache/adapters/default.js +118 -0
  79. package/lib/cache/adapters/index.d.ts +4 -0
  80. package/lib/cache/adapters/index.js +20 -0
  81. package/lib/cache/adapters/redis.d.ts +35 -0
  82. package/lib/cache/adapters/redis.js +180 -0
  83. package/lib/cache/adapters/types.d.ts +25 -0
  84. package/lib/cache/adapters/types.js +2 -0
  85. package/lib/cache/adapters/workeradapter.d.ts +24 -0
  86. package/lib/cache/adapters/workeradapter.js +76 -0
  87. package/lib/cache/index.d.ts +97 -0
  88. package/lib/cache/index.js +394 -0
  89. package/lib/cache/resources/channels.d.ts +7 -0
  90. package/lib/cache/resources/channels.js +20 -0
  91. package/lib/cache/resources/default/base.d.ts +25 -0
  92. package/lib/cache/resources/default/base.js +75 -0
  93. package/lib/cache/resources/default/guild-based.d.ts +33 -0
  94. package/lib/cache/resources/default/guild-based.js +91 -0
  95. package/lib/cache/resources/default/guild-related.d.ts +32 -0
  96. package/lib/cache/resources/default/guild-related.js +102 -0
  97. package/lib/cache/resources/emojis.d.ts +7 -0
  98. package/lib/cache/resources/emojis.js +17 -0
  99. package/lib/cache/resources/guilds.d.ts +10 -0
  100. package/lib/cache/resources/guilds.js +117 -0
  101. package/lib/cache/resources/members.d.ts +10 -0
  102. package/lib/cache/resources/members.js +41 -0
  103. package/lib/cache/resources/presence.d.ts +9 -0
  104. package/lib/cache/resources/presence.js +14 -0
  105. package/lib/cache/resources/roles.d.ts +7 -0
  106. package/lib/cache/resources/roles.js +17 -0
  107. package/lib/cache/resources/stage-instances.d.ts +5 -0
  108. package/lib/cache/resources/stage-instances.js +8 -0
  109. package/lib/cache/resources/stickers.d.ts +7 -0
  110. package/lib/cache/resources/stickers.js +17 -0
  111. package/lib/cache/resources/threads.d.ts +7 -0
  112. package/lib/cache/resources/threads.js +17 -0
  113. package/lib/cache/resources/users.d.ts +7 -0
  114. package/lib/cache/resources/users.js +17 -0
  115. package/lib/cache/resources/voice-states.d.ts +9 -0
  116. package/lib/cache/resources/voice-states.js +13 -0
  117. package/lib/client/base.d.ts +244 -0
  118. package/lib/client/base.js +178 -0
  119. package/lib/client/client.d.ts +32 -0
  120. package/lib/client/client.js +156 -0
  121. package/lib/client/httpclient.d.ts +21 -0
  122. package/lib/client/httpclient.js +143 -0
  123. package/lib/client/index.d.ts +4 -0
  124. package/lib/client/index.js +19 -0
  125. package/lib/client/oninteraction.d.ts +4 -0
  126. package/lib/client/oninteraction.js +180 -0
  127. package/lib/client/workerclient.d.ts +28 -0
  128. package/lib/client/workerclient.js +213 -0
  129. package/lib/collection.d.ts +233 -0
  130. package/lib/collection.js +371 -0
  131. package/lib/commands/applications/chat.d.ts +139 -0
  132. package/lib/commands/applications/chat.js +173 -0
  133. package/lib/commands/applications/chatcontext.d.ts +28 -0
  134. package/lib/commands/applications/chatcontext.js +53 -0
  135. package/lib/commands/applications/menu.d.ts +39 -0
  136. package/lib/commands/applications/menu.js +87 -0
  137. package/lib/commands/applications/menucontext.d.ts +25 -0
  138. package/lib/commands/applications/menucontext.js +64 -0
  139. package/lib/commands/applications/options.d.ts +67 -0
  140. package/lib/commands/applications/options.js +44 -0
  141. package/lib/commands/applications/shared.d.ts +35 -0
  142. package/lib/commands/applications/shared.js +2 -0
  143. package/lib/commands/decorators.d.ts +107 -0
  144. package/lib/commands/decorators.js +85 -0
  145. package/lib/commands/handler.d.ts +15 -0
  146. package/lib/commands/handler.js +140 -0
  147. package/lib/commands/index.d.ts +8 -0
  148. package/lib/commands/index.js +24 -0
  149. package/lib/commands/optionresolver.d.ts +49 -0
  150. package/lib/commands/optionresolver.js +146 -0
  151. package/lib/common/bot/watcher.d.ts +53 -0
  152. package/lib/common/bot/watcher.js +104 -0
  153. package/lib/common/index.d.ts +13 -0
  154. package/lib/common/index.js +30 -0
  155. package/lib/common/it/colors.d.ts +274 -0
  156. package/lib/common/it/colors.js +453 -0
  157. package/lib/common/it/constants.d.ts +36 -0
  158. package/lib/common/it/constants.js +40 -0
  159. package/lib/common/it/logger.d.ts +114 -0
  160. package/lib/common/it/logger.js +189 -0
  161. package/lib/common/it/utils.d.ts +107 -0
  162. package/lib/common/it/utils.js +275 -0
  163. package/lib/common/shorters/base.d.ts +5 -0
  164. package/lib/common/shorters/base.js +10 -0
  165. package/lib/common/shorters/channels.d.ts +90 -0
  166. package/lib/common/shorters/channels.js +95 -0
  167. package/lib/common/shorters/guilds.d.ts +389 -0
  168. package/lib/common/shorters/guilds.js +350 -0
  169. package/lib/common/shorters/members.d.ts +115 -0
  170. package/lib/common/shorters/members.js +179 -0
  171. package/lib/common/shorters/messages.d.ts +27 -0
  172. package/lib/common/shorters/messages.js +117 -0
  173. package/lib/common/shorters/roles.d.ts +12 -0
  174. package/lib/common/shorters/roles.js +51 -0
  175. package/lib/common/shorters/templates.d.ts +12 -0
  176. package/lib/common/shorters/templates.js +29 -0
  177. package/lib/common/shorters/users.d.ts +11 -0
  178. package/lib/common/shorters/users.js +42 -0
  179. package/lib/common/shorters/webhook.d.ts +33 -0
  180. package/lib/common/shorters/webhook.js +69 -0
  181. package/lib/common/types/options.d.ts +10 -0
  182. package/lib/common/types/options.js +2 -0
  183. package/lib/common/types/resolvables.d.ts +10 -0
  184. package/lib/common/types/resolvables.js +2 -0
  185. package/lib/common/types/util.d.ts +90 -0
  186. package/lib/common/types/util.js +2 -0
  187. package/lib/common/types/write.d.ts +21 -0
  188. package/lib/common/types/write.js +2 -0
  189. package/lib/components/ActionRow.d.ts +10 -0
  190. package/lib/components/ActionRow.js +13 -0
  191. package/lib/components/ButtonComponent.d.ts +19 -0
  192. package/lib/components/ButtonComponent.js +34 -0
  193. package/lib/components/ChannelSelectMenuComponent.d.ts +6 -0
  194. package/lib/components/ChannelSelectMenuComponent.js +12 -0
  195. package/lib/components/MentionableSelectMenuComponent.d.ts +4 -0
  196. package/lib/components/MentionableSelectMenuComponent.js +7 -0
  197. package/lib/components/RoleSelectMenuComponent.d.ts +4 -0
  198. package/lib/components/RoleSelectMenuComponent.js +7 -0
  199. package/lib/components/StringSelectMenuComponent.d.ts +6 -0
  200. package/lib/components/StringSelectMenuComponent.js +12 -0
  201. package/lib/components/TextInputComponent.d.ts +7 -0
  202. package/lib/components/TextInputComponent.js +14 -0
  203. package/lib/components/UserSelectMenuComponent.d.ts +4 -0
  204. package/lib/components/UserSelectMenuComponent.js +7 -0
  205. package/lib/components/command.d.ts +23 -0
  206. package/lib/components/command.js +15 -0
  207. package/lib/components/handler.d.ts +48 -0
  208. package/lib/components/handler.js +231 -0
  209. package/lib/components/index.d.ts +20 -0
  210. package/lib/components/index.js +55 -0
  211. package/lib/components/listener.d.ts +11 -0
  212. package/lib/components/listener.js +18 -0
  213. package/lib/events/event.d.ts +23 -0
  214. package/lib/events/event.js +2 -0
  215. package/lib/events/handler.d.ts +19 -0
  216. package/lib/events/handler.js +112 -0
  217. package/lib/events/hooks/application_command.d.ts +12 -0
  218. package/lib/events/hooks/application_command.js +8 -0
  219. package/lib/events/hooks/auto_moderation.d.ts +22 -0
  220. package/lib/events/hooks/auto_moderation.js +21 -0
  221. package/lib/events/hooks/channel.d.ts +11 -0
  222. package/lib/events/hooks/channel.js +24 -0
  223. package/lib/events/hooks/custom.d.ts +4 -0
  224. package/lib/events/hooks/custom.js +11 -0
  225. package/lib/events/hooks/dispatch.d.ts +5 -0
  226. package/lib/events/hooks/dispatch.js +12 -0
  227. package/lib/events/hooks/entitlement.d.ts +35 -0
  228. package/lib/events/hooks/entitlement.js +16 -0
  229. package/lib/events/hooks/guild.d.ts +228 -0
  230. package/lib/events/hooks/guild.js +101 -0
  231. package/lib/events/hooks/index.d.ts +23 -0
  232. package/lib/events/hooks/index.js +34 -0
  233. package/lib/events/hooks/integration.d.ts +94 -0
  234. package/lib/events/hooks/integration.js +27 -0
  235. package/lib/events/hooks/interactions.d.ts +4 -0
  236. package/lib/events/hooks/interactions.js +8 -0
  237. package/lib/events/hooks/invite.d.ts +21 -0
  238. package/lib/events/hooks/invite.js +12 -0
  239. package/lib/events/hooks/message.d.ts +69 -0
  240. package/lib/events/hooks/message.js +37 -0
  241. package/lib/events/hooks/presence.d.ts +27 -0
  242. package/lib/events/hooks/presence.js +8 -0
  243. package/lib/events/hooks/stage.d.ts +29 -0
  244. package/lib/events/hooks/stage.js +16 -0
  245. package/lib/events/hooks/thread.d.ts +198 -0
  246. package/lib/events/hooks/thread.js +29 -0
  247. package/lib/events/hooks/typing.d.ts +16 -0
  248. package/lib/events/hooks/typing.js +14 -0
  249. package/lib/events/hooks/user.d.ts +4 -0
  250. package/lib/events/hooks/user.js +8 -0
  251. package/lib/events/hooks/voice.d.ts +37 -0
  252. package/lib/events/hooks/voice.js +18 -0
  253. package/lib/events/hooks/webhook.d.ts +6 -0
  254. package/lib/events/hooks/webhook.js +8 -0
  255. package/lib/events/index.d.ts +2 -0
  256. package/lib/events/index.js +18 -0
  257. package/lib/index.d.ts +79 -0
  258. package/lib/index.js +104 -0
  259. package/lib/langs/handler.d.ts +11 -0
  260. package/lib/langs/handler.js +34 -0
  261. package/lib/langs/index.d.ts +2 -0
  262. package/lib/langs/index.js +18 -0
  263. package/lib/langs/router.d.ts +18 -0
  264. package/lib/langs/router.js +42 -0
  265. package/lib/structures/AnonymousGuild.d.ts +7 -0
  266. package/lib/structures/AnonymousGuild.js +11 -0
  267. package/lib/structures/AutoModerationRule.d.ts +22 -0
  268. package/lib/structures/AutoModerationRule.js +34 -0
  269. package/lib/structures/ClientUser.d.ts +25 -0
  270. package/lib/structures/ClientUser.js +52 -0
  271. package/lib/structures/Guild.d.ts +86 -0
  272. package/lib/structures/Guild.js +66 -0
  273. package/lib/structures/GuildEmoji.d.ts +29 -0
  274. package/lib/structures/GuildEmoji.js +47 -0
  275. package/lib/structures/GuildMember.d.ts +92 -0
  276. package/lib/structures/GuildMember.js +137 -0
  277. package/lib/structures/GuildPreview.d.ts +10 -0
  278. package/lib/structures/GuildPreview.js +14 -0
  279. package/lib/structures/GuildRole.d.ts +21 -0
  280. package/lib/structures/GuildRole.js +32 -0
  281. package/lib/structures/GuildTemplate.d.ts +23 -0
  282. package/lib/structures/GuildTemplate.js +36 -0
  283. package/lib/structures/Interaction.d.ts +152 -0
  284. package/lib/structures/Interaction.js +409 -0
  285. package/lib/structures/Message.d.ts +53 -0
  286. package/lib/structures/Message.js +123 -0
  287. package/lib/structures/Sticker.d.ts +27 -0
  288. package/lib/structures/Sticker.js +44 -0
  289. package/lib/structures/User.d.ts +20 -0
  290. package/lib/structures/User.js +37 -0
  291. package/lib/structures/Webhook.d.ts +42 -0
  292. package/lib/structures/Webhook.js +63 -0
  293. package/lib/structures/channels.d.ts +232 -0
  294. package/lib/structures/channels.js +366 -0
  295. package/lib/structures/extra/Base.d.ts +7 -0
  296. package/lib/structures/extra/Base.js +39 -0
  297. package/lib/structures/extra/BaseComponent.d.ts +9 -0
  298. package/lib/structures/extra/BaseComponent.js +12 -0
  299. package/lib/structures/extra/BaseGuild.d.ts +40 -0
  300. package/lib/structures/extra/BaseGuild.js +71 -0
  301. package/lib/structures/extra/BaseSelectMenuComponent.d.ts +9 -0
  302. package/lib/structures/extra/BaseSelectMenuComponent.js +13 -0
  303. package/lib/structures/extra/BitField.d.ts +14 -0
  304. package/lib/structures/extra/BitField.js +56 -0
  305. package/lib/structures/extra/DiscordBase.d.ts +18 -0
  306. package/lib/structures/extra/DiscordBase.js +28 -0
  307. package/lib/structures/extra/Permissions.d.ts +54 -0
  308. package/lib/structures/extra/Permissions.js +9 -0
  309. package/lib/structures/extra/functions.d.ts +11 -0
  310. package/lib/structures/extra/functions.js +69 -0
  311. package/lib/structures/index.d.ts +15 -0
  312. package/lib/structures/index.js +31 -0
  313. package/lib/websocket/SharedTypes.d.ts +128 -0
  314. package/lib/websocket/SharedTypes.js +2 -0
  315. package/lib/websocket/constants/index.d.ts +26 -0
  316. package/lib/websocket/constants/index.js +31 -0
  317. package/lib/websocket/discord/basesocket.d.ts +12 -0
  318. package/lib/websocket/discord/basesocket.js +37 -0
  319. package/lib/websocket/discord/events/memberUpdate.d.ts +16 -0
  320. package/lib/websocket/discord/events/memberUpdate.js +47 -0
  321. package/lib/websocket/discord/events/presenceUpdate.d.ts +24 -0
  322. package/lib/websocket/discord/events/presenceUpdate.js +46 -0
  323. package/lib/websocket/discord/handlemessage.d.ts +0 -0
  324. package/lib/websocket/discord/handlemessage.js +1 -0
  325. package/lib/websocket/discord/index.d.ts +4 -0
  326. package/lib/websocket/discord/index.js +20 -0
  327. package/lib/websocket/discord/memberUpdate.d.ts +16 -0
  328. package/lib/websocket/discord/memberUpdate.js +47 -0
  329. package/lib/websocket/discord/shard.d.ts +46 -0
  330. package/lib/websocket/discord/shard.js +278 -0
  331. package/lib/websocket/discord/sharder.d.ts +25 -0
  332. package/lib/websocket/discord/sharder.js +153 -0
  333. package/lib/websocket/discord/shared.d.ts +99 -0
  334. package/lib/websocket/discord/shared.js +8 -0
  335. package/lib/websocket/discord/worker.d.ts +43 -0
  336. package/lib/websocket/discord/worker.js +2 -0
  337. package/lib/websocket/discord/workermanager.d.ts +69 -0
  338. package/lib/websocket/discord/workermanager.js +273 -0
  339. package/lib/websocket/index.d.ts +3 -0
  340. package/lib/websocket/index.js +19 -0
  341. package/lib/websocket/structures/index.d.ts +117 -0
  342. package/lib/websocket/structures/index.js +261 -0
  343. package/lib/websocket/structures/timeout.d.ts +20 -0
  344. package/lib/websocket/structures/timeout.js +75 -0
  345. package/package.json +70 -0
@@ -0,0 +1,43 @@
1
+ import type { GatewayDispatchPayload } from '../../common';
2
+ export interface WorkerShardInfo {
3
+ open: boolean;
4
+ shardId: number;
5
+ latency: number;
6
+ resumable: boolean;
7
+ }
8
+ export type WorkerInfo = {
9
+ shards: WorkerShardInfo[];
10
+ workerId: number;
11
+ };
12
+ type CreateWorkerMessage<T extends string, D extends object = {}> = {
13
+ type: T;
14
+ } & D;
15
+ export type WorkerRequestConnect = CreateWorkerMessage<'CONNECT_QUEUE', {
16
+ shardId: number;
17
+ workerId: number;
18
+ }>;
19
+ export type WorkerReceivePayload = CreateWorkerMessage<'RECEIVE_PAYLOAD', {
20
+ shardId: number;
21
+ workerId: number;
22
+ payload: GatewayDispatchPayload;
23
+ }>;
24
+ export type WorkerSendResultPayload = CreateWorkerMessage<'RESULT_PAYLOAD', {
25
+ nonce: string;
26
+ }>;
27
+ export type WorkerSendCacheRequest = CreateWorkerMessage<'CACHE_REQUEST', {
28
+ nonce: string;
29
+ method: 'scan' | 'get' | 'set' | 'patch' | 'values' | 'keys' | 'count' | 'remove' | 'contains' | 'getToRelationship' | 'bulkAddToRelationShip' | 'addToRelationship' | 'removeRelationship' | 'removeToRelationship';
30
+ args: any[];
31
+ workerId: number;
32
+ }>;
33
+ export type WorkerSendShardInfo = CreateWorkerMessage<'SHARD_INFO', WorkerShardInfo & {
34
+ nonce: string;
35
+ }>;
36
+ export type WorkerSendInfo = CreateWorkerMessage<'WORKER_INFO', WorkerInfo & {
37
+ nonce: string;
38
+ }>;
39
+ export type WorkerReady = CreateWorkerMessage<'WORKER_READY', {
40
+ workerId: number;
41
+ }>;
42
+ export type WorkerMessage = WorkerRequestConnect | WorkerReceivePayload | WorkerSendResultPayload | WorkerSendCacheRequest | WorkerSendShardInfo | WorkerSendInfo | WorkerReady;
43
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,69 @@
1
+ /// <reference types="node" />
2
+ import { Worker } from 'node:worker_threads';
3
+ import { type Adapter } from '../../cache';
4
+ import { Logger, type GatewayPresenceUpdateData, type GatewaySendPayload } from '../../common';
5
+ import { ConnectQueue } from '../structures/timeout';
6
+ import { MemberUpdateHandler } from './events/memberUpdate';
7
+ import { PresenceUpdateHandler } from './events/presenceUpdate';
8
+ import type { ShardOptions, WorkerData, WorkerManagerOptions } from './shared';
9
+ import type { WorkerInfo, WorkerMessage, WorkerShardInfo } from './worker';
10
+ export declare class WorkerManager extends Map<number, Worker> {
11
+ options: Required<WorkerManagerOptions>;
12
+ debugger?: Logger;
13
+ connectQueue: ConnectQueue;
14
+ cacheAdapter: Adapter;
15
+ promises: Map<string, (value: any) => void>;
16
+ memberUpdateHandler: MemberUpdateHandler;
17
+ presenceUpdateHandler: PresenceUpdateHandler;
18
+ constructor(options: WorkerManagerOptions);
19
+ setCache(adapter: Adapter): void;
20
+ get remaining(): number;
21
+ get concurrency(): number;
22
+ get totalWorkers(): number;
23
+ get totalShards(): number;
24
+ get shardsPerWorker(): number;
25
+ get workers(): number;
26
+ syncLatency({ shardId, workerId }: {
27
+ shardId?: number;
28
+ workerId?: number;
29
+ }): Promise<number | undefined>;
30
+ calculateShardId(guildId: string): number;
31
+ calculateWorkerId(shardId: number): number;
32
+ prepareSpaces(): number[][];
33
+ prepareWorkers(shards: number[][]): Promise<void>;
34
+ createWorker(workerData: WorkerData): Worker;
35
+ spawn(workerId: number, shardId: number): void;
36
+ handleWorkerMessage(message: WorkerMessage): Promise<void>;
37
+ private generateNonce;
38
+ private generateSendPromise;
39
+ send(data: GatewaySendPayload, shardId: number): Promise<true>;
40
+ getShardInfo(shardId: number): Promise<WorkerShardInfo>;
41
+ getWorkerInfo(workerId: number): Promise<WorkerInfo>;
42
+ start(): Promise<void>;
43
+ }
44
+ type CreateManagerMessage<T extends string, D extends object = {}> = {
45
+ type: T;
46
+ } & D;
47
+ export type ManagerAllowConnect = CreateManagerMessage<'ALLOW_CONNECT', {
48
+ shardId: number;
49
+ presence: GatewayPresenceUpdateData;
50
+ }>;
51
+ export type ManagerSpawnShards = CreateManagerMessage<'SPAWN_SHARDS', Pick<ShardOptions, 'info' | 'properties' | 'compress'>>;
52
+ export type ManagerSendPayload = CreateManagerMessage<'SEND_PAYLOAD', GatewaySendPayload & {
53
+ shardId: number;
54
+ nonce: string;
55
+ }>;
56
+ export type ManagerRequestShardInfo = CreateManagerMessage<'SHARD_INFO', {
57
+ nonce: string;
58
+ shardId: number;
59
+ }>;
60
+ export type ManagerRequestWorkerInfo = CreateManagerMessage<'WORKER_INFO', {
61
+ nonce: string;
62
+ }>;
63
+ export type ManagerSendCacheResult = CreateManagerMessage<'CACHE_RESULT', {
64
+ nonce: string;
65
+ result: any;
66
+ }>;
67
+ export type ManagerSendBotReady = CreateManagerMessage<'BOT_READY'>;
68
+ export type ManagerMessages = ManagerAllowConnect | ManagerSpawnShards | ManagerSendPayload | ManagerRequestShardInfo | ManagerRequestWorkerInfo | ManagerSendCacheResult | ManagerSendBotReady;
69
+ export {};
@@ -0,0 +1,273 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WorkerManager = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const node_worker_threads_1 = require("node:worker_threads");
6
+ const cache_1 = require("../../cache");
7
+ const common_1 = require("../../common");
8
+ const constants_1 = require("../constants");
9
+ const structures_1 = require("../structures");
10
+ const timeout_1 = require("../structures/timeout");
11
+ const memberUpdate_1 = require("./events/memberUpdate");
12
+ const presenceUpdate_1 = require("./events/presenceUpdate");
13
+ class WorkerManager extends Map {
14
+ options;
15
+ debugger;
16
+ connectQueue;
17
+ cacheAdapter;
18
+ promises = new Map();
19
+ memberUpdateHandler = new memberUpdate_1.MemberUpdateHandler();
20
+ presenceUpdateHandler = new presenceUpdate_1.PresenceUpdateHandler();
21
+ constructor(options) {
22
+ super();
23
+ options.totalShards ??= options.info.shards;
24
+ this.options = (0, common_1.MergeOptions)(constants_1.WorkerManagerDefaults, options);
25
+ this.options.workers ??= Math.ceil(this.options.totalShards / this.options.shardsPerWorker);
26
+ this.options.info.shards = options.totalShards;
27
+ options.shardEnd ??= options.totalShards;
28
+ options.shardStart ??= 0;
29
+ this.connectQueue = new timeout_1.ConnectQueue(5.5e3, this.concurrency);
30
+ if (this.options.debug) {
31
+ this.debugger = new common_1.Logger({
32
+ name: '[WorkerManager]',
33
+ });
34
+ }
35
+ if (this.totalShards / this.shardsPerWorker > this.workers) {
36
+ throw new Error(`Cannot create enough shards in the specified workers, minimum: ${Math.ceil(this.totalShards / this.shardsPerWorker)}`);
37
+ }
38
+ this.cacheAdapter = new cache_1.MemoryAdapter();
39
+ }
40
+ setCache(adapter) {
41
+ this.cacheAdapter = adapter;
42
+ }
43
+ get remaining() {
44
+ return this.options.info.session_start_limit.remaining;
45
+ }
46
+ get concurrency() {
47
+ return this.options.info.session_start_limit.max_concurrency;
48
+ }
49
+ get totalWorkers() {
50
+ return this.options.workers;
51
+ }
52
+ get totalShards() {
53
+ return this.options.totalShards;
54
+ }
55
+ get shardsPerWorker() {
56
+ return this.options.shardsPerWorker;
57
+ }
58
+ get workers() {
59
+ return this.options.workers;
60
+ }
61
+ async syncLatency({ shardId, workerId }) {
62
+ if (typeof shardId !== 'number' && typeof workerId !== 'number') {
63
+ return;
64
+ }
65
+ const id = workerId ?? this.calculateWorkerId(shardId);
66
+ if (!this.has(id)) {
67
+ throw new Error(`Worker #${workerId} doesnt exist`);
68
+ }
69
+ const data = await this.getWorkerInfo(id);
70
+ return data.shards.reduce((acc, prv) => acc + prv.latency, 0) / data.shards.length;
71
+ }
72
+ calculateShardId(guildId) {
73
+ return Number((BigInt(guildId) >> 22n) % BigInt(this.options.info.shards ?? 1));
74
+ }
75
+ calculateWorkerId(shardId) {
76
+ const workerId = Math.floor(shardId / this.shardsPerWorker);
77
+ if (workerId >= this.workers) {
78
+ throw new Error('Invalid shardId');
79
+ }
80
+ return workerId;
81
+ }
82
+ prepareSpaces() {
83
+ this.debugger?.info('Preparing buckets');
84
+ const chunks = structures_1.SequentialBucket.chunk(new Array(this.options.shardStart !== undefined && this.options.shardEnd !== undefined
85
+ ? this.options.shardEnd - this.options.shardStart
86
+ : this.options.totalShards), this.options.shardsPerWorker);
87
+ chunks.forEach((shards, index) => {
88
+ for (let i = 0; i < shards.length; i++) {
89
+ const id = i + (index > 0 ? index * this.options.shardsPerWorker : 0) + (this.options.shardStart ?? 0);
90
+ chunks[index][i] = id;
91
+ }
92
+ });
93
+ this.debugger?.info(`${chunks.length} buckets created`);
94
+ return chunks;
95
+ }
96
+ async prepareWorkers(shards) {
97
+ for (let i = 0; i < shards.length; i++) {
98
+ let worker = this.get(i);
99
+ if (!worker) {
100
+ worker = this.createWorker({
101
+ path: this.options.path,
102
+ debug: this.options.debug,
103
+ token: this.options.token,
104
+ shards: shards[i],
105
+ intents: this.options.intents,
106
+ workerId: i,
107
+ });
108
+ this.set(i, worker);
109
+ }
110
+ worker.postMessage({
111
+ type: 'SPAWN_SHARDS',
112
+ compress: this.options.compress ?? false,
113
+ info: this.options.info,
114
+ properties: this.options.properties,
115
+ });
116
+ }
117
+ }
118
+ createWorker(workerData) {
119
+ const worker = new node_worker_threads_1.Worker(workerData.path, { workerData });
120
+ worker.on('message', data => this.handleWorkerMessage(data));
121
+ return worker;
122
+ }
123
+ spawn(workerId, shardId) {
124
+ this.connectQueue.push(() => {
125
+ const worker = this.get(workerId);
126
+ if (!worker) {
127
+ this.debugger?.fatal("Trying spawn with worker doesn't exist");
128
+ return;
129
+ }
130
+ worker.postMessage({
131
+ type: 'ALLOW_CONNECT',
132
+ shardId,
133
+ presence: this.options.presence?.(shardId, workerId),
134
+ });
135
+ });
136
+ }
137
+ async handleWorkerMessage(message) {
138
+ switch (message.type) {
139
+ case 'CONNECT_QUEUE':
140
+ this.spawn(message.workerId, message.shardId);
141
+ break;
142
+ case 'CACHE_REQUEST':
143
+ {
144
+ const worker = this.get(message.workerId);
145
+ if (!worker) {
146
+ throw new Error('Invalid request from unavailable worker');
147
+ }
148
+ // @ts-expect-error
149
+ const result = await this.cacheAdapter[message.method](...message.args);
150
+ worker.postMessage({
151
+ type: 'CACHE_RESULT',
152
+ nonce: message.nonce,
153
+ result,
154
+ });
155
+ }
156
+ break;
157
+ case 'RECEIVE_PAYLOAD':
158
+ {
159
+ switch (message.payload.t) {
160
+ case 'GUILD_MEMBER_UPDATE':
161
+ if (!this.memberUpdateHandler.check(message.payload.d)) {
162
+ return;
163
+ }
164
+ break;
165
+ case 'PRESENCE_UPDATE':
166
+ if (!this.presenceUpdateHandler.check(message.payload.d)) {
167
+ return;
168
+ }
169
+ break;
170
+ }
171
+ this.options.handlePayload(message.shardId, message.workerId, message.payload);
172
+ }
173
+ break;
174
+ case 'RESULT_PAYLOAD':
175
+ {
176
+ const resolve = this.promises.get(message.nonce);
177
+ if (!resolve) {
178
+ return;
179
+ }
180
+ this.promises.delete(message.nonce);
181
+ resolve(true);
182
+ }
183
+ break;
184
+ case 'SHARD_INFO':
185
+ {
186
+ const { nonce, type, ...data } = message;
187
+ const resolve = this.promises.get(nonce);
188
+ if (!resolve) {
189
+ return;
190
+ }
191
+ this.promises.delete(nonce);
192
+ resolve(data);
193
+ }
194
+ break;
195
+ case 'WORKER_INFO':
196
+ {
197
+ const { nonce, type, ...data } = message;
198
+ const resolve = this.promises.get(nonce);
199
+ if (!resolve) {
200
+ return;
201
+ }
202
+ this.promises.delete(nonce);
203
+ resolve(data);
204
+ }
205
+ break;
206
+ case 'WORKER_READY':
207
+ {
208
+ if (message.workerId === [...this.keys()].at(-1)) {
209
+ this.get(this.keys().next().value)?.postMessage({
210
+ type: 'BOT_READY',
211
+ });
212
+ }
213
+ }
214
+ break;
215
+ }
216
+ }
217
+ generateNonce(large = true) {
218
+ const nonce = (0, node_crypto_1.randomUUID)();
219
+ return large ? nonce : nonce.split('-')[0];
220
+ }
221
+ generateSendPromise(nonce, message = 'Timeout') {
222
+ let resolve = (_) => {
223
+ /**/
224
+ };
225
+ const promise = new Promise((res, rej) => {
226
+ resolve = res;
227
+ setTimeout(() => {
228
+ rej(new Error(message));
229
+ }, 3e3);
230
+ });
231
+ this.promises.set(nonce, resolve);
232
+ return promise;
233
+ }
234
+ async send(data, shardId) {
235
+ const workerId = this.calculateWorkerId(shardId);
236
+ const worker = this.get(workerId);
237
+ if (!worker) {
238
+ throw new Error(`Worker #${workerId} doesnt exist`);
239
+ }
240
+ const nonce = this.generateNonce();
241
+ worker.postMessage({
242
+ type: 'SEND_PAYLOAD',
243
+ shardId,
244
+ nonce,
245
+ ...data,
246
+ });
247
+ return this.generateSendPromise(nonce, 'Shard send payload timeout');
248
+ }
249
+ async getShardInfo(shardId) {
250
+ const workerId = this.calculateWorkerId(shardId);
251
+ const worker = this.get(workerId);
252
+ if (!worker) {
253
+ throw new Error(`Worker #${workerId} doesnt exist`);
254
+ }
255
+ const nonce = this.generateNonce(false);
256
+ worker.postMessage({ shardId, nonce, type: 'SHARD_INFO' });
257
+ return this.generateSendPromise(nonce, 'Get shard info timeout');
258
+ }
259
+ async getWorkerInfo(workerId) {
260
+ const worker = this.get(workerId);
261
+ if (!worker) {
262
+ throw new Error(`Worker #${workerId} doesnt exist`);
263
+ }
264
+ const nonce = this.generateNonce();
265
+ worker.postMessage({ nonce, type: 'WORKER_INFO' });
266
+ return this.generateSendPromise(nonce, 'Get worker info timeout');
267
+ }
268
+ async start() {
269
+ const spaces = this.prepareSpaces();
270
+ await this.prepareWorkers(spaces);
271
+ }
272
+ }
273
+ exports.WorkerManager = WorkerManager;
@@ -0,0 +1,3 @@
1
+ export * from './SharedTypes';
2
+ export * from './constants';
3
+ export * from './discord';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./SharedTypes"), exports);
18
+ __exportStar(require("./constants"), exports);
19
+ __exportStar(require("./discord"), exports);
@@ -0,0 +1,117 @@
1
+ /// <reference types="node" />
2
+ import type { Logger } from '../../common';
3
+ /**
4
+ * just any kind of request to queue and resolve later
5
+ */
6
+ export type QueuedRequest = (value: void | Promise<void>) => Promise<unknown> | any;
7
+ /**
8
+ * options of the dynamic bucket
9
+ */
10
+ export interface DynamicBucketOptions {
11
+ limit: number;
12
+ refillInterval: number;
13
+ refillAmount: number;
14
+ debugger?: Logger;
15
+ }
16
+ /**
17
+ * generally useless for interaction based bots
18
+ * ideally this would only be triggered on certain paths
19
+ * example: a huge amount of messages being spammed
20
+ *
21
+ * a dynamic bucket is just a priority queue implemented using linked lists
22
+ * we create an empty bucket for every path
23
+ * dynamically allocating memory improves the final memory footprint
24
+ */
25
+ export declare class DynamicBucket {
26
+ limit: number;
27
+ refillInterval: number;
28
+ refillAmount: number;
29
+ /** The queue of requests to acquire an available request. Mapped by <shardId, resolve()> */
30
+ queue: PriorityQueue<QueuedRequest>;
31
+ /** The amount of requests that have been used up already. */
32
+ used: number;
33
+ /** Whether or not the queue is already processing. */
34
+ processing: boolean;
35
+ /** The timeout id for the timer to reduce the used amount by the refill amount. */
36
+ timeoutId?: NodeJS.Timeout;
37
+ /** The timestamp in milliseconds when the next refill is scheduled. */
38
+ refillsAt?: number;
39
+ debugger?: Logger;
40
+ constructor(options: DynamicBucketOptions);
41
+ get remaining(): number;
42
+ refill(): void;
43
+ /** Begin processing the queue. */
44
+ processQueue(): Promise<void>;
45
+ /** Pauses the execution until the request is available to be made. */
46
+ acquire(priority: number): Promise<void>;
47
+ toString(): string;
48
+ }
49
+ /**
50
+ * abstract node lol
51
+ */
52
+ export interface AbstractNode<T> {
53
+ data: T;
54
+ next: this | null;
55
+ }
56
+ export interface QueuePusher<T> {
57
+ push(data: T): NonNullable<TNode<T>>;
58
+ }
59
+ export interface QueuePusherWithPriority<T> {
60
+ push(data: T, priority: number): NonNullable<PNode<T>>;
61
+ }
62
+ export declare class TNode<T> implements AbstractNode<T> {
63
+ data: T;
64
+ next: this | null;
65
+ constructor(data: T);
66
+ static null<T>(list: AbstractNode<T> | null): list is null;
67
+ }
68
+ export declare class PNode<T> extends TNode<T> {
69
+ priority: number;
70
+ constructor(data: T, priority: number);
71
+ }
72
+ export declare abstract class Queue<T> {
73
+ protected abstract head: AbstractNode<T> | null;
74
+ /**
75
+ * O(1)
76
+ */
77
+ pop(): AbstractNode<T> | null;
78
+ /**
79
+ * O(1)
80
+ */
81
+ peek(): T;
82
+ /**
83
+ * O(n)
84
+ */
85
+ size(): number;
86
+ /**
87
+ * O(1)
88
+ */
89
+ isEmpty(): boolean;
90
+ [Symbol.iterator](): IterableIterator<T>;
91
+ toArray(): T[];
92
+ toString(): string;
93
+ }
94
+ export declare class LinkedList<T> extends Queue<T> implements QueuePusher<T> {
95
+ protected head: TNode<T> | null;
96
+ /**
97
+ * O(1)
98
+ */
99
+ push(data: T): NonNullable<TNode<T>>;
100
+ }
101
+ export declare class PriorityQueue<T> extends Queue<T> implements QueuePusherWithPriority<T> {
102
+ protected head: PNode<T> | null;
103
+ /**
104
+ * O(#priorities)
105
+ */
106
+ push(data: T, priority: number): NonNullable<PNode<T>>;
107
+ }
108
+ export declare class SequentialBucket {
109
+ private connections;
110
+ private capacity;
111
+ private spawnTimeout;
112
+ constructor(maxCapacity: number);
113
+ destroy(): Promise<void>;
114
+ push(promise: QueuedRequest): Promise<void>;
115
+ acquire(promises?: LinkedList<QueuedRequest>): Promise<boolean>;
116
+ static chunk<T>(array: T[], chunks: number): T[][];
117
+ }