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