@stonyx/discord 0.1.1-alpha.16 → 0.1.1-alpha.17

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 (2) hide show
  1. package/dist/bot.js +16 -2
  2. package/package.json +1 -1
package/dist/bot.js CHANGED
@@ -5,6 +5,7 @@ import { forEachFileImport } from '@stonyx/utils/file';
5
5
  import { deriveIntents, derivePartials } from './intents.js';
6
6
  import { chunkMessage } from './message.js';
7
7
  const MAX_RETRIES = 3;
8
+ const MAX_QUEUE_SIZE = 1000;
8
9
  const QUEUE_TTL = 300_000; // 5 minutes
9
10
  const READY_TIMEOUT = 30_000; // 30 seconds
10
11
  const RETRYABLE_NETWORK_CODES = new Set(['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED']);
@@ -85,6 +86,8 @@ export default class DiscordBot {
85
86
  log.error('Discord session invalidated — rejecting all queued messages');
86
87
  const entries = this._queue.splice(0);
87
88
  for (const entry of entries) {
89
+ if (entry.timerId !== null)
90
+ clearTimeout(entry.timerId);
88
91
  entry.reject(new Error('Discord session invalidated'));
89
92
  }
90
93
  });
@@ -177,6 +180,9 @@ export default class DiscordBot {
177
180
  return this._enqueue(content, channelId, imagePath);
178
181
  }
179
182
  _enqueue(content, channelId, imagePath) {
183
+ if (this._queue.length >= MAX_QUEUE_SIZE) {
184
+ return Promise.reject(new Error('Message queue full'));
185
+ }
180
186
  return new Promise((resolve, reject) => {
181
187
  this._queue.push({
182
188
  content,
@@ -186,10 +192,14 @@ export default class DiscordBot {
186
192
  reject,
187
193
  retries: 0,
188
194
  enqueuedAt: Date.now(),
195
+ timerId: null,
189
196
  });
190
197
  });
191
198
  }
192
199
  _enqueueForRetry(content, channelId, imagePath, retries) {
200
+ if (this._queue.length >= MAX_QUEUE_SIZE) {
201
+ return Promise.reject(new Error('Message queue full'));
202
+ }
193
203
  return new Promise((resolve, reject) => {
194
204
  const entry = {
195
205
  content,
@@ -199,6 +209,7 @@ export default class DiscordBot {
199
209
  reject,
200
210
  retries,
201
211
  enqueuedAt: Date.now(),
212
+ timerId: null,
202
213
  };
203
214
  this._queue.push(entry);
204
215
  this._scheduleRetry(entry);
@@ -206,7 +217,8 @@ export default class DiscordBot {
206
217
  }
207
218
  _scheduleRetry(entry) {
208
219
  const backoff = Math.min(1000 * Math.pow(2, entry.retries) + Math.floor(Math.random() * 500), 30000);
209
- setTimeout(() => {
220
+ entry.timerId = setTimeout(() => {
221
+ entry.timerId = null;
210
222
  // Only process if the entry is still in the queue (wasn't rejected by close/invalidated)
211
223
  const idx = this._queue.indexOf(entry);
212
224
  if (idx === -1)
@@ -373,9 +385,11 @@ export default class DiscordBot {
373
385
  await member.roles.add(role);
374
386
  }
375
387
  close() {
376
- // Reject all pending queue entries
388
+ // Cancel all pending retry timers and reject all queue entries
377
389
  const entries = this._queue.splice(0);
378
390
  for (const entry of entries) {
391
+ if (entry.timerId !== null)
392
+ clearTimeout(entry.timerId);
379
393
  entry.reject(new Error('Discord bot closed'));
380
394
  }
381
395
  if (this.client) {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.1.1-alpha.16",
7
+ "version": "0.1.1-alpha.17",
8
8
  "description": "Discord bot module for the Stonyx framework",
9
9
  "main": "dist/main.js",
10
10
  "types": "dist/main.d.ts",