@dereekb/nestjs 13.32.0 → 13.34.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.
@@ -1,1500 +0,0 @@
1
- import { handlerFactory, handlerConfigurerFactory, handlerMappedSetFunctionFactory, isHexWithByteLength, lastValue } from '@dereekb/util';
2
- import { InteractionType, GatewayIntentBits, Client, Events, TextChannel } from 'discord.js';
3
- import { RawBody, isTestNodeEnv } from '@dereekb/nestjs';
4
- import { Injectable, Inject, Logger, Post, Req, Controller, Module } from '@nestjs/common';
5
- import { createPublicKey, verify } from 'node:crypto';
6
- import { ConfigService, ConfigModule } from '@nestjs/config';
7
- import { fetchPageFactory } from '@dereekb/util/fetch';
8
-
9
- function _define_property$6(obj, key, value) {
10
- if (key in obj) {
11
- Object.defineProperty(obj, key, {
12
- value: value,
13
- enumerable: true,
14
- configurable: true,
15
- writable: true
16
- });
17
- } else {
18
- obj[key] = value;
19
- }
20
- return obj;
21
- }
22
- function _object_spread$2(target) {
23
- for(var i = 1; i < arguments.length; i++){
24
- var source = arguments[i] != null ? arguments[i] : {};
25
- var ownKeys = Object.keys(source);
26
- if (typeof Object.getOwnPropertySymbols === "function") {
27
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
28
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
29
- }));
30
- }
31
- ownKeys.forEach(function(key) {
32
- _define_property$6(target, key, source[key]);
33
- });
34
- }
35
- return target;
36
- }
37
- function ownKeys$1(object, enumerableOnly) {
38
- var keys = Object.keys(object);
39
- if (Object.getOwnPropertySymbols) {
40
- var symbols = Object.getOwnPropertySymbols(object);
41
- keys.push.apply(keys, symbols);
42
- }
43
- return keys;
44
- }
45
- function _object_spread_props$1(target, source) {
46
- source = source != null ? source : {};
47
- if (Object.getOwnPropertyDescriptors) {
48
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
49
- } else {
50
- ownKeys$1(Object(source)).forEach(function(key) {
51
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
52
- });
53
- }
54
- return target;
55
- }
56
- /**
57
- * Casts an untyped Discord interaction to a typed one.
58
- *
59
- * @param interaction - The raw interaction to cast.
60
- * @returns The interaction cast to the specified typed DiscordWebhookInteraction.
61
- */ function discordWebhookInteraction(interaction) {
62
- return interaction;
63
- }
64
- var discordInteractionHandlerFactory = handlerFactory(function(x) {
65
- return x.type;
66
- });
67
- var discordInteractionHandlerConfigurerFactory = handlerConfigurerFactory({
68
- configurerForAccessor: function configurerForAccessor(accessor) {
69
- var fnWithKey = handlerMappedSetFunctionFactory(accessor, discordWebhookInteraction);
70
- var configurer = _object_spread_props$1(_object_spread$2({}, accessor), {
71
- handleApplicationCommand: fnWithKey(InteractionType.ApplicationCommand),
72
- handleMessageComponent: fnWithKey(InteractionType.MessageComponent),
73
- handleModalSubmit: fnWithKey(InteractionType.ModalSubmit),
74
- handleAutocomplete: fnWithKey(InteractionType.ApplicationCommandAutocomplete)
75
- });
76
- return configurer;
77
- }
78
- });
79
-
80
- function _class_call_check$6(instance, Constructor) {
81
- if (!(instance instanceof Constructor)) {
82
- throw new TypeError("Cannot call a class as a function");
83
- }
84
- }
85
- function _defineProperties$4(target, props) {
86
- for(var i = 0; i < props.length; i++){
87
- var descriptor = props[i];
88
- descriptor.enumerable = descriptor.enumerable || false;
89
- descriptor.configurable = true;
90
- if ("value" in descriptor) descriptor.writable = true;
91
- Object.defineProperty(target, descriptor.key, descriptor);
92
- }
93
- }
94
- function _create_class$4(Constructor, protoProps, staticProps) {
95
- if (staticProps) _defineProperties$4(Constructor, staticProps);
96
- return Constructor;
97
- }
98
- function _define_property$5(obj, key, value) {
99
- if (key in obj) {
100
- Object.defineProperty(obj, key, {
101
- value: value,
102
- enumerable: true,
103
- configurable: true,
104
- writable: true
105
- });
106
- } else {
107
- obj[key] = value;
108
- }
109
- return obj;
110
- }
111
- /**
112
- * Default environment variable for the Discord application public key.
113
- */ var DISCORD_PUBLIC_KEY_ENV_VAR = 'DISCORD_PUBLIC_KEY';
114
- /**
115
- * The byte length of a Discord Ed25519 public key (32 bytes = 64 hex characters).
116
- */ var DISCORD_ED25519_PUBLIC_KEY_BYTE_LENGTH = 32;
117
- /**
118
- * Configuration for the DiscordWebhookService.
119
- */ var DiscordWebhookServiceConfig = /*#__PURE__*/ function() {
120
- function DiscordWebhookServiceConfig() {
121
- _class_call_check$6(this, DiscordWebhookServiceConfig);
122
- _define_property$5(this, "discordWebhook", void 0);
123
- }
124
- _create_class$4(DiscordWebhookServiceConfig, null, [
125
- {
126
- key: "assertValidConfig",
127
- value: function assertValidConfig(config) {
128
- var publicKey = config.discordWebhook.publicKey;
129
- if (!publicKey) {
130
- throw new Error('No Discord public key specified.');
131
- }
132
- if (!isHexWithByteLength(publicKey, {
133
- byteLength: DISCORD_ED25519_PUBLIC_KEY_BYTE_LENGTH
134
- })) {
135
- throw new Error('Discord public key is not available or is invalid. Expected a 64-character hex string (32-byte Ed25519 key).');
136
- }
137
- }
138
- }
139
- ]);
140
- return DiscordWebhookServiceConfig;
141
- }
142
- ();
143
-
144
- function _type_of(obj) {
145
- "@swc/helpers - typeof";
146
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
147
- }
148
- function __decorate(decorators, target, key, desc) {
149
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
150
- if ((typeof Reflect === "undefined" ? "undefined" : _type_of(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
151
- else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
152
- return c > 3 && r && Object.defineProperty(target, key, r), r;
153
- }
154
- function __param(paramIndex, decorator) {
155
- return function(target, key) {
156
- decorator(target, key, paramIndex);
157
- };
158
- }
159
- typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
160
- var e = new Error(message);
161
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
162
- };
163
-
164
- function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
165
- try {
166
- var info = gen[key](arg);
167
- var value = info.value;
168
- } catch (error) {
169
- reject(error);
170
- return;
171
- }
172
- if (info.done) {
173
- resolve(value);
174
- } else {
175
- Promise.resolve(value).then(_next, _throw);
176
- }
177
- }
178
- function _async_to_generator$3(fn) {
179
- return function() {
180
- var self = this, args = arguments;
181
- return new Promise(function(resolve, reject) {
182
- var gen = fn.apply(self, args);
183
- function _next(value) {
184
- asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
185
- }
186
- function _throw(err) {
187
- asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
188
- }
189
- _next(undefined);
190
- });
191
- };
192
- }
193
- function _ts_generator$3(thisArg, body) {
194
- var f, y, t, _ = {
195
- label: 0,
196
- sent: function() {
197
- if (t[0] & 1) throw t[1];
198
- return t[1];
199
- },
200
- trys: [],
201
- ops: []
202
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
203
- return d(g, "next", {
204
- value: verb(0)
205
- }), d(g, "throw", {
206
- value: verb(1)
207
- }), d(g, "return", {
208
- value: verb(2)
209
- }), typeof Symbol === "function" && d(g, Symbol.iterator, {
210
- value: function() {
211
- return this;
212
- }
213
- }), g;
214
- function verb(n) {
215
- return function(v) {
216
- return step([
217
- n,
218
- v
219
- ]);
220
- };
221
- }
222
- function step(op) {
223
- if (f) throw new TypeError("Generator is already executing.");
224
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
225
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
226
- if (y = 0, t) op = [
227
- op[0] & 2,
228
- t.value
229
- ];
230
- switch(op[0]){
231
- case 0:
232
- case 1:
233
- t = op;
234
- break;
235
- case 4:
236
- _.label++;
237
- return {
238
- value: op[1],
239
- done: false
240
- };
241
- case 5:
242
- _.label++;
243
- y = op[1];
244
- op = [
245
- 0
246
- ];
247
- continue;
248
- case 7:
249
- op = _.ops.pop();
250
- _.trys.pop();
251
- continue;
252
- default:
253
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
254
- _ = 0;
255
- continue;
256
- }
257
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
258
- _.label = op[1];
259
- break;
260
- }
261
- if (op[0] === 6 && _.label < t[1]) {
262
- _.label = t[1];
263
- t = op;
264
- break;
265
- }
266
- if (t && _.label < t[2]) {
267
- _.label = t[2];
268
- _.ops.push(op);
269
- break;
270
- }
271
- if (t[2]) _.ops.pop();
272
- _.trys.pop();
273
- continue;
274
- }
275
- op = body.call(thisArg, _);
276
- } catch (e) {
277
- op = [
278
- 6,
279
- e
280
- ];
281
- y = 0;
282
- } finally{
283
- f = t = 0;
284
- }
285
- if (op[0] & 5) throw op[1];
286
- return {
287
- value: op[0] ? op[1] : void 0,
288
- done: true
289
- };
290
- }
291
- }
292
- /**
293
- * Creates a verifier for Discord interaction webhook requests.
294
- *
295
- * Discord signs interaction webhook requests with Ed25519. The signed message is
296
- * the concatenation of the x-signature-timestamp header and the raw request body.
297
- * The signature is provided in the x-signature-ed25519 header as a hex string.
298
- *
299
- * Uses Node.js built-in crypto with JWK key import — no external dependencies required.
300
- *
301
- * @param config - Verification config containing the application's public key.
302
- * @returns A DiscordWebhookEventVerifier function that validates Ed25519-signed requests.
303
- *
304
- * @example
305
- * ```ts
306
- * const verifier = discordWebhookEventVerifier({ publicKey: 'your-hex-public-key' });
307
- * const result = await verifier(req, rawBody);
308
- *
309
- * if (result.valid) {
310
- * // result.body contains the parsed interaction
311
- * }
312
- * ```
313
- */ function discordWebhookEventVerifier(config) {
314
- var publicKeyHex = config.publicKey;
315
- // Import the raw 32-byte Ed25519 public key via JWK format.
316
- var publicKey = createPublicKey({
317
- key: {
318
- kty: 'OKP',
319
- crv: 'Ed25519',
320
- x: Buffer.from(publicKeyHex, 'hex').toString('base64url')
321
- },
322
- format: 'jwk'
323
- });
324
- return function(request, rawBody) {
325
- return _async_to_generator$3(function() {
326
- var signature, timestamp, result, message, signatureBuffer, valid, body;
327
- return _ts_generator$3(this, function(_state) {
328
- signature = request.headers['x-signature-ed25519'];
329
- timestamp = request.headers['x-signature-timestamp'];
330
- if (!signature || !timestamp) {
331
- result = {
332
- valid: false
333
- };
334
- } else {
335
- message = Buffer.concat([
336
- Buffer.from(timestamp),
337
- rawBody
338
- ]);
339
- signatureBuffer = Buffer.from(signature, 'hex');
340
- valid = false;
341
- try {
342
- valid = verify(null, message, publicKey, signatureBuffer);
343
- } catch (unused) {
344
- valid = false;
345
- }
346
- if (valid) {
347
- body = JSON.parse(rawBody.toString('utf-8'));
348
- result = {
349
- valid: true,
350
- body: body
351
- };
352
- } else {
353
- result = {
354
- valid: false
355
- };
356
- }
357
- }
358
- return [
359
- 2,
360
- result
361
- ];
362
- });
363
- })();
364
- };
365
- }
366
-
367
- function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
368
- try {
369
- var info = gen[key](arg);
370
- var value = info.value;
371
- } catch (error) {
372
- reject(error);
373
- return;
374
- }
375
- if (info.done) {
376
- resolve(value);
377
- } else {
378
- Promise.resolve(value).then(_next, _throw);
379
- }
380
- }
381
- function _async_to_generator$2(fn) {
382
- return function() {
383
- var self = this, args = arguments;
384
- return new Promise(function(resolve, reject) {
385
- var gen = fn.apply(self, args);
386
- function _next(value) {
387
- asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
388
- }
389
- function _throw(err) {
390
- asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
391
- }
392
- _next(undefined);
393
- });
394
- };
395
- }
396
- function _class_call_check$5(instance, Constructor) {
397
- if (!(instance instanceof Constructor)) {
398
- throw new TypeError("Cannot call a class as a function");
399
- }
400
- }
401
- function _defineProperties$3(target, props) {
402
- for(var i = 0; i < props.length; i++){
403
- var descriptor = props[i];
404
- descriptor.enumerable = descriptor.enumerable || false;
405
- descriptor.configurable = true;
406
- if ("value" in descriptor) descriptor.writable = true;
407
- Object.defineProperty(target, descriptor.key, descriptor);
408
- }
409
- }
410
- function _create_class$3(Constructor, protoProps, staticProps) {
411
- if (protoProps) _defineProperties$3(Constructor.prototype, protoProps);
412
- return Constructor;
413
- }
414
- function _define_property$4(obj, key, value) {
415
- if (key in obj) {
416
- Object.defineProperty(obj, key, {
417
- value: value,
418
- enumerable: true,
419
- configurable: true,
420
- writable: true
421
- });
422
- } else {
423
- obj[key] = value;
424
- }
425
- return obj;
426
- }
427
- function _ts_generator$2(thisArg, body) {
428
- var f, y, t, _ = {
429
- label: 0,
430
- sent: function() {
431
- if (t[0] & 1) throw t[1];
432
- return t[1];
433
- },
434
- trys: [],
435
- ops: []
436
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
437
- return d(g, "next", {
438
- value: verb(0)
439
- }), d(g, "throw", {
440
- value: verb(1)
441
- }), d(g, "return", {
442
- value: verb(2)
443
- }), typeof Symbol === "function" && d(g, Symbol.iterator, {
444
- value: function() {
445
- return this;
446
- }
447
- }), g;
448
- function verb(n) {
449
- return function(v) {
450
- return step([
451
- n,
452
- v
453
- ]);
454
- };
455
- }
456
- function step(op) {
457
- if (f) throw new TypeError("Generator is already executing.");
458
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
459
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
460
- if (y = 0, t) op = [
461
- op[0] & 2,
462
- t.value
463
- ];
464
- switch(op[0]){
465
- case 0:
466
- case 1:
467
- t = op;
468
- break;
469
- case 4:
470
- _.label++;
471
- return {
472
- value: op[1],
473
- done: false
474
- };
475
- case 5:
476
- _.label++;
477
- y = op[1];
478
- op = [
479
- 0
480
- ];
481
- continue;
482
- case 7:
483
- op = _.ops.pop();
484
- _.trys.pop();
485
- continue;
486
- default:
487
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
488
- _ = 0;
489
- continue;
490
- }
491
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
492
- _.label = op[1];
493
- break;
494
- }
495
- if (op[0] === 6 && _.label < t[1]) {
496
- _.label = t[1];
497
- t = op;
498
- break;
499
- }
500
- if (t && _.label < t[2]) {
501
- _.label = t[2];
502
- _.ops.push(op);
503
- break;
504
- }
505
- if (t[2]) _.ops.pop();
506
- _.trys.pop();
507
- continue;
508
- }
509
- op = body.call(thisArg, _);
510
- } catch (e) {
511
- op = [
512
- 6,
513
- e
514
- ];
515
- y = 0;
516
- } finally{
517
- f = t = 0;
518
- }
519
- if (op[0] & 5) throw op[1];
520
- return {
521
- value: op[0] ? op[1] : void 0,
522
- done: true
523
- };
524
- }
525
- }
526
- /**
527
- * Service that handles Discord interaction webhook events.
528
- *
529
- * Verifies incoming webhook signatures and dispatches interactions to registered handlers.
530
- */ var DiscordWebhookService = /*#__PURE__*/ function() {
531
- function DiscordWebhookService(discordWebhookServiceConfig) {
532
- _class_call_check$5(this, DiscordWebhookService);
533
- _define_property$4(this, "logger", new Logger('DiscordWebhookService'));
534
- _define_property$4(this, "_verifier", void 0);
535
- _define_property$4(this, "handler", discordInteractionHandlerFactory());
536
- _define_property$4(this, "configure", discordInteractionHandlerConfigurerFactory(this.handler));
537
- this._verifier = discordWebhookEventVerifier({
538
- publicKey: discordWebhookServiceConfig.discordWebhook.publicKey
539
- });
540
- }
541
- _create_class$3(DiscordWebhookService, [
542
- {
543
- key: "updateForWebhook",
544
- value: function updateForWebhook(req, rawBody) {
545
- return _async_to_generator$2(function() {
546
- var result;
547
- return _ts_generator$2(this, function(_state) {
548
- switch(_state.label){
549
- case 0:
550
- return [
551
- 4,
552
- this._verifier(req, rawBody)
553
- ];
554
- case 1:
555
- result = _state.sent();
556
- if (!result.valid) return [
557
- 3,
558
- 3
559
- ];
560
- return [
561
- 4,
562
- this.updateForDiscordInteraction(result.body)
563
- ];
564
- case 2:
565
- _state.sent();
566
- return [
567
- 3,
568
- 4
569
- ];
570
- case 3:
571
- this.logger.warn('Received invalid Discord interaction event.', req);
572
- _state.label = 4;
573
- case 4:
574
- return [
575
- 2
576
- ];
577
- }
578
- });
579
- }).call(this);
580
- }
581
- },
582
- {
583
- key: "updateForDiscordInteraction",
584
- value: function updateForDiscordInteraction(interaction) {
585
- return _async_to_generator$2(function() {
586
- var result;
587
- return _ts_generator$2(this, function(_state) {
588
- switch(_state.label){
589
- case 0:
590
- return [
591
- 4,
592
- this.handler(interaction)
593
- ];
594
- case 1:
595
- result = _state.sent();
596
- if (!result) {
597
- this.logger.warn('Received unexpected/unhandled Discord interaction.', interaction);
598
- }
599
- return [
600
- 2
601
- ];
602
- }
603
- });
604
- }).call(this);
605
- }
606
- }
607
- ]);
608
- return DiscordWebhookService;
609
- }();
610
- DiscordWebhookService = __decorate([
611
- Injectable(),
612
- __param(0, Inject(DiscordWebhookServiceConfig))
613
- ], DiscordWebhookService);
614
-
615
- function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
616
- try {
617
- var info = gen[key](arg);
618
- var value = info.value;
619
- } catch (error) {
620
- reject(error);
621
- return;
622
- }
623
- if (info.done) {
624
- resolve(value);
625
- } else {
626
- Promise.resolve(value).then(_next, _throw);
627
- }
628
- }
629
- function _async_to_generator$1(fn) {
630
- return function() {
631
- var self = this, args = arguments;
632
- return new Promise(function(resolve, reject) {
633
- var gen = fn.apply(self, args);
634
- function _next(value) {
635
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
636
- }
637
- function _throw(err) {
638
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
639
- }
640
- _next(undefined);
641
- });
642
- };
643
- }
644
- function _class_call_check$4(instance, Constructor) {
645
- if (!(instance instanceof Constructor)) {
646
- throw new TypeError("Cannot call a class as a function");
647
- }
648
- }
649
- function _defineProperties$2(target, props) {
650
- for(var i = 0; i < props.length; i++){
651
- var descriptor = props[i];
652
- descriptor.enumerable = descriptor.enumerable || false;
653
- descriptor.configurable = true;
654
- if ("value" in descriptor) descriptor.writable = true;
655
- Object.defineProperty(target, descriptor.key, descriptor);
656
- }
657
- }
658
- function _create_class$2(Constructor, protoProps, staticProps) {
659
- if (protoProps) _defineProperties$2(Constructor.prototype, protoProps);
660
- return Constructor;
661
- }
662
- function _define_property$3(obj, key, value) {
663
- if (key in obj) {
664
- Object.defineProperty(obj, key, {
665
- value: value,
666
- enumerable: true,
667
- configurable: true,
668
- writable: true
669
- });
670
- } else {
671
- obj[key] = value;
672
- }
673
- return obj;
674
- }
675
- function _ts_generator$1(thisArg, body) {
676
- var f, y, t, _ = {
677
- label: 0,
678
- sent: function() {
679
- if (t[0] & 1) throw t[1];
680
- return t[1];
681
- },
682
- trys: [],
683
- ops: []
684
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
685
- return d(g, "next", {
686
- value: verb(0)
687
- }), d(g, "throw", {
688
- value: verb(1)
689
- }), d(g, "return", {
690
- value: verb(2)
691
- }), typeof Symbol === "function" && d(g, Symbol.iterator, {
692
- value: function() {
693
- return this;
694
- }
695
- }), g;
696
- function verb(n) {
697
- return function(v) {
698
- return step([
699
- n,
700
- v
701
- ]);
702
- };
703
- }
704
- function step(op) {
705
- if (f) throw new TypeError("Generator is already executing.");
706
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
707
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
708
- if (y = 0, t) op = [
709
- op[0] & 2,
710
- t.value
711
- ];
712
- switch(op[0]){
713
- case 0:
714
- case 1:
715
- t = op;
716
- break;
717
- case 4:
718
- _.label++;
719
- return {
720
- value: op[1],
721
- done: false
722
- };
723
- case 5:
724
- _.label++;
725
- y = op[1];
726
- op = [
727
- 0
728
- ];
729
- continue;
730
- case 7:
731
- op = _.ops.pop();
732
- _.trys.pop();
733
- continue;
734
- default:
735
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
736
- _ = 0;
737
- continue;
738
- }
739
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
740
- _.label = op[1];
741
- break;
742
- }
743
- if (op[0] === 6 && _.label < t[1]) {
744
- _.label = t[1];
745
- t = op;
746
- break;
747
- }
748
- if (t && _.label < t[2]) {
749
- _.label = t[2];
750
- _.ops.push(op);
751
- break;
752
- }
753
- if (t[2]) _.ops.pop();
754
- _.trys.pop();
755
- continue;
756
- }
757
- op = body.call(thisArg, _);
758
- } catch (e) {
759
- op = [
760
- 6,
761
- e
762
- ];
763
- y = 0;
764
- } finally{
765
- f = t = 0;
766
- }
767
- if (op[0] & 5) throw op[1];
768
- return {
769
- value: op[0] ? op[1] : void 0,
770
- done: true
771
- };
772
- }
773
- }
774
- var DiscordWebhookController = /*#__PURE__*/ function() {
775
- function DiscordWebhookController(discordWebhookService) {
776
- _class_call_check$4(this, DiscordWebhookController);
777
- _define_property$3(this, "_discordWebhookService", void 0);
778
- this._discordWebhookService = discordWebhookService;
779
- }
780
- _create_class$2(DiscordWebhookController, [
781
- {
782
- key: "handleDiscordWebhook",
783
- value: function handleDiscordWebhook(req, rawBody) {
784
- return _async_to_generator$1(function() {
785
- return _ts_generator$1(this, function(_state) {
786
- switch(_state.label){
787
- case 0:
788
- return [
789
- 4,
790
- this._discordWebhookService.updateForWebhook(req, rawBody)
791
- ];
792
- case 1:
793
- _state.sent();
794
- return [
795
- 2
796
- ];
797
- }
798
- });
799
- }).call(this);
800
- }
801
- }
802
- ]);
803
- return DiscordWebhookController;
804
- }();
805
- __decorate([
806
- Post(),
807
- __param(0, Req()),
808
- __param(1, RawBody())
809
- ], DiscordWebhookController.prototype, "handleDiscordWebhook", null);
810
- DiscordWebhookController = __decorate([
811
- Controller('/webhook/discord'),
812
- __param(0, Inject(DiscordWebhookService))
813
- ], DiscordWebhookController);
814
-
815
- function _class_call_check$3(instance, Constructor) {
816
- if (!(instance instanceof Constructor)) {
817
- throw new TypeError("Cannot call a class as a function");
818
- }
819
- }
820
- /**
821
- * Factory that creates a DiscordWebhookServiceConfig from environment variables.
822
- *
823
- * @param configService - The NestJS config service used to read the Discord public key environment variable.
824
- * @returns A validated DiscordWebhookServiceConfig populated from environment variables.
825
- */ function discordWebhookServiceConfigFactory(configService) {
826
- var config = {
827
- discordWebhook: {
828
- publicKey: configService.get(DISCORD_PUBLIC_KEY_ENV_VAR)
829
- }
830
- };
831
- DiscordWebhookServiceConfig.assertValidConfig(config);
832
- return config;
833
- }
834
- /**
835
- * NestJS module that provides Discord interaction webhook handling.
836
- *
837
- * Standalone — does not depend on DiscordModule (no bot token needed).
838
- * Reads the application public key from the DISCORD_PUBLIC_KEY environment variable.
839
- */ var DiscordWebhookModule = function DiscordWebhookModule() {
840
- _class_call_check$3(this, DiscordWebhookModule);
841
- };
842
- DiscordWebhookModule = __decorate([
843
- Module({
844
- imports: [
845
- ConfigModule
846
- ],
847
- controllers: [
848
- DiscordWebhookController
849
- ],
850
- providers: [
851
- {
852
- provide: DiscordWebhookServiceConfig,
853
- inject: [
854
- ConfigService
855
- ],
856
- useFactory: discordWebhookServiceConfigFactory
857
- },
858
- DiscordWebhookService
859
- ],
860
- exports: [
861
- DiscordWebhookService
862
- ]
863
- })
864
- ], DiscordWebhookModule);
865
-
866
- function _class_call_check$2(instance, Constructor) {
867
- if (!(instance instanceof Constructor)) {
868
- throw new TypeError("Cannot call a class as a function");
869
- }
870
- }
871
- function _defineProperties$1(target, props) {
872
- for(var i = 0; i < props.length; i++){
873
- var descriptor = props[i];
874
- descriptor.enumerable = descriptor.enumerable || false;
875
- descriptor.configurable = true;
876
- if ("value" in descriptor) descriptor.writable = true;
877
- Object.defineProperty(target, descriptor.key, descriptor);
878
- }
879
- }
880
- function _create_class$1(Constructor, protoProps, staticProps) {
881
- if (staticProps) _defineProperties$1(Constructor, staticProps);
882
- return Constructor;
883
- }
884
- function _define_property$2(obj, key, value) {
885
- if (key in obj) {
886
- Object.defineProperty(obj, key, {
887
- value: value,
888
- enumerable: true,
889
- configurable: true,
890
- writable: true
891
- });
892
- } else {
893
- obj[key] = value;
894
- }
895
- return obj;
896
- }
897
- /**
898
- * Default environment variable for the Discord bot token.
899
- */ var DISCORD_BOT_TOKEN_ENV_VAR = 'DISCORD_BOT_TOKEN';
900
- /**
901
- * Placeholder bot token value used in development and CI environments where a real Discord
902
- * login should not be attempted. Mirrors the value used in the workspace's .env file.
903
- */ var DISCORD_BOT_TOKEN_PLACEHOLDER = 'placeholder';
904
- /**
905
- * Returns true if the input is a real, usable Discord bot token.
906
- *
907
- * A token is usable when it is non-empty and is not the shared placeholder value, allowing
908
- * non-production environments to skip a real gateway login that would always fail.
909
- *
910
- * @param botToken - The bot token read from configuration, if any.
911
- * @returns True when the token should be used to log in.
912
- *
913
- * @example
914
- * ```ts
915
- * isUsableDiscordBotToken('placeholder'); // false
916
- * isUsableDiscordBotToken('real-token'); // true
917
- * ```
918
- */ function isUsableDiscordBotToken(botToken) {
919
- return Boolean(botToken) && botToken !== DISCORD_BOT_TOKEN_PLACEHOLDER;
920
- }
921
- /**
922
- * Default gateway intents for a bot that reads guild messages.
923
- *
924
- * Includes Guilds, GuildMessages, and MessageContent.
925
- * Note: MessageContent is a privileged intent and must be enabled in the Discord Developer Portal.
926
- */ var DEFAULT_DISCORD_INTENTS = [
927
- GatewayIntentBits.Guilds,
928
- GatewayIntentBits.GuildMessages,
929
- GatewayIntentBits.MessageContent
930
- ];
931
- /**
932
- * Configuration for the DiscordApi service.
933
- */ var DiscordServiceConfig = /*#__PURE__*/ function() {
934
- function DiscordServiceConfig() {
935
- _class_call_check$2(this, DiscordServiceConfig);
936
- _define_property$2(this, "discord", void 0);
937
- }
938
- _create_class$1(DiscordServiceConfig, null, [
939
- {
940
- key: "assertValidConfig",
941
- value: function assertValidConfig(config) {
942
- if (!config.discord.botToken) {
943
- throw new Error('No Discord bot token specified.');
944
- }
945
- }
946
- }
947
- ]);
948
- return DiscordServiceConfig;
949
- }
950
- ();
951
-
952
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
953
- try {
954
- var info = gen[key](arg);
955
- var value = info.value;
956
- } catch (error) {
957
- reject(error);
958
- return;
959
- }
960
- if (info.done) {
961
- resolve(value);
962
- } else {
963
- Promise.resolve(value).then(_next, _throw);
964
- }
965
- }
966
- function _async_to_generator(fn) {
967
- return function() {
968
- var self = this, args = arguments;
969
- return new Promise(function(resolve, reject) {
970
- var gen = fn.apply(self, args);
971
- function _next(value) {
972
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
973
- }
974
- function _throw(err) {
975
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
976
- }
977
- _next(undefined);
978
- });
979
- };
980
- }
981
- function _class_call_check$1(instance, Constructor) {
982
- if (!(instance instanceof Constructor)) {
983
- throw new TypeError("Cannot call a class as a function");
984
- }
985
- }
986
- function _defineProperties(target, props) {
987
- for(var i = 0; i < props.length; i++){
988
- var descriptor = props[i];
989
- descriptor.enumerable = descriptor.enumerable || false;
990
- descriptor.configurable = true;
991
- if ("value" in descriptor) descriptor.writable = true;
992
- Object.defineProperty(target, descriptor.key, descriptor);
993
- }
994
- }
995
- function _create_class(Constructor, protoProps, staticProps) {
996
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
997
- return Constructor;
998
- }
999
- function _define_property$1(obj, key, value) {
1000
- if (key in obj) {
1001
- Object.defineProperty(obj, key, {
1002
- value: value,
1003
- enumerable: true,
1004
- configurable: true,
1005
- writable: true
1006
- });
1007
- } else {
1008
- obj[key] = value;
1009
- }
1010
- return obj;
1011
- }
1012
- function _instanceof(left, right) {
1013
- "@swc/helpers - instanceof";
1014
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
1015
- return !!right[Symbol.hasInstance](left);
1016
- } else {
1017
- return left instanceof right;
1018
- }
1019
- }
1020
- function _object_spread$1(target) {
1021
- for(var i = 1; i < arguments.length; i++){
1022
- var source = arguments[i] != null ? arguments[i] : {};
1023
- var ownKeys = Object.keys(source);
1024
- if (typeof Object.getOwnPropertySymbols === "function") {
1025
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
1026
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
1027
- }));
1028
- }
1029
- ownKeys.forEach(function(key) {
1030
- _define_property$1(target, key, source[key]);
1031
- });
1032
- }
1033
- return target;
1034
- }
1035
- function _ts_generator(thisArg, body) {
1036
- var f, y, t, _ = {
1037
- label: 0,
1038
- sent: function() {
1039
- if (t[0] & 1) throw t[1];
1040
- return t[1];
1041
- },
1042
- trys: [],
1043
- ops: []
1044
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
1045
- return d(g, "next", {
1046
- value: verb(0)
1047
- }), d(g, "throw", {
1048
- value: verb(1)
1049
- }), d(g, "return", {
1050
- value: verb(2)
1051
- }), typeof Symbol === "function" && d(g, Symbol.iterator, {
1052
- value: function() {
1053
- return this;
1054
- }
1055
- }), g;
1056
- function verb(n) {
1057
- return function(v) {
1058
- return step([
1059
- n,
1060
- v
1061
- ]);
1062
- };
1063
- }
1064
- function step(op) {
1065
- if (f) throw new TypeError("Generator is already executing.");
1066
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
1067
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
1068
- if (y = 0, t) op = [
1069
- op[0] & 2,
1070
- t.value
1071
- ];
1072
- switch(op[0]){
1073
- case 0:
1074
- case 1:
1075
- t = op;
1076
- break;
1077
- case 4:
1078
- _.label++;
1079
- return {
1080
- value: op[1],
1081
- done: false
1082
- };
1083
- case 5:
1084
- _.label++;
1085
- y = op[1];
1086
- op = [
1087
- 0
1088
- ];
1089
- continue;
1090
- case 7:
1091
- op = _.ops.pop();
1092
- _.trys.pop();
1093
- continue;
1094
- default:
1095
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
1096
- _ = 0;
1097
- continue;
1098
- }
1099
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
1100
- _.label = op[1];
1101
- break;
1102
- }
1103
- if (op[0] === 6 && _.label < t[1]) {
1104
- _.label = t[1];
1105
- t = op;
1106
- break;
1107
- }
1108
- if (t && _.label < t[2]) {
1109
- _.label = t[2];
1110
- _.ops.push(op);
1111
- break;
1112
- }
1113
- if (t[2]) _.ops.pop();
1114
- _.trys.pop();
1115
- continue;
1116
- }
1117
- op = body.call(thisArg, _);
1118
- } catch (e) {
1119
- op = [
1120
- 6,
1121
- e
1122
- ];
1123
- y = 0;
1124
- } finally{
1125
- f = t = 0;
1126
- }
1127
- if (op[0] & 5) throw op[1];
1128
- return {
1129
- value: op[0] ? op[1] : void 0,
1130
- done: true
1131
- };
1132
- }
1133
- }
1134
- /**
1135
- * Injectable service that wraps the discord.js Client for bot operations.
1136
- *
1137
- * Automatically logs in on module init and destroys the client on module destroy
1138
- * when autoLogin is enabled (default).
1139
- */ var DiscordApi = /*#__PURE__*/ function() {
1140
- function DiscordApi(config) {
1141
- _class_call_check$1(this, DiscordApi);
1142
- _define_property$1(this, "config", void 0);
1143
- _define_property$1(this, "logger", new Logger('DiscordApi'));
1144
- /**
1145
- * The underlying discord.js Client instance.
1146
- */ _define_property$1(this, "client", void 0);
1147
- this.config = config;
1148
- var clientOptions = config.discord.clientOptions;
1149
- this.client = new Client(_object_spread$1({
1150
- intents: DEFAULT_DISCORD_INTENTS
1151
- }, clientOptions));
1152
- }
1153
- _create_class(DiscordApi, [
1154
- {
1155
- key: "onModuleInit",
1156
- value: function onModuleInit() {
1157
- return _async_to_generator(function() {
1158
- var _this, _this_config_discord, _this_config_discord_autoLogin, autoLogin, botToken, result;
1159
- return _ts_generator(this, function(_state) {
1160
- _this = this;
1161
- _this_config_discord = this.config.discord, _this_config_discord_autoLogin = _this_config_discord.autoLogin, autoLogin = _this_config_discord_autoLogin === void 0 ? true : _this_config_discord_autoLogin, botToken = _this_config_discord.botToken;
1162
- if (autoLogin) {
1163
- result = this.client.login(botToken).then(function() {
1164
- return undefined;
1165
- }).catch(function(e) {
1166
- _this.logger.error('Failed to log in to Discord', e);
1167
- });
1168
- } else {
1169
- result = Promise.resolve();
1170
- }
1171
- return [
1172
- 2,
1173
- result
1174
- ];
1175
- });
1176
- }).call(this);
1177
- }
1178
- },
1179
- {
1180
- key: "onModuleDestroy",
1181
- value: function onModuleDestroy() {
1182
- return _async_to_generator(function() {
1183
- return _ts_generator(this, function(_state) {
1184
- return [
1185
- 2,
1186
- this.client.destroy()
1187
- ];
1188
- });
1189
- }).call(this);
1190
- }
1191
- },
1192
- {
1193
- key: "sendMessage",
1194
- value: /**
1195
- * Sends a text message to a Discord channel.
1196
- *
1197
- * @param channelId - target channel's snowflake ID
1198
- * @param content - message text to send
1199
- *
1200
- * @throws {Error} When the channel is not found or is not a text channel.
1201
- *
1202
- * @example
1203
- * ```ts
1204
- * const message = await discordApi.sendMessage('123456789', 'Hello from the bot!');
1205
- * ```
1206
- */ /**
1207
- * Sends a text message to the specified Discord channel.
1208
- *
1209
- * @param channelId - Target channel's snowflake ID.
1210
- * @param content - Message text to send.
1211
- * @returns The sent Discord Message.
1212
- * @throws {Error} When the channel is not found or is not a text channel.
1213
- */ function sendMessage(channelId, content) {
1214
- return _async_to_generator(function() {
1215
- var channel;
1216
- return _ts_generator(this, function(_state) {
1217
- switch(_state.label){
1218
- case 0:
1219
- return [
1220
- 4,
1221
- this.client.channels.fetch(channelId)
1222
- ];
1223
- case 1:
1224
- channel = _state.sent();
1225
- if (!channel || !_instanceof(channel, TextChannel)) {
1226
- throw new Error("Channel ".concat(channelId, " not found or is not a text channel."));
1227
- }
1228
- return [
1229
- 2,
1230
- channel.send(content)
1231
- ];
1232
- }
1233
- });
1234
- }).call(this);
1235
- }
1236
- },
1237
- {
1238
- /**
1239
- * Registers a handler for the MessageCreate event (incoming messages).
1240
- *
1241
- * Returns an unsubscribe function to remove the handler.
1242
- *
1243
- * @param handler - callback invoked for each incoming message
1244
- *
1245
- * @example
1246
- * ```ts
1247
- * const unsubscribe = discordApi.onMessage((message) => {
1248
- * if (!message.author.bot) {
1249
- * console.log(`${message.author.tag}: ${message.content}`);
1250
- * }
1251
- * });
1252
- *
1253
- * // Later, to stop listening:
1254
- * unsubscribe();
1255
- * ```
1256
- */ /**
1257
- * Registers a handler for incoming Discord messages (MessageCreate event).
1258
- *
1259
- * @param handler - Callback invoked for each incoming Message.
1260
- * @returns An unsubscribe function that removes the registered handler.
1261
- */ key: "onMessage",
1262
- value: function onMessage(handler) {
1263
- var _this = this;
1264
- this.client.on(Events.MessageCreate, handler);
1265
- return function() {
1266
- return _this.client.off(Events.MessageCreate, handler);
1267
- };
1268
- }
1269
- }
1270
- ]);
1271
- return DiscordApi;
1272
- }();
1273
- DiscordApi = __decorate([
1274
- Injectable(),
1275
- __param(0, Inject(DiscordServiceConfig))
1276
- ], DiscordApi);
1277
-
1278
- function _class_call_check(instance, Constructor) {
1279
- if (!(instance instanceof Constructor)) {
1280
- throw new TypeError("Cannot call a class as a function");
1281
- }
1282
- }
1283
- /**
1284
- * Factory that creates a DiscordServiceConfig from environment variables.
1285
- *
1286
- * autoLogin is enabled only when a real bot token is configured and the process is not running
1287
- * under a test environment, so development and CI runs (which use a placeholder token) never
1288
- * attempt a real gateway login that would fail.
1289
- *
1290
- * @param configService - The NestJS config service used to read Discord environment variables.
1291
- * @returns A validated DiscordServiceConfig populated from environment variables.
1292
- */ function discordServiceConfigFactory(configService) {
1293
- var botToken = configService.get(DISCORD_BOT_TOKEN_ENV_VAR);
1294
- var config = {
1295
- discord: {
1296
- botToken: botToken,
1297
- autoLogin: isUsableDiscordBotToken(botToken) && !isTestNodeEnv()
1298
- }
1299
- };
1300
- DiscordServiceConfig.assertValidConfig(config);
1301
- return config;
1302
- }
1303
- /**
1304
- * NestJS module that provides the DiscordApi service.
1305
- *
1306
- * Reads the bot token from the DISCORD_BOT_TOKEN environment variable.
1307
- */ var DiscordModule = function DiscordModule() {
1308
- _class_call_check(this, DiscordModule);
1309
- };
1310
- DiscordModule = __decorate([
1311
- Module({
1312
- imports: [
1313
- ConfigModule
1314
- ],
1315
- providers: [
1316
- {
1317
- provide: DiscordServiceConfig,
1318
- inject: [
1319
- ConfigService
1320
- ],
1321
- useFactory: discordServiceConfigFactory
1322
- },
1323
- DiscordApi
1324
- ],
1325
- exports: [
1326
- DiscordApi
1327
- ]
1328
- })
1329
- ], DiscordModule);
1330
-
1331
- function _define_property(obj, key, value) {
1332
- if (key in obj) {
1333
- Object.defineProperty(obj, key, {
1334
- value: value,
1335
- enumerable: true,
1336
- configurable: true,
1337
- writable: true
1338
- });
1339
- } else {
1340
- obj[key] = value;
1341
- }
1342
- return obj;
1343
- }
1344
- function _object_spread(target) {
1345
- for(var i = 1; i < arguments.length; i++){
1346
- var source = arguments[i] != null ? arguments[i] : {};
1347
- var ownKeys = Object.keys(source);
1348
- if (typeof Object.getOwnPropertySymbols === "function") {
1349
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
1350
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
1351
- }));
1352
- }
1353
- ownKeys.forEach(function(key) {
1354
- _define_property(target, key, source[key]);
1355
- });
1356
- }
1357
- return target;
1358
- }
1359
- function ownKeys(object, enumerableOnly) {
1360
- var keys = Object.keys(object);
1361
- if (Object.getOwnPropertySymbols) {
1362
- var symbols = Object.getOwnPropertySymbols(object);
1363
- keys.push.apply(keys, symbols);
1364
- }
1365
- return keys;
1366
- }
1367
- function _object_spread_props(target, source) {
1368
- source = source != null ? source : {};
1369
- if (Object.getOwnPropertyDescriptors) {
1370
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
1371
- } else {
1372
- ownKeys(Object(source)).forEach(function(key) {
1373
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
1374
- });
1375
- }
1376
- return target;
1377
- }
1378
- /**
1379
- * Default number of messages per page when fetching Discord channel messages.
1380
- */ var DEFAULT_DISCORD_MESSAGES_PER_PAGE = 100;
1381
- /**
1382
- * Creates a page factory that wraps a Discord message fetch function with automatic cursor-based pagination.
1383
- *
1384
- * Discord paginates via `before`/`after` snowflake IDs. This factory automatically reads the last
1385
- * message's ID from each response and sets it as the `before` cursor for the next request.
1386
- * When the number of returned messages is less than the requested limit, pagination stops.
1387
- *
1388
- * @param input - The factory input configuration.
1389
- * @returns A page factory that produces iterable page fetchers.
1390
- *
1391
- * @example
1392
- * ```typescript
1393
- * const pageFactory = discordFetchMessagePageFactory({ fetch: fetchChannelMessages });
1394
- *
1395
- * const fetchPage = pageFactory({ limit: 50 });
1396
- * const firstPage = await fetchPage.fetchNext();
1397
- *
1398
- * if (firstPage.hasNext) {
1399
- * const secondPage = await firstPage.fetchNext();
1400
- * }
1401
- * ```
1402
- */ function discordFetchMessagePageFactory(input) {
1403
- var _ref;
1404
- var fetch = input.fetch, config = input.config, defaults = input.defaults;
1405
- var readMessageId = (_ref = config === null || config === void 0 ? void 0 : config.readMessageId) !== null && _ref !== void 0 ? _ref : function(message) {
1406
- return message.id;
1407
- };
1408
- return fetchPageFactory(_object_spread_props(_object_spread({}, defaults), {
1409
- fetch: fetch,
1410
- readFetchPageResultInfo: function readFetchPageResultInfo(result) {
1411
- var count = result.data.length;
1412
- var nextCursor = count > 0 ? readMessageId(lastValue(result.data)) : undefined;
1413
- return {
1414
- hasNext: count > 0,
1415
- nextPageCursor: nextCursor
1416
- };
1417
- },
1418
- buildInputForNextPage: function buildInputForNextPage(pageResult, input, options) {
1419
- var _ref, _options_maxItemsPerPage, _ref1;
1420
- var _pageResult_result;
1421
- var nextCursor = pageResult.nextPageCursor;
1422
- var effectiveLimit = (_ref = (_options_maxItemsPerPage = options.maxItemsPerPage) !== null && _options_maxItemsPerPage !== void 0 ? _options_maxItemsPerPage : input.limit) !== null && _ref !== void 0 ? _ref : DEFAULT_DISCORD_MESSAGES_PER_PAGE;
1423
- var resultCount = (_ref1 = (_pageResult_result = pageResult.result) === null || _pageResult_result === void 0 ? void 0 : _pageResult_result.data.length) !== null && _ref1 !== void 0 ? _ref1 : 0;
1424
- var nextInput;
1425
- // Discord signals no more results when fewer items than the limit are returned
1426
- if (!nextCursor || resultCount < effectiveLimit) {
1427
- nextInput = undefined;
1428
- } else {
1429
- nextInput = _object_spread_props(_object_spread({}, input), {
1430
- before: nextCursor,
1431
- after: undefined,
1432
- around: undefined,
1433
- limit: effectiveLimit
1434
- });
1435
- }
1436
- return nextInput;
1437
- }
1438
- }));
1439
- }
1440
-
1441
- function _array_like_to_array(arr, len) {
1442
- if (len == null || len > arr.length) len = arr.length;
1443
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
1444
- return arr2;
1445
- }
1446
- function _array_without_holes(arr) {
1447
- if (Array.isArray(arr)) return _array_like_to_array(arr);
1448
- }
1449
- function _iterable_to_array(iter) {
1450
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
1451
- }
1452
- function _non_iterable_spread() {
1453
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1454
- }
1455
- function _to_consumable_array(arr) {
1456
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
1457
- }
1458
- function _unsupported_iterable_to_array(o, minLen) {
1459
- if (!o) return;
1460
- if (typeof o === "string") return _array_like_to_array(o, minLen);
1461
- var n = Object.prototype.toString.call(o).slice(8, -1);
1462
- if (n === "Object" && o.constructor) n = o.constructor.name;
1463
- if (n === "Map" || n === "Set") return Array.from(n);
1464
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
1465
- }
1466
- /**
1467
- * Returns default ClientOptions for a bot that reads guild messages.
1468
- *
1469
- * Includes Guilds, GuildMessages, and MessageContent intents.
1470
- *
1471
- * @returns Partial ClientOptions with the default bot intents set.
1472
- *
1473
- * @example
1474
- * ```ts
1475
- * const options = discordDefaultClientOptions();
1476
- * // options.intents === [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
1477
- * ```
1478
- */ function discordDefaultClientOptions() {
1479
- return {
1480
- intents: DEFAULT_DISCORD_INTENTS
1481
- };
1482
- }
1483
- /**
1484
- * Returns ClientOptions with additional intents merged with the defaults.
1485
- *
1486
- * @param additionalIntents - Extra intents to include beyond the defaults.
1487
- * @returns Partial ClientOptions with the merged intent list.
1488
- *
1489
- * @example
1490
- * ```ts
1491
- * const options = discordClientOptionsWithIntents([GatewayIntentBits.DirectMessages]);
1492
- * // options.intents includes Guilds, GuildMessages, MessageContent, and DirectMessages
1493
- * ```
1494
- */ function discordClientOptionsWithIntents(additionalIntents) {
1495
- return {
1496
- intents: _to_consumable_array(DEFAULT_DISCORD_INTENTS).concat(_to_consumable_array(additionalIntents))
1497
- };
1498
- }
1499
-
1500
- export { DEFAULT_DISCORD_INTENTS, DEFAULT_DISCORD_MESSAGES_PER_PAGE, DISCORD_BOT_TOKEN_ENV_VAR, DISCORD_BOT_TOKEN_PLACEHOLDER, DISCORD_ED25519_PUBLIC_KEY_BYTE_LENGTH, DISCORD_PUBLIC_KEY_ENV_VAR, DiscordApi, DiscordModule, DiscordServiceConfig, DiscordWebhookController, DiscordWebhookModule, DiscordWebhookService, DiscordWebhookServiceConfig, discordClientOptionsWithIntents, discordDefaultClientOptions, discordFetchMessagePageFactory, discordInteractionHandlerConfigurerFactory, discordInteractionHandlerFactory, discordServiceConfigFactory, discordWebhookEventVerifier, discordWebhookInteraction, discordWebhookServiceConfigFactory, isUsableDiscordBotToken };