seyfert 2.0.0 → 2.1.1-dev-11310514874.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 (265) hide show
  1. package/lib/api/Router.d.ts +2 -2
  2. package/lib/api/Router.js +2 -1
  3. package/lib/api/Routes/applications.d.ts +29 -30
  4. package/lib/api/Routes/cdn.d.ts +6 -1
  5. package/lib/api/Routes/channels.d.ts +44 -45
  6. package/lib/api/Routes/gateway.d.ts +3 -4
  7. package/lib/api/Routes/guilds.d.ts +81 -80
  8. package/lib/api/Routes/index.d.ts +3 -1
  9. package/lib/api/Routes/interactions.d.ts +9 -3
  10. package/lib/api/Routes/invites.d.ts +3 -4
  11. package/lib/api/Routes/skus.d.ts +10 -0
  12. package/lib/api/Routes/skus.js +2 -0
  13. package/lib/api/Routes/soundboard.d.ts +23 -0
  14. package/lib/api/Routes/soundboard.js +2 -0
  15. package/lib/api/Routes/stage-instances.d.ts +5 -6
  16. package/lib/api/Routes/stickers.d.ts +3 -4
  17. package/lib/api/Routes/users.d.ts +11 -12
  18. package/lib/api/Routes/voice.d.ts +2 -3
  19. package/lib/api/Routes/webhooks.d.ts +16 -15
  20. package/lib/api/api.d.ts +17 -11
  21. package/lib/api/api.js +26 -22
  22. package/lib/api/shared.d.ts +2 -2
  23. package/lib/api/utils/constants.d.ts +3 -1
  24. package/lib/api/utils/constants.js +3 -2
  25. package/lib/builders/ActionRow.d.ts +1 -1
  26. package/lib/builders/Attachment.d.ts +3 -3
  27. package/lib/builders/Attachment.js +13 -13
  28. package/lib/builders/Button.d.ts +1 -1
  29. package/lib/builders/Button.js +1 -1
  30. package/lib/builders/Embed.d.ts +1 -1
  31. package/lib/builders/Embed.js +2 -2
  32. package/lib/builders/Modal.d.ts +1 -1
  33. package/lib/builders/Poll.d.ts +1 -1
  34. package/lib/builders/Poll.js +1 -1
  35. package/lib/builders/SelectMenu.d.ts +1 -1
  36. package/lib/builders/SelectMenu.js +1 -1
  37. package/lib/builders/types.d.ts +2 -1
  38. package/lib/cache/adapters/default.js +5 -3
  39. package/lib/cache/adapters/limited.d.ts +1 -2
  40. package/lib/cache/adapters/limited.js +34 -30
  41. package/lib/cache/adapters/workeradapter.js +3 -1
  42. package/lib/cache/index.d.ts +12 -12
  43. package/lib/cache/index.js +48 -39
  44. package/lib/cache/resources/bans.d.ts +2 -2
  45. package/lib/cache/resources/bans.js +1 -1
  46. package/lib/cache/resources/channels.d.ts +1 -1
  47. package/lib/cache/resources/default/guild-related.d.ts +1 -2
  48. package/lib/cache/resources/emojis.d.ts +2 -2
  49. package/lib/cache/resources/emojis.js +1 -1
  50. package/lib/cache/resources/guilds.d.ts +2 -2
  51. package/lib/cache/resources/guilds.js +7 -11
  52. package/lib/cache/resources/members.d.ts +2 -2
  53. package/lib/cache/resources/members.js +1 -1
  54. package/lib/cache/resources/messages.d.ts +2 -2
  55. package/lib/cache/resources/messages.js +3 -3
  56. package/lib/cache/resources/overwrites.d.ts +1 -1
  57. package/lib/cache/resources/roles.d.ts +2 -2
  58. package/lib/cache/resources/roles.js +1 -1
  59. package/lib/cache/resources/stickers.d.ts +2 -2
  60. package/lib/cache/resources/stickers.js +1 -1
  61. package/lib/cache/resources/users.d.ts +2 -2
  62. package/lib/cache/resources/users.js +1 -1
  63. package/lib/cache/resources/voice-states.d.ts +2 -2
  64. package/lib/cache/resources/voice-states.js +1 -1
  65. package/lib/client/base.d.ts +10 -15
  66. package/lib/client/base.js +48 -44
  67. package/lib/client/client.d.ts +4 -4
  68. package/lib/client/client.js +37 -38
  69. package/lib/client/collectors.js +1 -1
  70. package/lib/client/httpclient.d.ts +1 -1
  71. package/lib/client/httpclient.js +2 -2
  72. package/lib/client/transformers.d.ts +37 -34
  73. package/lib/client/transformers.js +66 -63
  74. package/lib/client/workerclient.d.ts +13 -13
  75. package/lib/client/workerclient.js +182 -65
  76. package/lib/collection.d.ts +3 -3
  77. package/lib/commands/applications/chat.d.ts +3 -3
  78. package/lib/commands/applications/chat.js +5 -6
  79. package/lib/commands/applications/chatcontext.d.ts +6 -6
  80. package/lib/commands/applications/chatcontext.js +16 -8
  81. package/lib/commands/applications/entryPoint.d.ts +46 -0
  82. package/lib/commands/applications/entryPoint.js +56 -0
  83. package/lib/commands/applications/entrycontext.d.ts +40 -0
  84. package/lib/commands/applications/entrycontext.js +85 -0
  85. package/lib/commands/applications/menu.d.ts +1 -1
  86. package/lib/commands/applications/menucontext.d.ts +5 -5
  87. package/lib/commands/applications/menucontext.js +6 -6
  88. package/lib/commands/applications/options.d.ts +7 -6
  89. package/lib/commands/applications/shared.d.ts +5 -2
  90. package/lib/commands/basecontext.d.ts +1 -1
  91. package/lib/commands/decorators.d.ts +20 -26
  92. package/lib/commands/decorators.js +8 -4
  93. package/lib/commands/handle.d.ts +13 -12
  94. package/lib/commands/handle.js +133 -65
  95. package/lib/commands/handler.d.ts +9 -7
  96. package/lib/commands/handler.js +27 -18
  97. package/lib/commands/index.d.ts +2 -0
  98. package/lib/commands/index.js +2 -0
  99. package/lib/commands/optionresolver.d.ts +4 -4
  100. package/lib/commands/optionresolver.js +4 -4
  101. package/lib/common/bot/watcher.d.ts +1 -1
  102. package/lib/common/it/formatter.d.ts +21 -21
  103. package/lib/common/it/formatter.js +40 -41
  104. package/lib/common/it/logger.js +1 -1
  105. package/lib/common/it/utils.d.ts +14 -6
  106. package/lib/common/it/utils.js +62 -27
  107. package/lib/common/shorters/application.d.ts +2 -3
  108. package/lib/common/shorters/application.js +3 -3
  109. package/lib/common/shorters/bans.js +1 -1
  110. package/lib/common/shorters/channels.d.ts +3 -3
  111. package/lib/common/shorters/channels.js +3 -3
  112. package/lib/common/shorters/emojis.js +4 -4
  113. package/lib/common/shorters/guilds.d.ts +3 -3
  114. package/lib/common/shorters/guilds.js +5 -9
  115. package/lib/common/shorters/interaction.d.ts +3 -3
  116. package/lib/common/shorters/interaction.js +6 -5
  117. package/lib/common/shorters/members.d.ts +4 -5
  118. package/lib/common/shorters/members.js +6 -7
  119. package/lib/common/shorters/messages.d.ts +1 -1
  120. package/lib/common/shorters/messages.js +4 -4
  121. package/lib/common/shorters/reactions.d.ts +1 -1
  122. package/lib/common/shorters/reactions.js +1 -1
  123. package/lib/common/shorters/roles.js +1 -1
  124. package/lib/common/shorters/templates.js +2 -2
  125. package/lib/common/shorters/threads.d.ts +1 -1
  126. package/lib/common/shorters/threads.js +17 -11
  127. package/lib/common/shorters/webhook.d.ts +1 -1
  128. package/lib/common/shorters/webhook.js +4 -4
  129. package/lib/common/types/options.d.ts +2 -2
  130. package/lib/common/types/resolvables.d.ts +1 -1
  131. package/lib/common/types/util.d.ts +4 -1
  132. package/lib/common/types/write.d.ts +3 -3
  133. package/lib/components/BaseComponent.js +1 -1
  134. package/lib/components/ButtonComponent.d.ts +1 -1
  135. package/lib/components/componentcommand.d.ts +1 -1
  136. package/lib/components/componentcontext.d.ts +4 -4
  137. package/lib/components/componentcontext.js +1 -1
  138. package/lib/components/handler.d.ts +10 -8
  139. package/lib/components/handler.js +16 -13
  140. package/lib/components/index.js +2 -1
  141. package/lib/components/modalcontext.d.ts +2 -2
  142. package/lib/components/modalcontext.js +1 -1
  143. package/lib/deps/mixer.d.ts +1 -1
  144. package/lib/deps/mixer.js +6 -1
  145. package/lib/events/handler.d.ts +5 -5
  146. package/lib/events/handler.js +69 -19
  147. package/lib/events/hooks/application_command.d.ts +1 -1
  148. package/lib/events/hooks/auto_moderation.d.ts +1 -1
  149. package/lib/events/hooks/auto_moderation.js +1 -1
  150. package/lib/events/hooks/channel.d.ts +1 -1
  151. package/lib/events/hooks/custom.d.ts +1 -0
  152. package/lib/events/hooks/custom.js +5 -1
  153. package/lib/events/hooks/dispatch.d.ts +1 -1
  154. package/lib/events/hooks/entitlement.d.ts +4 -5
  155. package/lib/events/hooks/entitlement.js +4 -4
  156. package/lib/events/hooks/guild.d.ts +3 -3
  157. package/lib/events/hooks/guild.js +1 -1
  158. package/lib/events/hooks/index.d.ts +1 -0
  159. package/lib/events/hooks/index.js +1 -0
  160. package/lib/events/hooks/integration.d.ts +1 -1
  161. package/lib/events/hooks/integration.js +1 -1
  162. package/lib/events/hooks/interactions.d.ts +3 -3
  163. package/lib/events/hooks/invite.d.ts +1 -1
  164. package/lib/events/hooks/message.d.ts +3 -3
  165. package/lib/events/hooks/message.js +1 -1
  166. package/lib/events/hooks/presence.d.ts +2 -2
  167. package/lib/events/hooks/soundboard.d.ts +155 -0
  168. package/lib/events/hooks/soundboard.js +28 -0
  169. package/lib/events/hooks/stage.d.ts +2 -2
  170. package/lib/events/hooks/subscriptions.d.ts +35 -0
  171. package/lib/events/hooks/subscriptions.js +16 -0
  172. package/lib/events/hooks/thread.d.ts +2 -2
  173. package/lib/events/hooks/thread.js +2 -2
  174. package/lib/events/hooks/typing.d.ts +1 -1
  175. package/lib/events/hooks/typing.js +1 -1
  176. package/lib/events/hooks/user.d.ts +2 -2
  177. package/lib/events/hooks/voice.d.ts +2 -2
  178. package/lib/events/hooks/voice.js +1 -1
  179. package/lib/events/hooks/webhook.d.ts +1 -1
  180. package/lib/index.d.ts +2 -2
  181. package/lib/index.js +3 -5
  182. package/lib/langs/handler.d.ts +2 -2
  183. package/lib/structures/Guild.d.ts +7 -4
  184. package/lib/structures/Guild.js +1 -1
  185. package/lib/structures/GuildBan.d.ts +1 -1
  186. package/lib/structures/GuildMember.d.ts +7 -5
  187. package/lib/structures/GuildMember.js +5 -3
  188. package/lib/structures/GuildRole.d.ts +1 -1
  189. package/lib/structures/GuildTemplate.d.ts +1 -1
  190. package/lib/structures/Interaction.d.ts +39 -16
  191. package/lib/structures/Interaction.js +82 -26
  192. package/lib/structures/Message.d.ts +4 -10
  193. package/lib/structures/Message.js +5 -8
  194. package/lib/structures/Poll.d.ts +3 -3
  195. package/lib/structures/Sticker.d.ts +2 -2
  196. package/lib/structures/Sticker.js +1 -1
  197. package/lib/structures/VoiceState.d.ts +2 -1
  198. package/lib/structures/VoiceState.js +1 -1
  199. package/lib/structures/Webhook.d.ts +3 -3
  200. package/lib/structures/Webhook.js +1 -1
  201. package/lib/structures/channels.d.ts +4 -4
  202. package/lib/structures/channels.js +8 -7
  203. package/lib/structures/extra/BaseGuild.d.ts +11 -1
  204. package/lib/structures/extra/BaseGuild.js +27 -0
  205. package/lib/structures/extra/BitField.d.ts +3 -3
  206. package/lib/structures/extra/BitField.js +34 -33
  207. package/lib/structures/extra/DiscordBase.js +1 -1
  208. package/lib/structures/extra/Permissions.d.ts +2 -0
  209. package/lib/structures/extra/Permissions.js +21 -0
  210. package/lib/types/gateway.d.ts +71 -6
  211. package/lib/types/payloads/_interactions/_applicationCommands/chatInput.d.ts +6 -1
  212. package/lib/types/payloads/_interactions/applicationCommands.d.ts +41 -6
  213. package/lib/types/payloads/_interactions/applicationCommands.js +28 -1
  214. package/lib/types/payloads/_interactions/base.d.ts +1 -1
  215. package/lib/types/payloads/_interactions/responses.d.ts +74 -2
  216. package/lib/types/payloads/_interactions/responses.js +4 -0
  217. package/lib/types/payloads/channel.d.ts +46 -6
  218. package/lib/types/payloads/channel.js +5 -1
  219. package/lib/types/payloads/guild.d.ts +9 -1
  220. package/lib/types/payloads/guild.js +8 -0
  221. package/lib/types/payloads/index.d.ts +1 -0
  222. package/lib/types/payloads/index.js +1 -0
  223. package/lib/types/payloads/monetization.d.ts +29 -1
  224. package/lib/types/payloads/monetization.js +10 -1
  225. package/lib/types/payloads/soundboard.d.ts +23 -0
  226. package/lib/types/payloads/soundboard.js +5 -0
  227. package/lib/types/payloads/user.js +0 -1
  228. package/lib/types/rest/application.d.ts +3 -3
  229. package/lib/types/rest/auditLog.d.ts +1 -1
  230. package/lib/types/rest/channel.d.ts +5 -18
  231. package/lib/types/rest/guild.d.ts +2 -2
  232. package/lib/types/rest/guildScheduledEvent.d.ts +1 -1
  233. package/lib/types/rest/index.d.ts +1 -0
  234. package/lib/types/rest/index.js +1 -0
  235. package/lib/types/rest/interactions.d.ts +30 -8
  236. package/lib/types/rest/monetization.d.ts +22 -1
  237. package/lib/types/rest/oauth2.d.ts +1 -1
  238. package/lib/types/rest/poll.d.ts +1 -1
  239. package/lib/types/rest/soundboard.d.ts +64 -0
  240. package/lib/types/rest/soundboard.js +2 -0
  241. package/lib/types/rest/stageInstance.d.ts +1 -1
  242. package/lib/types/rest/template.d.ts +1 -1
  243. package/lib/types/rest/user.d.ts +1 -1
  244. package/lib/types/rest/webhook.d.ts +3 -3
  245. package/lib/types/utils/index.d.ts +27 -7
  246. package/lib/types/utils/index.js +25 -6
  247. package/lib/websocket/SharedTypes.d.ts +18 -3
  248. package/lib/websocket/constants/index.d.ts +3 -2
  249. package/lib/websocket/constants/index.js +4 -4
  250. package/lib/websocket/discord/basesocket.d.ts +1 -1
  251. package/lib/websocket/discord/shard.d.ts +4 -3
  252. package/lib/websocket/discord/shard.js +47 -24
  253. package/lib/websocket/discord/sharder.d.ts +9 -6
  254. package/lib/websocket/discord/sharder.js +92 -17
  255. package/lib/websocket/discord/shared.d.ts +12 -3
  256. package/lib/websocket/discord/socket/custom.d.ts +1 -1
  257. package/lib/websocket/discord/socket/custom.js +70 -40
  258. package/lib/websocket/discord/worker.d.ts +9 -2
  259. package/lib/websocket/discord/workermanager.d.ts +42 -16
  260. package/lib/websocket/discord/workermanager.js +170 -46
  261. package/lib/websocket/structures/timeout.d.ts +4 -5
  262. package/lib/websocket/structures/timeout.js +23 -24
  263. package/package.json +72 -65
  264. package/lib/cache/resources/threads.d.ts +0 -14
  265. package/lib/cache/resources/threads.js +0 -32
@@ -14,12 +14,27 @@ const constants_1 = require("../constants");
14
14
  const structures_1 = require("../structures");
15
15
  const timeout_1 = require("../structures/timeout");
16
16
  class WorkerManager extends Map {
17
+ static prepareSpaces(options, logger) {
18
+ logger?.info('Preparing buckets', options);
19
+ const chunks = structures_1.DynamicBucket.chunk(new Array(options.shardEnd - options.shardStart), options.shardsPerWorker);
20
+ chunks.forEach((shards, index) => {
21
+ for (let i = 0; i < shards.length; i++) {
22
+ const id = i + (index > 0 ? index * options.shardsPerWorker : 0) + options.shardStart;
23
+ chunks[index][i] = id;
24
+ }
25
+ });
26
+ logger?.info(`${chunks.length} buckets created`);
27
+ return chunks;
28
+ }
17
29
  options;
18
30
  debugger;
19
31
  connectQueue;
32
+ workerQueue = [];
20
33
  cacheAdapter;
21
34
  promises = new Map();
22
35
  rest;
36
+ reshardingWorkerQueue = [];
37
+ _info;
23
38
  constructor(options) {
24
39
  super();
25
40
  this.options = options;
@@ -52,12 +67,9 @@ class WorkerManager extends Map {
52
67
  get shardsPerWorker() {
53
68
  return this.options.shardsPerWorker;
54
69
  }
55
- get workers() {
56
- return this.options.workers;
57
- }
58
- async syncLatency({ shardId, workerId }) {
70
+ async syncLatency({ shardId, workerId, }) {
59
71
  if (typeof shardId !== 'number' && typeof workerId !== 'number') {
60
- return;
72
+ throw new Error('Undefined workerId and shardId');
61
73
  }
62
74
  const id = workerId ?? this.calculateWorkerId(shardId);
63
75
  if (!this.has(id)) {
@@ -71,27 +83,15 @@ class WorkerManager extends Map {
71
83
  }
72
84
  calculateWorkerId(shardId) {
73
85
  const workerId = Math.floor((shardId - this.shardStart) / this.shardsPerWorker);
74
- if (workerId >= this.workers) {
86
+ if (workerId >= this.totalWorkers) {
75
87
  throw new Error('Invalid shardId');
76
88
  }
77
89
  return workerId;
78
90
  }
79
- prepareSpaces() {
80
- this.debugger?.info('Preparing buckets');
81
- const chunks = structures_1.DynamicBucket.chunk(new Array(this.shardEnd - this.shardStart), this.options.shardsPerWorker);
82
- chunks.forEach((shards, index) => {
83
- for (let i = 0; i < shards.length; i++) {
84
- const id = i + (index > 0 ? index * this.options.shardsPerWorker : 0) + this.shardStart;
85
- chunks[index][i] = id;
86
- }
87
- });
88
- this.debugger?.info(`${chunks.length} buckets created`);
89
- return chunks;
90
- }
91
91
  postMessage(id, body) {
92
92
  const worker = this.get(id);
93
93
  if (!worker)
94
- return this.debugger?.error(`Worker ${id} doesnt exists.`);
94
+ return this.debugger?.error(`Worker ${id} does not exists.`);
95
95
  switch (this.options.mode) {
96
96
  case 'clusters':
97
97
  worker.send(body);
@@ -104,35 +104,49 @@ class WorkerManager extends Map {
104
104
  break;
105
105
  }
106
106
  }
107
- async prepareWorkers(shards) {
107
+ async prepareWorkers(shards, resharding = false) {
108
108
  const worker_threads = (0, common_1.lazyLoadPackage)('node:worker_threads');
109
109
  if (!worker_threads)
110
110
  throw new Error('Cannot prepare workers without worker_threads.');
111
111
  for (let i = 0; i < shards.length; i++) {
112
- let worker = this.get(i);
113
- if (!worker) {
114
- worker = this.createWorker({
115
- path: this.options.path,
116
- debug: this.options.debug,
117
- token: this.options.token,
118
- shards: shards[i],
119
- intents: this.options.intents,
120
- workerId: i,
121
- workerProxy: this.options.workerProxy,
122
- totalShards: this.totalShards,
123
- mode: this.options.mode,
112
+ const workerExists = this.has(i);
113
+ if (resharding || !workerExists) {
114
+ this[resharding ? 'reshardingWorkerQueue' : 'workerQueue'].push(() => {
115
+ const worker = this.createWorker({
116
+ path: this.options.path,
117
+ debug: this.options.debug,
118
+ token: this.options.token,
119
+ shards: shards[i],
120
+ intents: this.options.intents,
121
+ workerId: i,
122
+ workerProxy: this.options.workerProxy,
123
+ totalShards: resharding ? this._info.shards : this.totalShards,
124
+ mode: this.options.mode,
125
+ resharding,
126
+ });
127
+ this.set(i, worker);
124
128
  });
125
- this.set(i, worker);
126
129
  }
127
130
  }
128
131
  }
129
132
  createWorker(workerData) {
133
+ if (this.has(workerData.workerId)) {
134
+ if (workerData.resharding) {
135
+ this.postMessage(workerData.workerId, {
136
+ type: 'WORKER_ALREADY_EXISTS_RESHARDING',
137
+ });
138
+ }
139
+ const worker = this.get(workerData.workerId);
140
+ return worker;
141
+ }
130
142
  const worker_threads = (0, common_1.lazyLoadPackage)('node:worker_threads');
131
143
  if (!worker_threads)
132
144
  throw new Error('Cannot create worker without worker_threads.');
133
145
  const env = {
134
146
  SEYFERT_SPAWNING: 'true',
135
147
  };
148
+ if (workerData.resharding)
149
+ env.SEYFERT_WORKER_RESHARDING = 'true';
136
150
  for (const i in workerData) {
137
151
  env[`SEYFERT_WORKER_${i.toUpperCase()}`] = workerData[i];
138
152
  }
@@ -152,22 +166,23 @@ class WorkerManager extends Map {
152
166
  worker.on('message', data => this.handleWorkerMessage(data));
153
167
  return worker;
154
168
  }
155
- case 'custom':
169
+ case 'custom': {
156
170
  this.options.adapter.spawn(workerData, env);
157
171
  return {
158
172
  ready: false,
159
173
  };
174
+ }
160
175
  }
161
176
  }
162
- spawn(workerId, shardId) {
177
+ spawn(workerId, shardId, resharding = false) {
163
178
  this.connectQueue.push(() => {
164
179
  const worker = this.has(workerId);
165
180
  if (!worker) {
166
- this.debugger?.fatal("Trying spawn with worker doesn't exist");
181
+ this.debugger?.fatal(`Trying ${resharding ? 'reshard' : 'spawn'} with worker that doesn't exist`);
167
182
  return;
168
183
  }
169
184
  this.postMessage(workerId, {
170
- type: 'ALLOW_CONNECT',
185
+ type: resharding ? 'ALLOW_CONNECT_RESHARDING' : 'ALLOW_CONNECT',
171
186
  shardId,
172
187
  presence: this.options.presence?.(shardId, workerId),
173
188
  });
@@ -175,6 +190,66 @@ class WorkerManager extends Map {
175
190
  }
176
191
  async handleWorkerMessage(message) {
177
192
  switch (message.type) {
193
+ case 'WORKER_READY_RESHARDING':
194
+ {
195
+ this.get(message.workerId).resharded = true;
196
+ if (!this.reshardingWorkerQueue.length && [...this.values()].every(w => w.resharded)) {
197
+ for (const [id] of this.entries()) {
198
+ this.postMessage(id, {
199
+ type: 'DISCONNECT_ALL_SHARDS_RESHARDING',
200
+ });
201
+ }
202
+ this.forEach(w => {
203
+ delete w.resharded;
204
+ });
205
+ }
206
+ else {
207
+ const nextWorker = this.reshardingWorkerQueue.shift();
208
+ if (nextWorker) {
209
+ this.debugger?.info('Spawning next worker to reshard');
210
+ nextWorker();
211
+ }
212
+ else {
213
+ this.debugger?.info('No more workers to reshard left');
214
+ }
215
+ }
216
+ }
217
+ break;
218
+ case 'DISCONNECTED_ALL_SHARDS_RESHARDING':
219
+ {
220
+ this.get(message.workerId).disconnected = true;
221
+ if ([...this.values()].every(w => w.disconnected)) {
222
+ this.options.totalShards = this._info.shards;
223
+ this.options.shardEnd = this.options.totalShards = this._info.shards;
224
+ this.options.workers = this.size;
225
+ delete this._info;
226
+ for (const [id] of this.entries()) {
227
+ this.postMessage(id, {
228
+ type: 'CONNECT_ALL_SHARDS_RESHARDING',
229
+ });
230
+ }
231
+ this.forEach(w => {
232
+ delete w.disconnected;
233
+ });
234
+ }
235
+ }
236
+ break;
237
+ case 'WORKER_START_RESHARDING':
238
+ {
239
+ this.postMessage(message.workerId, {
240
+ type: 'SPAWN_SHARDS_RESHARDING',
241
+ compress: this.options.compress ?? false,
242
+ info: {
243
+ ...this.options.info,
244
+ shards: this._info.shards,
245
+ },
246
+ properties: {
247
+ ...constants_1.properties,
248
+ ...this.options.properties,
249
+ },
250
+ });
251
+ }
252
+ break;
178
253
  case 'WORKER_START':
179
254
  {
180
255
  this.postMessage(message.workerId, {
@@ -191,6 +266,9 @@ class WorkerManager extends Map {
191
266
  });
192
267
  }
193
268
  break;
269
+ case 'CONNECT_QUEUE_RESHARDING':
270
+ this.spawn(message.workerId, message.shardId, true);
271
+ break;
194
272
  case 'CONNECT_QUEUE':
195
273
  this.spawn(message.workerId, message.shardId);
196
274
  break;
@@ -260,6 +338,18 @@ class WorkerManager extends Map {
260
338
  }
261
339
  }
262
340
  break;
341
+ case 'WORKER_SHARDS_CONNECTED':
342
+ {
343
+ const nextWorker = this.workerQueue.shift();
344
+ if (nextWorker) {
345
+ this.debugger?.info('Spawning next worker');
346
+ nextWorker();
347
+ }
348
+ else {
349
+ this.debugger?.info('No more workers to spawn left');
350
+ }
351
+ }
352
+ break;
263
353
  case 'WORKER_API_REQUEST':
264
354
  {
265
355
  const response = await this.rest.request(message.method, message.url, message.requestOptions);
@@ -300,12 +390,11 @@ class WorkerManager extends Map {
300
390
  break;
301
391
  }
302
392
  }
303
- generateNonce(large = true) {
393
+ generateNonce() {
304
394
  const uuid = (0, node_crypto_1.randomUUID)();
305
- const nonce = large ? uuid : uuid.split('-')[0];
306
- if (this.promises.has(nonce))
307
- return this.generateNonce(large);
308
- return nonce;
395
+ if (this.promises.has(uuid))
396
+ return this.generateNonce();
397
+ return uuid;
309
398
  }
310
399
  generateSendPromise(nonce, message = 'Timeout') {
311
400
  return new Promise((res, rej) => {
@@ -337,7 +426,7 @@ class WorkerManager extends Map {
337
426
  if (!worker) {
338
427
  throw new Error(`Worker #${workerId} doesnt exist`);
339
428
  }
340
- const nonce = this.generateNonce(false);
429
+ const nonce = this.generateNonce();
341
430
  this.postMessage(workerId, { shardId, nonce, type: 'SHARD_INFO' });
342
431
  return this.generateSendPromise(nonce, 'Get shard info timeout');
343
432
  }
@@ -361,10 +450,11 @@ class WorkerManager extends Map {
361
450
  domain: 'https://discord.com',
362
451
  debug: this.options.debug,
363
452
  });
364
- this.options.info ??= await new __1.Router(this.rest).createProxy().gateway.bot.get();
453
+ this.options.info ??= await this.rest.proxy.gateway.bot.get();
365
454
  this.options.shardEnd ??= this.options.totalShards ?? this.options.info.shards;
366
455
  this.options.totalShards ??= this.options.shardEnd;
367
456
  this.options = (0, common_1.MergeOptions)(constants_1.WorkerManagerDefaults, this.options);
457
+ this.options.resharding.getInfo ??= () => this.rest.proxy.gateway.bot.get();
368
458
  this.options.workers ??= Math.ceil(this.options.totalShards / this.options.shardsPerWorker);
369
459
  this.connectQueue = new timeout_1.ConnectQueue(5.5e3, this.concurrency);
370
460
  if (this.options.debug) {
@@ -372,11 +462,45 @@ class WorkerManager extends Map {
372
462
  name: '[WorkerManager]',
373
463
  });
374
464
  }
375
- if (this.totalShards / this.shardsPerWorker > this.workers) {
465
+ if (this.totalShards / this.shardsPerWorker > this.totalWorkers) {
376
466
  throw new Error(`Cannot create enough shards in the specified workers, minimum: ${Math.ceil(this.totalShards / this.shardsPerWorker)}`);
377
467
  }
378
- const spaces = this.prepareSpaces();
468
+ const spaces = WorkerManager.prepareSpaces({
469
+ shardStart: this.shardStart,
470
+ shardEnd: this.shardEnd,
471
+ shardsPerWorker: this.shardsPerWorker,
472
+ }, this.debugger);
379
473
  await this.prepareWorkers(spaces);
474
+ // Start workers queue
475
+ this.workerQueue.shift()();
476
+ await this.startResharding();
477
+ }
478
+ async startResharding() {
479
+ if (this.options.resharding.interval <= 0)
480
+ return;
481
+ if (this.shardStart !== 0 || this.shardEnd !== this.totalShards)
482
+ return this.debugger?.debug('Cannot start resharder');
483
+ setInterval(async () => {
484
+ this.debugger?.debug('Checking if reshard is needed');
485
+ const info = await this.options.resharding.getInfo();
486
+ if (info.shards <= this.totalShards)
487
+ return this.debugger?.debug('Resharding not needed');
488
+ //https://github.com/discordeno/discordeno/blob/6a5f446c0651b9fad9f1550ff1857fe7a026426b/packages/gateway/src/manager.ts#L106C8-L106C94
489
+ const percentage = (info.shards / ((this.totalShards * 2500) / 1000)) * 100;
490
+ if (percentage < this.options.resharding.percentage)
491
+ return this.debugger?.debug(`Percentage is not enough to reshard ${percentage}/${this.options.resharding.percentage}`);
492
+ this.debugger?.info('Starting resharding process');
493
+ this._info = info;
494
+ this.connectQueue.concurrency = info.session_start_limit.max_concurrency;
495
+ this.options.info.session_start_limit.max_concurrency = info.session_start_limit.max_concurrency;
496
+ const spaces = WorkerManager.prepareSpaces({
497
+ shardsPerWorker: this.shardsPerWorker,
498
+ shardEnd: info.shards,
499
+ shardStart: 0,
500
+ }, this.debugger);
501
+ await this.prepareWorkers(spaces, true);
502
+ return this.reshardingWorkerQueue.shift()();
503
+ }, this.options.resharding.interval);
380
504
  }
381
505
  }
382
506
  exports.WorkerManager = WorkerManager;
@@ -9,11 +9,10 @@ export declare class ConnectTimeout {
9
9
  export declare class ConnectQueue {
10
10
  intervalTime: number;
11
11
  concurrency: number;
12
- readonly queue: {
13
- cb: (() => any) | undefined;
14
- }[];
12
+ private queue;
13
+ private remaining;
15
14
  protected interval?: NodeJS.Timeout;
16
15
  constructor(intervalTime?: number, concurrency?: number);
17
- push(callback: () => any): Promise<void>;
18
- shift(): Promise<any>;
16
+ push(callback: () => unknown): unknown;
17
+ startInterval(): void;
19
18
  }
@@ -32,41 +32,40 @@ class ConnectQueue {
32
32
  intervalTime;
33
33
  concurrency;
34
34
  queue = [];
35
+ remaining = 0;
35
36
  interval = undefined;
36
37
  constructor(intervalTime = 5000, concurrency = 1) {
37
38
  this.intervalTime = intervalTime;
38
39
  this.concurrency = concurrency;
40
+ this.remaining = concurrency;
39
41
  }
40
- async push(callback) {
41
- this.queue.push({ cb: callback });
42
- if (this.queue.length === this.concurrency) {
43
- for (let i = 0; i < this.concurrency; i++) {
44
- await this.queue[i].cb?.();
45
- this.queue[i].cb = undefined;
46
- }
47
- this.interval = setInterval(() => {
48
- for (let i = 0; i < this.concurrency; i++) {
49
- this.shift();
50
- }
51
- }, this.intervalTime);
42
+ push(callback) {
43
+ if (this.remaining === 0)
44
+ return this.queue.push(callback);
45
+ this.remaining--;
46
+ if (!this.interval) {
47
+ this.startInterval();
52
48
  }
49
+ if (this.queue.length < this.concurrency) {
50
+ return callback();
51
+ }
52
+ return this.queue.push(callback);
53
53
  }
54
- async shift() {
55
- const shift = this.queue.shift();
56
- if (!shift) {
54
+ startInterval() {
55
+ this.interval = setInterval(() => {
56
+ let cb;
57
+ while (this.queue.length && !(cb = this.queue.shift())) {
58
+ //
59
+ }
60
+ if (cb)
61
+ return cb?.();
62
+ if (this.remaining < this.concurrency)
63
+ return this.remaining++;
57
64
  if (!this.queue.length) {
58
65
  clearInterval(this.interval);
59
66
  this.interval = undefined;
60
67
  }
61
- return;
62
- }
63
- if (!shift.cb)
64
- return this.shift();
65
- await shift.cb?.();
66
- if (!this.queue.length) {
67
- clearInterval(this.interval);
68
- this.interval = undefined;
69
- }
68
+ }, this.intervalTime / this.concurrency);
70
69
  }
71
70
  }
72
71
  exports.ConnectQueue = ConnectQueue;
package/package.json CHANGED
@@ -1,65 +1,72 @@
1
- {
2
- "name": "seyfert",
3
- "version": "2.0.0",
4
- "description": "The most advanced framework for discord bots",
5
- "main": "./lib/index.js",
6
- "module": "./lib/index.js",
7
- "types": "./lib/index.d.ts",
8
- "files": [
9
- "lib/**"
10
- ],
11
- "scripts": {
12
- "build": "tsc --outDir ./lib",
13
- "prepublishOnly": "npm run build",
14
- "prepare": "npm run build && husky",
15
- "lint": "biome lint --write ./src",
16
- "format": "biome format --write ./src",
17
- "check-h": "biome check --write ./src",
18
- "check": "biome check --write --no-errors-on-unmatched ./src"
19
- },
20
- "author": "MARCROCK22",
21
- "license": "Apache-2.0",
22
- "devDependencies": {
23
- "@biomejs/biome": "1.8.3",
24
- "@commitlint/cli": "^19.4.0",
25
- "@commitlint/config-conventional": "^19.2.2",
26
- "@types/node": "^22.5.0",
27
- "husky": "^9.1.5",
28
- "lint-staged": "^15.2.9",
29
- "typescript": "^5.5.4"
30
- },
31
- "homepage": "https://seyfert.dev",
32
- "repository": {
33
- "type": "git",
34
- "url": "git+https://github.com/tiramisulabs/seyfert.git"
35
- },
36
- "bugs": {
37
- "url": "https://github.com/tiramisulabs/seyfert"
38
- },
39
- "keywords": [
40
- "api",
41
- "discord",
42
- "bots",
43
- "typescript",
44
- "botdev"
45
- ],
46
- "publishConfig": {
47
- "access": "public"
48
- },
49
- "maintainers": [
50
- {
51
- "name": "socram03",
52
- "url": "https://github.com/socram03"
53
- }
54
- ],
55
- "contributors": [
56
- {
57
- "name": "Free 公園",
58
- "url": "https://github.com/FreeAoi"
59
- },
60
- {
61
- "name": "David",
62
- "url": "https://github.com/Drylozu"
63
- }
64
- ]
65
- }
1
+ {
2
+ "name": "seyfert",
3
+ "version": "2.1.1-dev-11310514874.0",
4
+ "description": "The most advanced framework for discord bots",
5
+ "main": "./lib/index.js",
6
+ "module": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "files": [
9
+ "lib/**"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc --outDir ./lib",
13
+ "prepublishOnly": "npm run build",
14
+ "prepare": "npm run build && husky",
15
+ "lint": "biome lint --write ./src",
16
+ "format": "biome format --write ./src",
17
+ "check-h": "biome check --write ./src",
18
+ "check": "biome check --write --no-errors-on-unmatched ./src",
19
+ "test": "vitest run --config ./tests/vitest.config.mts ./tests/"
20
+ },
21
+ "author": "MARCROCK22",
22
+ "license": "MIT",
23
+ "devDependencies": {
24
+ "@biomejs/biome": "1.9.3",
25
+ "@commitlint/cli": "^19.5.0",
26
+ "@commitlint/config-conventional": "^19.5.0",
27
+ "@types/node": "^22.7.4",
28
+ "husky": "^9.1.6",
29
+ "lint-staged": "^15.2.10",
30
+ "typescript": "^5.6.2",
31
+ "vitest": "^2.1.2"
32
+ },
33
+ "homepage": "https://seyfert.dev",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/tiramisulabs/seyfert.git"
37
+ },
38
+ "bugs": {
39
+ "url": "https://github.com/tiramisulabs/seyfert"
40
+ },
41
+ "keywords": [
42
+ "api",
43
+ "discord",
44
+ "bots",
45
+ "typescript",
46
+ "botdev"
47
+ ],
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "maintainers": [
52
+ {
53
+ "name": "socram03",
54
+ "url": "https://github.com/socram03"
55
+ }
56
+ ],
57
+ "contributors": [
58
+ {
59
+ "name": "Free 公園",
60
+ "url": "https://github.com/FreeAoi"
61
+ },
62
+ {
63
+ "name": "David",
64
+ "url": "https://github.com/Drylozu"
65
+ }
66
+ ],
67
+ "lint-staged": {
68
+ "*.ts": [
69
+ "biome check --write"
70
+ ]
71
+ }
72
+ }
@@ -1,14 +0,0 @@
1
- import type { APIThreadChannel } from '../../types';
2
- import type { ReturnCache } from '../..';
3
- import { GuildRelatedResource } from './default/guild-related';
4
- import { type ThreadChannelStructure } from '../../client/transformers';
5
- export declare class Threads extends GuildRelatedResource<any, APIThreadChannel> {
6
- namespace: string;
7
- filter(data: APIThreadChannel, id: string, guild_id?: string): boolean;
8
- get(id: string): ReturnCache<ThreadChannelStructure | undefined>;
9
- raw(id: string): ReturnCache<APIThreadChannel | undefined>;
10
- bulk(ids: string[]): ReturnCache<ThreadChannelStructure[]>;
11
- bulkRaw(ids: string[]): ReturnCache<APIThreadChannel[]>;
12
- values(guild: string): ReturnCache<ThreadChannelStructure[]>;
13
- valuesRaw(guild: string): ReturnCache<APIThreadChannel[]>;
14
- }
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Threads = void 0;
4
- const common_1 = require("../../common");
5
- const guild_related_1 = require("./default/guild-related");
6
- const transformers_1 = require("../../client/transformers");
7
- class Threads extends guild_related_1.GuildRelatedResource {
8
- namespace = 'thread';
9
- //@ts-expect-error
10
- filter(data, id, guild_id) {
11
- return true;
12
- }
13
- get(id) {
14
- return (0, common_1.fakePromise)(super.get(id)).then(rawThread => rawThread ? transformers_1.Transformers.ThreadChannel(this.client, rawThread) : undefined);
15
- }
16
- raw(id) {
17
- return super.get(id);
18
- }
19
- bulk(ids) {
20
- return (0, common_1.fakePromise)(super.bulk(ids)).then(threads => threads.map(rawThread => transformers_1.Transformers.ThreadChannel(this.client, rawThread)));
21
- }
22
- bulkRaw(ids) {
23
- return super.bulk(ids);
24
- }
25
- values(guild) {
26
- return (0, common_1.fakePromise)(super.values(guild)).then(threads => threads.map(rawThread => transformers_1.Transformers.ThreadChannel(this.client, rawThread)));
27
- }
28
- valuesRaw(guild) {
29
- return super.values(guild);
30
- }
31
- }
32
- exports.Threads = Threads;