@spatulox/simplediscordbot 3.2.0 → 4.0.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.
- package/CHANGELOG.md +43 -0
- package/dist/index.d.mts +193 -57
- package/dist/index.d.ts +193 -57
- package/dist/index.js +1023 -931
- package/dist/index.mjs +959 -858
- package/package.json +21 -7
- package/.env.example +0 -2
- package/prompt-message-for-wiki.txt +0 -18
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
// src/core/Bot.ts
|
|
2
|
-
import { Events, version } from "discord.js";
|
|
2
|
+
import { Events, version as version2 } from "discord.js";
|
|
3
3
|
import { Time } from "@spatulox/utils";
|
|
4
4
|
import { Log as Log3 } from "@spatulox/utils";
|
|
5
5
|
import { InternetChecker } from "@spatulox/utils";
|
|
6
6
|
|
|
7
7
|
// src/core/BotLog.ts
|
|
8
8
|
import {
|
|
9
|
-
EmbedBuilder
|
|
9
|
+
EmbedBuilder as EmbedBuilder2
|
|
10
|
+
} from "discord.js";
|
|
11
|
+
import { Log } from "@spatulox/utils";
|
|
12
|
+
|
|
13
|
+
// src/manager/builder/SendableComponentBuilder.ts
|
|
14
|
+
import {
|
|
10
15
|
ActionRowBuilder as ActionRowBuilder2,
|
|
16
|
+
EmbedBuilder,
|
|
17
|
+
MessageFlags as MessageFlags2,
|
|
11
18
|
ContainerBuilder,
|
|
12
|
-
|
|
19
|
+
TextDisplayBuilder,
|
|
20
|
+
BaseSelectMenuBuilder
|
|
13
21
|
} from "discord.js";
|
|
14
|
-
import { Log } from "@spatulox/utils";
|
|
15
22
|
|
|
16
23
|
// src/manager/interactible/SelectMenuManager.ts
|
|
17
24
|
import {
|
|
@@ -131,11 +138,20 @@ var SelectMenuManager = class _SelectMenuManager {
|
|
|
131
138
|
flags: ephemeral ? [MessageFlags.Ephemeral] : []
|
|
132
139
|
};
|
|
133
140
|
}
|
|
134
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Options to edit an existing message : message.edit()
|
|
143
|
+
*/
|
|
144
|
+
static toMessageUpdate(menus) {
|
|
135
145
|
return {
|
|
136
146
|
components: this._createRowsToReturn(menus)
|
|
137
147
|
};
|
|
138
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Options for interaction.update() and interaction.editReply() (buttons, select menus, modals, deferred replies)
|
|
151
|
+
*/
|
|
152
|
+
static toInteractionUpdate(menus) {
|
|
153
|
+
return this.toMessageUpdate(menus);
|
|
154
|
+
}
|
|
139
155
|
static _createRowsToReturn(menus) {
|
|
140
156
|
if (Array.isArray(menus)) {
|
|
141
157
|
return menus.map(
|
|
@@ -146,6 +162,65 @@ var SelectMenuManager = class _SelectMenuManager {
|
|
|
146
162
|
}
|
|
147
163
|
};
|
|
148
164
|
|
|
165
|
+
// src/manager/builder/SendableComponentBuilder.ts
|
|
166
|
+
var SendableComponentBuilder = class {
|
|
167
|
+
static isSendableComponent(thing) {
|
|
168
|
+
return thing instanceof EmbedBuilder || thing instanceof ContainerBuilder || thing instanceof BaseSelectMenuBuilder || thing instanceof ActionRowBuilder2;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A ContainerBuilder turns the whole message into a Components V2 one
|
|
172
|
+
*/
|
|
173
|
+
static isComponentsV2(components) {
|
|
174
|
+
const comps = Array.isArray(components) ? components : [components];
|
|
175
|
+
return comps.some((comp) => comp instanceof ContainerBuilder);
|
|
176
|
+
}
|
|
177
|
+
static build(base, content, components) {
|
|
178
|
+
const comps = (Array.isArray(components) ? components : [components]).filter((comp) => !!comp);
|
|
179
|
+
const componentsV2 = this.isComponentsV2(comps);
|
|
180
|
+
const embeds = [];
|
|
181
|
+
const rows = [];
|
|
182
|
+
if (content) {
|
|
183
|
+
if (componentsV2) {
|
|
184
|
+
rows.push(new TextDisplayBuilder().setContent(content));
|
|
185
|
+
} else {
|
|
186
|
+
base.content = content;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (const comp of comps) {
|
|
190
|
+
if (comp instanceof EmbedBuilder) {
|
|
191
|
+
if (componentsV2) {
|
|
192
|
+
throw new Error("An EmbedBuilder cannot be sent alongside a ContainerBuilder (Components V2 messages don't support embeds)");
|
|
193
|
+
}
|
|
194
|
+
embeds.push(comp);
|
|
195
|
+
} else if (SelectMenuManager.isSelectMenuList(comp)) {
|
|
196
|
+
rows.push(SelectMenuManager.row(comp));
|
|
197
|
+
} else if (comp instanceof ContainerBuilder || comp instanceof ActionRowBuilder2) {
|
|
198
|
+
rows.push(comp);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (embeds.length > 0) base.embeds = embeds;
|
|
202
|
+
if (rows.length > 0) base.components = rows;
|
|
203
|
+
if (componentsV2) base.flags = [MessageFlags2.IsComponentsV2];
|
|
204
|
+
return base;
|
|
205
|
+
}
|
|
206
|
+
static buildInteraction(content, component, ephemeral) {
|
|
207
|
+
const base = this.build({}, content, component);
|
|
208
|
+
if (ephemeral) {
|
|
209
|
+
base.flags = [...Array.isArray(base.flags) ? base.flags : [], MessageFlags2.Ephemeral];
|
|
210
|
+
}
|
|
211
|
+
return base;
|
|
212
|
+
}
|
|
213
|
+
static buildMessage(contentOrComponent, component) {
|
|
214
|
+
const content = typeof contentOrComponent === "string" ? contentOrComponent : null;
|
|
215
|
+
const components = typeof contentOrComponent === "string" || contentOrComponent === null || contentOrComponent === void 0 ? component : contentOrComponent;
|
|
216
|
+
const hasComponents = Array.isArray(components) ? components.length > 0 : !!components;
|
|
217
|
+
if (!content && !hasComponents) {
|
|
218
|
+
throw new Error("At least content or component need to be defined to build a message");
|
|
219
|
+
}
|
|
220
|
+
return this.build({}, content, components);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
149
224
|
// src/core/BotEnv.ts
|
|
150
225
|
var BotEnv = {
|
|
151
226
|
get token() {
|
|
@@ -154,7 +229,9 @@ var BotEnv = {
|
|
|
154
229
|
return token;
|
|
155
230
|
},
|
|
156
231
|
get dev() {
|
|
157
|
-
|
|
232
|
+
const dev = process.env.DISCORD_BOT_DEV;
|
|
233
|
+
if (dev === void 0) return false;
|
|
234
|
+
return !["", "false", "0", "no", "off"].includes(dev.trim().toLowerCase());
|
|
158
235
|
}
|
|
159
236
|
};
|
|
160
237
|
|
|
@@ -164,10 +241,8 @@ var BotLog = class _BotLog {
|
|
|
164
241
|
static warnChannel = null;
|
|
165
242
|
static debugChannel = null;
|
|
166
243
|
static errorChannel = null;
|
|
167
|
-
constructor() {
|
|
168
|
-
}
|
|
169
244
|
static config() {
|
|
170
|
-
return Bot.config
|
|
245
|
+
return Bot.config?.log;
|
|
171
246
|
}
|
|
172
247
|
/**
|
|
173
248
|
* Initialize Discord logging channels and update Bot.log references
|
|
@@ -198,91 +273,28 @@ var BotLog = class _BotLog {
|
|
|
198
273
|
}
|
|
199
274
|
}
|
|
200
275
|
}
|
|
201
|
-
/*public static async initDiscordLogging(): Promise<void> {
|
|
202
|
-
if (!Bot.client.isReady()) {
|
|
203
|
-
Log.warn('Client not ready for Discord logging init');
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (Bot.config.log?.info.channelId) {
|
|
208
|
-
try {
|
|
209
|
-
const logCh = await Bot.client.channels.fetch(Bot.config.log.info.channelId) as TextChannel;
|
|
210
|
-
if (logCh?.isTextBased()) {
|
|
211
|
-
BotLog.logChannel = logCh;
|
|
212
|
-
} else {
|
|
213
|
-
Log.warn(`Log channel ${Bot.config.log.info.channelId} invalid`);
|
|
214
|
-
}
|
|
215
|
-
} catch (error) {
|
|
216
|
-
Log.error(`Log channel fetch failed: ${error}`);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (Bot.config.log?.warn.channelId) {
|
|
221
|
-
try {
|
|
222
|
-
const errorCh = await Bot.client.channels.fetch(Bot.config.log.warn.channelId) as TextChannel;
|
|
223
|
-
if (errorCh?.isTextBased()) {
|
|
224
|
-
BotLog.warnChannel = errorCh;
|
|
225
|
-
} else {
|
|
226
|
-
Log.warn(`Warn channel ${Bot.config.log.warn.channelId} invalid`);
|
|
227
|
-
}
|
|
228
|
-
} catch (error) {
|
|
229
|
-
Log.error(`Warn channel fetch failed: ${error}`);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (Bot.config.log?.error.channelId) {
|
|
234
|
-
try {
|
|
235
|
-
const errorCh = await Bot.client.channels.fetch(Bot.config.log.error.channelId) as TextChannel;
|
|
236
|
-
if (errorCh?.isTextBased()) {
|
|
237
|
-
BotLog.errorChannel = errorCh;
|
|
238
|
-
} else {
|
|
239
|
-
Log.warn(`Error channel ${Bot.config.log.error.channelId} invalid`);
|
|
240
|
-
}
|
|
241
|
-
} catch (error) {
|
|
242
|
-
Log.error(`Error channel fetch failed: ${error}`);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (Bot.config.log?.debug.channelId) {
|
|
247
|
-
try {
|
|
248
|
-
const errorCh = await Bot.client.channels.fetch(Bot.config.log.debug.channelId) as TextChannel;
|
|
249
|
-
if (errorCh?.isTextBased()) {
|
|
250
|
-
BotLog.debugChannel = errorCh;
|
|
251
|
-
} else {
|
|
252
|
-
Log.warn(`Debug channel ${Bot.config.log.debug.channelId} invalid`);
|
|
253
|
-
}
|
|
254
|
-
} catch (error) {
|
|
255
|
-
Log.error(`Debug channel fetch failed: ${error}`);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}*/
|
|
259
276
|
/**
|
|
260
277
|
* Send content to specific Discord channel
|
|
261
278
|
*/
|
|
262
279
|
static async _sendToChannel(channel, content, prefix = "info") {
|
|
263
280
|
if (!channel) return;
|
|
264
|
-
let msg;
|
|
265
281
|
try {
|
|
266
|
-
if (content
|
|
267
|
-
const text = content.data.description ?? content.data.title;
|
|
268
|
-
if (text) {
|
|
269
|
-
Log.info(text);
|
|
270
|
-
}
|
|
271
|
-
msg = await channel.send({ embeds: [content] });
|
|
272
|
-
} else if (SelectMenuManager.isSelectMenuList(content)) {
|
|
273
|
-
msg = await channel.send({ components: SelectMenuManager.rows(content) });
|
|
274
|
-
} else if (content instanceof ContainerBuilder) {
|
|
275
|
-
msg = await channel.send({ components: [content], flags: MessageFlags2.IsComponentsV2 });
|
|
276
|
-
} else if (content instanceof ActionRowBuilder2) {
|
|
277
|
-
msg = await channel.send({ components: [content] });
|
|
278
|
-
} else {
|
|
282
|
+
if (typeof content === "string") {
|
|
279
283
|
const timestamp = `\`${(/* @__PURE__ */ new Date()).toISOString()}\``;
|
|
280
|
-
|
|
284
|
+
return await channel.send(`[${timestamp}] [${prefix.toUpperCase()}] ${content}`);
|
|
281
285
|
}
|
|
286
|
+
return await channel.send(SendableComponentBuilder.buildMessage(content));
|
|
282
287
|
} catch (error) {
|
|
283
288
|
Log.error(`Failed to send to Discord channel: ${error}`);
|
|
284
289
|
}
|
|
285
|
-
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Text printed in the console : the string itself, or the description / title of an embed
|
|
293
|
+
*/
|
|
294
|
+
static _consoleText(content) {
|
|
295
|
+
if (typeof content === "string") return content;
|
|
296
|
+
if (content instanceof EmbedBuilder2) return content.data.description ?? content.data.title ?? null;
|
|
297
|
+
return null;
|
|
286
298
|
}
|
|
287
299
|
/**
|
|
288
300
|
* Send INFO log - TEXT or EMBED ! Respecte config.log.info
|
|
@@ -290,8 +302,9 @@ var BotLog = class _BotLog {
|
|
|
290
302
|
static async info(content) {
|
|
291
303
|
const logConfig = Bot.config?.log;
|
|
292
304
|
if (!logConfig || logConfig.info.console) {
|
|
293
|
-
|
|
294
|
-
|
|
305
|
+
const text = this._consoleText(content);
|
|
306
|
+
if (text) {
|
|
307
|
+
Log.info(text);
|
|
295
308
|
}
|
|
296
309
|
}
|
|
297
310
|
if (logConfig?.info.discord && this.logChannel) {
|
|
@@ -302,10 +315,11 @@ var BotLog = class _BotLog {
|
|
|
302
315
|
* Send ERROR log - TEXT or EMBED ! Respecte config.log.error
|
|
303
316
|
*/
|
|
304
317
|
static async error(content) {
|
|
305
|
-
const logConfig = Bot.config
|
|
318
|
+
const logConfig = Bot.config?.log;
|
|
306
319
|
if (!logConfig || logConfig.error.console) {
|
|
307
|
-
|
|
308
|
-
|
|
320
|
+
const text = this._consoleText(content);
|
|
321
|
+
if (text) {
|
|
322
|
+
Log.error(text);
|
|
309
323
|
}
|
|
310
324
|
}
|
|
311
325
|
if (logConfig?.error.discord && this.errorChannel) {
|
|
@@ -316,10 +330,11 @@ var BotLog = class _BotLog {
|
|
|
316
330
|
* Send WARNING log - TEXT or EMBED ! Respecte config.log.warn
|
|
317
331
|
*/
|
|
318
332
|
static async warn(content) {
|
|
319
|
-
const logConfig = Bot.config
|
|
333
|
+
const logConfig = Bot.config?.log;
|
|
320
334
|
if (!logConfig || logConfig?.warn.console) {
|
|
321
|
-
|
|
322
|
-
|
|
335
|
+
const text = this._consoleText(content);
|
|
336
|
+
if (text) {
|
|
337
|
+
Log.warn(text);
|
|
323
338
|
}
|
|
324
339
|
}
|
|
325
340
|
if (logConfig?.warn.discord && this.warnChannel) {
|
|
@@ -331,10 +346,11 @@ var BotLog = class _BotLog {
|
|
|
331
346
|
*/
|
|
332
347
|
static async debug(content) {
|
|
333
348
|
if (!BotEnv.dev) return;
|
|
334
|
-
const logConfig = Bot.config
|
|
349
|
+
const logConfig = Bot.config?.log;
|
|
335
350
|
if (!logConfig || logConfig?.debug.console) {
|
|
336
|
-
|
|
337
|
-
|
|
351
|
+
const text = this._consoleText(content);
|
|
352
|
+
if (text) {
|
|
353
|
+
Log.debug(text);
|
|
338
354
|
}
|
|
339
355
|
}
|
|
340
356
|
if (logConfig?.debug.discord && this.debugChannel) {
|
|
@@ -345,7 +361,7 @@ var BotLog = class _BotLog {
|
|
|
345
361
|
|
|
346
362
|
// src/manager/messages/EmbedManager.ts
|
|
347
363
|
import {
|
|
348
|
-
EmbedBuilder as
|
|
364
|
+
EmbedBuilder as EmbedBuilder3,
|
|
349
365
|
MessageFlags as MessageFlags3
|
|
350
366
|
} from "discord.js";
|
|
351
367
|
|
|
@@ -397,13 +413,13 @@ var EmbedManager = class {
|
|
|
397
413
|
return Bot.client?.user?.displayAvatarURL({ forceStatic: false, size: 128 }) || "";
|
|
398
414
|
}
|
|
399
415
|
static get DEFAULT_COLOR() {
|
|
400
|
-
return Bot.config
|
|
416
|
+
return Bot.config?.defaultSimpleColor ?? 6064856 /* default */;
|
|
401
417
|
}
|
|
402
418
|
/**
|
|
403
419
|
* Creates base embed - SAME SIMPLE API !
|
|
404
420
|
*/
|
|
405
421
|
static create(color = null) {
|
|
406
|
-
const embed = new
|
|
422
|
+
const embed = new EmbedBuilder3().setTimestamp(/* @__PURE__ */ new Date());
|
|
407
423
|
const colorC = color ?? this.DEFAULT_COLOR;
|
|
408
424
|
if (colorC !== "transparent" /* transparent */) {
|
|
409
425
|
embed.setColor(colorC);
|
|
@@ -422,32 +438,32 @@ var EmbedManager = class {
|
|
|
422
438
|
/**
|
|
423
439
|
* Creates simple embed with just description
|
|
424
440
|
*/
|
|
425
|
-
static simple(
|
|
426
|
-
return this.create(color).setDescription(
|
|
441
|
+
static simple(description2, color = null) {
|
|
442
|
+
return this.create(color).setDescription(description2);
|
|
427
443
|
}
|
|
428
444
|
/**
|
|
429
445
|
* Creates error embed
|
|
430
446
|
*/
|
|
431
|
-
static error(
|
|
432
|
-
return this.create(8912917 /* error */).setTitle("Something went Wrong").setDescription(
|
|
447
|
+
static error(description2) {
|
|
448
|
+
return this.create(8912917 /* error */).setTitle("Something went Wrong").setDescription(description2);
|
|
433
449
|
}
|
|
434
450
|
/**
|
|
435
451
|
* Creates success embed
|
|
436
452
|
*/
|
|
437
|
-
static success(
|
|
438
|
-
return this.create(25600 /* minecraft */).setTitle("Success").setDescription(
|
|
453
|
+
static success(description2) {
|
|
454
|
+
return this.create(25600 /* minecraft */).setTitle("Success").setDescription(description2);
|
|
439
455
|
}
|
|
440
456
|
/**
|
|
441
457
|
* Creates a simple description embed
|
|
442
458
|
*/
|
|
443
|
-
static description(
|
|
444
|
-
return this.create().setDescription(
|
|
459
|
+
static description(description2) {
|
|
460
|
+
return this.create().setDescription(description2).setFooter(null).setTimestamp(null);
|
|
445
461
|
}
|
|
446
462
|
/**
|
|
447
463
|
* Creates debug embed
|
|
448
464
|
*/
|
|
449
|
-
static debug(
|
|
450
|
-
return this.create(65280 /* green */).setTitle("Debug").setDescription(
|
|
465
|
+
static debug(description2) {
|
|
466
|
+
return this.create(65280 /* green */).setTitle("Debug").setDescription(description2);
|
|
451
467
|
}
|
|
452
468
|
/**
|
|
453
469
|
* Defer ephemeral reply
|
|
@@ -477,13 +493,19 @@ var EmbedManager = class {
|
|
|
477
493
|
};
|
|
478
494
|
}
|
|
479
495
|
/**
|
|
480
|
-
*
|
|
496
|
+
* Options to edit an existing message : message.edit()
|
|
481
497
|
*/
|
|
482
|
-
static
|
|
498
|
+
static toMessageUpdate(embed) {
|
|
483
499
|
return {
|
|
484
500
|
embeds: [embed]
|
|
485
501
|
};
|
|
486
502
|
}
|
|
503
|
+
/**
|
|
504
|
+
* Options for interaction.update() and interaction.editReply() (buttons, select menus, modals, deferred replies)
|
|
505
|
+
*/
|
|
506
|
+
static toInteractionUpdate(embed) {
|
|
507
|
+
return this.toMessageUpdate(embed);
|
|
508
|
+
}
|
|
487
509
|
/**
|
|
488
510
|
* Transform embed into objet to send a message
|
|
489
511
|
*/
|
|
@@ -500,72 +522,6 @@ import {
|
|
|
500
522
|
GuildMember
|
|
501
523
|
} from "discord.js";
|
|
502
524
|
import { Log as Log2 } from "@spatulox/utils";
|
|
503
|
-
|
|
504
|
-
// src/manager/builder/SendableComponentBuilder.ts
|
|
505
|
-
import {
|
|
506
|
-
ActionRowBuilder as ActionRowBuilder3,
|
|
507
|
-
EmbedBuilder as EmbedBuilder3,
|
|
508
|
-
MessageFlags as MessageFlags4,
|
|
509
|
-
ContainerBuilder as ContainerBuilder2
|
|
510
|
-
} from "discord.js";
|
|
511
|
-
import { BaseSelectMenuBuilder } from "@discordjs/builders";
|
|
512
|
-
var SendableComponentBuilder = class {
|
|
513
|
-
static isSendableComponent(thing) {
|
|
514
|
-
return thing instanceof EmbedBuilder3 || thing instanceof ContainerBuilder2 || thing instanceof BaseSelectMenuBuilder || thing instanceof ActionRowBuilder3;
|
|
515
|
-
}
|
|
516
|
-
static build(base, content, components) {
|
|
517
|
-
if (content) {
|
|
518
|
-
base.content = content;
|
|
519
|
-
}
|
|
520
|
-
const comps = Array.isArray(components) ? components : [components].filter(Boolean);
|
|
521
|
-
for (const comp of comps) {
|
|
522
|
-
base.embeds = base.embeds || [];
|
|
523
|
-
base.components = base.components || [];
|
|
524
|
-
if (comp instanceof EmbedBuilder3) {
|
|
525
|
-
base.embeds.push(comp);
|
|
526
|
-
}
|
|
527
|
-
if (SelectMenuManager.isSelectMenuList(comp)) {
|
|
528
|
-
base.components.push(SelectMenuManager.row(comp));
|
|
529
|
-
}
|
|
530
|
-
if (comp instanceof ContainerBuilder2) {
|
|
531
|
-
base.components.push(comp);
|
|
532
|
-
}
|
|
533
|
-
if (comp instanceof ActionRowBuilder3) {
|
|
534
|
-
base.components.push(comp);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
return base;
|
|
538
|
-
}
|
|
539
|
-
static buildInteraction(content, component, ephemeral) {
|
|
540
|
-
let base = {};
|
|
541
|
-
this.build(base, content, component);
|
|
542
|
-
if (ephemeral) {
|
|
543
|
-
base.flags = [MessageFlags4.Ephemeral];
|
|
544
|
-
}
|
|
545
|
-
return base;
|
|
546
|
-
}
|
|
547
|
-
static buildMessage(contentOrComponent, component) {
|
|
548
|
-
let base = {};
|
|
549
|
-
if (typeof contentOrComponent === "string" && !component) {
|
|
550
|
-
return this.build(base, contentOrComponent);
|
|
551
|
-
}
|
|
552
|
-
if (typeof contentOrComponent !== "string" && !component) {
|
|
553
|
-
if (contentOrComponent instanceof ContainerBuilder2) {
|
|
554
|
-
base.flags = [MessageFlags4.IsComponentsV2];
|
|
555
|
-
}
|
|
556
|
-
return this.build(base, null, contentOrComponent);
|
|
557
|
-
}
|
|
558
|
-
if (typeof contentOrComponent == "string" && component) {
|
|
559
|
-
if (component instanceof ContainerBuilder2) {
|
|
560
|
-
base.flags = [MessageFlags4.IsComponentsV2];
|
|
561
|
-
}
|
|
562
|
-
return this.build(base, contentOrComponent, component);
|
|
563
|
-
}
|
|
564
|
-
throw new Error("At least content or component need to be defined to build a message");
|
|
565
|
-
}
|
|
566
|
-
};
|
|
567
|
-
|
|
568
|
-
// src/core/BotMessage.ts
|
|
569
525
|
var BotMessage = class {
|
|
570
526
|
static async send(channel, content, component) {
|
|
571
527
|
try {
|
|
@@ -574,42 +530,34 @@ var BotMessage = class {
|
|
|
574
530
|
return null;
|
|
575
531
|
}
|
|
576
532
|
if (typeof channel === "string") {
|
|
577
|
-
const fetchedChannel = Bot.client.channels.cache.get(channel);
|
|
533
|
+
const fetchedChannel = Bot.client.channels.cache.get(channel) ?? await Bot.client.channels.fetch(channel).catch(() => null);
|
|
578
534
|
if (!fetchedChannel?.isTextBased()) {
|
|
579
535
|
Log2.warn(`Cannot send message : Invalid channel ID: ${channel}`);
|
|
580
536
|
return null;
|
|
581
537
|
}
|
|
582
538
|
channel = fetchedChannel;
|
|
583
539
|
}
|
|
584
|
-
|
|
585
|
-
if (typeof content !== "string" && !component) {
|
|
586
|
-
if (SendableComponentBuilder.isSendableComponent(content)) {
|
|
587
|
-
messageCreate = SendableComponentBuilder.buildMessage(content);
|
|
588
|
-
} else {
|
|
589
|
-
messageCreate = content;
|
|
590
|
-
}
|
|
591
|
-
} else {
|
|
592
|
-
content = content;
|
|
593
|
-
if (content && component) {
|
|
594
|
-
messageCreate = SendableComponentBuilder.buildMessage(content, component);
|
|
595
|
-
} else if (content) {
|
|
596
|
-
messageCreate = SendableComponentBuilder.buildMessage(content);
|
|
597
|
-
} else if (component) {
|
|
598
|
-
messageCreate = SendableComponentBuilder.buildMessage(component);
|
|
599
|
-
} else {
|
|
600
|
-
throw new Error("Cannot send message : content and component cannot be null at the same time");
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
try {
|
|
604
|
-
return await channel.send(messageCreate);
|
|
605
|
-
} catch (e) {
|
|
606
|
-
throw e;
|
|
607
|
-
}
|
|
540
|
+
return await channel.send(this.buildPayload(content, component));
|
|
608
541
|
} catch (e) {
|
|
609
542
|
Log2.error(`Cannot send message : ${e}`);
|
|
610
543
|
return null;
|
|
611
544
|
}
|
|
612
545
|
}
|
|
546
|
+
static buildPayload(content, component) {
|
|
547
|
+
if (typeof content === "string") {
|
|
548
|
+
return component ? SendableComponentBuilder.buildMessage(content, component) : SendableComponentBuilder.buildMessage(content);
|
|
549
|
+
}
|
|
550
|
+
if (content === null || content === void 0) {
|
|
551
|
+
if (!component) {
|
|
552
|
+
throw new Error("Cannot send message : content and component cannot be null at the same time");
|
|
553
|
+
}
|
|
554
|
+
return SendableComponentBuilder.buildMessage(component);
|
|
555
|
+
}
|
|
556
|
+
if (SendableComponentBuilder.isSendableComponent(content) || Array.isArray(content)) {
|
|
557
|
+
return SendableComponentBuilder.buildMessage(content);
|
|
558
|
+
}
|
|
559
|
+
return content;
|
|
560
|
+
}
|
|
613
561
|
static async sendDM(userOrId, content, component) {
|
|
614
562
|
try {
|
|
615
563
|
if (!userOrId) {
|
|
@@ -627,17 +575,7 @@ var BotMessage = class {
|
|
|
627
575
|
}
|
|
628
576
|
targetUser = fetchedUser;
|
|
629
577
|
}
|
|
630
|
-
|
|
631
|
-
if (typeof content === "string" && component) {
|
|
632
|
-
messageCreate = SendableComponentBuilder.buildMessage(content, component);
|
|
633
|
-
} else if (typeof content === "string") {
|
|
634
|
-
messageCreate = SendableComponentBuilder.buildMessage(content);
|
|
635
|
-
} else if (SendableComponentBuilder.isSendableComponent(content)) {
|
|
636
|
-
messageCreate = SendableComponentBuilder.buildMessage(content);
|
|
637
|
-
} else {
|
|
638
|
-
messageCreate = content;
|
|
639
|
-
}
|
|
640
|
-
return await targetUser.send(messageCreate);
|
|
578
|
+
return await targetUser.send(this.buildPayload(content, component));
|
|
641
579
|
} catch (error) {
|
|
642
580
|
Log2.error(`Cannot send DM to ${userOrId}: ${error}`);
|
|
643
581
|
return null;
|
|
@@ -649,9 +587,9 @@ var BotMessage = class {
|
|
|
649
587
|
static success(channel, message) {
|
|
650
588
|
const embed = EmbedManager.success(message);
|
|
651
589
|
if (channel instanceof User || channel instanceof GuildMember) {
|
|
652
|
-
return this.sendDM(channel,
|
|
590
|
+
return this.sendDM(channel, embed);
|
|
653
591
|
}
|
|
654
|
-
return this.send(channel,
|
|
592
|
+
return this.send(channel, embed);
|
|
655
593
|
}
|
|
656
594
|
/**
|
|
657
595
|
* Quick error message
|
|
@@ -659,62 +597,64 @@ var BotMessage = class {
|
|
|
659
597
|
static error(channel, message) {
|
|
660
598
|
const embed = EmbedManager.error(message);
|
|
661
599
|
if (channel instanceof User || channel instanceof GuildMember) {
|
|
662
|
-
return this.sendDM(channel,
|
|
600
|
+
return this.sendDM(channel, embed);
|
|
663
601
|
}
|
|
664
|
-
return this.send(channel,
|
|
602
|
+
return this.send(channel, embed);
|
|
665
603
|
}
|
|
666
604
|
};
|
|
667
605
|
|
|
668
606
|
// src/core/BotInteraction.ts
|
|
607
|
+
import {
|
|
608
|
+
MessageFlags as MessageFlags4
|
|
609
|
+
} from "discord.js";
|
|
669
610
|
var BotInteraction = class {
|
|
670
611
|
/**
|
|
671
612
|
* InteractionReplyOptions && InteractionUpdateOptions
|
|
672
613
|
* The two have "content", "embeds" & "flags" field, so an internal cast is ok, unless discord/discordjs deprecate it
|
|
673
614
|
*/
|
|
674
615
|
static buildReplyOptions(content, component, ephemeral) {
|
|
675
|
-
return
|
|
616
|
+
return SendableComponentBuilder.buildInteraction(content, component, ephemeral);
|
|
676
617
|
}
|
|
677
618
|
static buildUpdateOptions(content, component) {
|
|
678
|
-
return
|
|
619
|
+
return SendableComponentBuilder.buildInteraction(content, component, false);
|
|
679
620
|
}
|
|
680
|
-
|
|
681
|
-
|
|
621
|
+
/**
|
|
622
|
+
* Normalize the (content, component, ephemeral) overloads :
|
|
623
|
+
* (string, ephemeral?) | (component, ephemeral?) | (string, component, ephemeral?)
|
|
624
|
+
*/
|
|
625
|
+
static resolveArgs(content, component, ephemeral) {
|
|
626
|
+
if (typeof content === "string") {
|
|
627
|
+
if (typeof component === "boolean") {
|
|
628
|
+
return this.buildReplyOptions(content, null, component);
|
|
629
|
+
}
|
|
630
|
+
return this.buildReplyOptions(content, component ?? null, ephemeral);
|
|
631
|
+
}
|
|
632
|
+
return this.buildReplyOptions(null, content, typeof component === "boolean" ? component : ephemeral);
|
|
682
633
|
}
|
|
683
|
-
static async send(interaction, content, component
|
|
634
|
+
static async send(interaction, content, component, ephemeral = false) {
|
|
684
635
|
if (!interaction.isRepliable()) return false;
|
|
685
|
-
const options = this.
|
|
686
|
-
typeof content === "string" ? content : null,
|
|
687
|
-
typeof content === "string" ? component : content,
|
|
688
|
-
typeof content === "string" ? ephemeral : component
|
|
689
|
-
);
|
|
636
|
+
const options = this.resolveArgs(content, component, ephemeral);
|
|
690
637
|
if (!interaction.deferred && !interaction.replied) {
|
|
691
638
|
return await interaction.reply(options);
|
|
692
639
|
} else {
|
|
693
640
|
return await interaction.followUp(options);
|
|
694
641
|
}
|
|
695
642
|
}
|
|
696
|
-
static async reply(interaction, content, component
|
|
643
|
+
static async reply(interaction, content, component, ephemeral = false) {
|
|
697
644
|
if (!interaction.isRepliable()) return false;
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
);
|
|
703
|
-
return await interaction.reply(options);
|
|
704
|
-
}
|
|
705
|
-
static async followUp(interaction, content, component = false, ephemeral = false) {
|
|
706
|
-
if (!interaction.isMessageComponent()) return false;
|
|
707
|
-
const options = this.buildReplyOptions(
|
|
708
|
-
typeof content === "string" ? content : "",
|
|
709
|
-
typeof content === "string" ? component : content,
|
|
710
|
-
typeof content === "string" ? ephemeral : component
|
|
711
|
-
);
|
|
712
|
-
return await interaction.followUp(options);
|
|
645
|
+
return await interaction.reply(this.resolveArgs(content, component, ephemeral));
|
|
646
|
+
}
|
|
647
|
+
static async followUp(interaction, content, component, ephemeral = false) {
|
|
648
|
+
if (!interaction.isRepliable()) return false;
|
|
649
|
+
return await interaction.followUp(this.resolveArgs(content, component, ephemeral));
|
|
713
650
|
}
|
|
714
|
-
|
|
651
|
+
/**
|
|
652
|
+
* @param ephemeral Only used by commands : components and modals defer the message they come from
|
|
653
|
+
*/
|
|
654
|
+
static async defer(interaction, ephemeral = false) {
|
|
715
655
|
if (interaction.isChatInputCommand() || interaction.isContextMenuCommand()) {
|
|
716
656
|
if (!interaction.deferred && !interaction.replied) {
|
|
717
|
-
return await interaction.deferReply();
|
|
657
|
+
return await interaction.deferReply(ephemeral ? { flags: MessageFlags4.Ephemeral } : void 0);
|
|
718
658
|
}
|
|
719
659
|
return;
|
|
720
660
|
}
|
|
@@ -726,81 +666,37 @@ var BotInteraction = class {
|
|
|
726
666
|
}
|
|
727
667
|
static async update(interaction, content, component) {
|
|
728
668
|
const options = this.buildUpdateOptions(
|
|
729
|
-
typeof content === "string" ? content :
|
|
730
|
-
typeof content === "string" ? component : content
|
|
669
|
+
typeof content === "string" ? content : null,
|
|
670
|
+
typeof content === "string" ? component ?? null : content
|
|
731
671
|
);
|
|
672
|
+
if (!interaction.isRepliable()) return false;
|
|
673
|
+
if (interaction.deferred || interaction.replied) {
|
|
674
|
+
return await interaction.editReply(options);
|
|
675
|
+
}
|
|
732
676
|
if (interaction.isMessageComponent()) {
|
|
733
677
|
return await interaction.update(options);
|
|
734
678
|
}
|
|
735
|
-
if (
|
|
736
|
-
|
|
737
|
-
return await interaction.editReply(options);
|
|
679
|
+
if (interaction.isModalSubmit() && interaction.isFromMessage()) {
|
|
680
|
+
return await interaction.update(options);
|
|
738
681
|
}
|
|
739
682
|
return false;
|
|
740
683
|
}
|
|
741
684
|
};
|
|
742
685
|
|
|
743
686
|
// package.json
|
|
744
|
-
var
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
exports: {
|
|
750
|
-
types: "./dist/index.d.ts",
|
|
751
|
-
import: "./dist/index.js",
|
|
752
|
-
require: "./dist/index.js"
|
|
753
|
-
},
|
|
754
|
-
types: "./dist/index.d.ts",
|
|
755
|
-
scripts: {
|
|
756
|
-
"type-check": "tsc --noEmit",
|
|
757
|
-
build: "npm run type-check && rm -rf ./dist/ && tsup",
|
|
758
|
-
dev: "npm run type-check && tsx watch src/test/index.ts",
|
|
759
|
-
patch: "npm run build && npm version patch",
|
|
760
|
-
minor: "npm run build && npm version minor",
|
|
761
|
-
major: "npm run build && npm version major",
|
|
762
|
-
"pub:patch": "npm run patch && npm publish --access public",
|
|
763
|
-
"pub:minor": "npm run minor && npm publish --access public",
|
|
764
|
-
"pub:major": "npm run major && npm publish --access public"
|
|
765
|
-
},
|
|
766
|
-
license: "MIT",
|
|
767
|
-
dependencies: {
|
|
768
|
-
"@spatulox/discord-interaction-manager": "^2.4.0",
|
|
769
|
-
"@spatulox/utils": "^0.2.0"
|
|
770
|
-
},
|
|
771
|
-
peerDependencies: {
|
|
772
|
-
"discord.js": "^14.26.0"
|
|
773
|
-
},
|
|
774
|
-
devDependencies: {
|
|
775
|
-
"@types/node": "^22.14.0",
|
|
776
|
-
"@types/node-schedule": "^2.1.7",
|
|
777
|
-
"discord.js": "^14.26.0",
|
|
778
|
-
dotenv: "^17.2.4",
|
|
779
|
-
tsup: "^8.5.1",
|
|
780
|
-
tsx: "^4.20.3",
|
|
781
|
-
typescript: "^5.9.3"
|
|
782
|
-
},
|
|
783
|
-
keywords: [
|
|
784
|
-
"discord",
|
|
785
|
-
"framework",
|
|
786
|
-
"bot",
|
|
787
|
-
"client",
|
|
788
|
-
"discordjs",
|
|
789
|
-
"discord.js",
|
|
790
|
-
"node"
|
|
791
|
-
],
|
|
792
|
-
bugs: {
|
|
793
|
-
url: "https://github.com/spatulox-discord/SimpleDiscordBot/issues"
|
|
794
|
-
}
|
|
795
|
-
};
|
|
687
|
+
var name = "@spatulox/simplediscordbot";
|
|
688
|
+
var version = "3.2.0";
|
|
689
|
+
var author = "Spatulox";
|
|
690
|
+
var description = "Simple discord bot framework to set up a bot under 30 secondes";
|
|
691
|
+
var license = "MIT";
|
|
796
692
|
|
|
797
693
|
// src/SimpleDiscordBotInfo.ts
|
|
798
694
|
var SimpleDiscordBotInfo = {
|
|
799
|
-
name
|
|
800
|
-
version
|
|
801
|
-
author
|
|
802
|
-
description
|
|
803
|
-
license
|
|
695
|
+
name,
|
|
696
|
+
version,
|
|
697
|
+
author,
|
|
698
|
+
description,
|
|
699
|
+
license
|
|
804
700
|
};
|
|
805
701
|
|
|
806
702
|
// src/core/Bot.ts
|
|
@@ -811,8 +707,9 @@ var Bot = class _Bot {
|
|
|
811
707
|
static interaction = BotInteraction;
|
|
812
708
|
// Instance properties
|
|
813
709
|
static _client;
|
|
814
|
-
static
|
|
710
|
+
static token;
|
|
815
711
|
static _config;
|
|
712
|
+
static randomActivityInterval = null;
|
|
816
713
|
get config() {
|
|
817
714
|
return _Bot._config;
|
|
818
715
|
}
|
|
@@ -825,34 +722,41 @@ var Bot = class _Bot {
|
|
|
825
722
|
static get config() {
|
|
826
723
|
return _Bot._config;
|
|
827
724
|
}
|
|
725
|
+
/**
|
|
726
|
+
* Resolves once the login is done (true) or has definitively failed (false)
|
|
727
|
+
*/
|
|
728
|
+
started;
|
|
828
729
|
constructor(client, config = {}) {
|
|
829
730
|
Log3.info("----------------------------------------------------");
|
|
830
731
|
Log3.info("Starting Bot");
|
|
831
|
-
_Bot.
|
|
732
|
+
_Bot.token = BotEnv.token;
|
|
832
733
|
_Bot._config = config;
|
|
833
734
|
_Bot._client = client;
|
|
834
|
-
(async () => {
|
|
835
|
-
|
|
735
|
+
_Bot._client.once(Events.ClientReady, async () => {
|
|
736
|
+
if (_Bot._client.user) {
|
|
737
|
+
await _Bot.log.initDiscordLogging();
|
|
738
|
+
Log3.info(`Connected on ${_Bot._client.guilds.cache.size} servers as ${_Bot._client.user.tag}`);
|
|
739
|
+
_Bot.log.info(EmbedManager.description("Bot Started"));
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
this.started = (async () => {
|
|
743
|
+
Log3.info(`Using discord.js version: ${version2}`);
|
|
836
744
|
Log3.info(`Using simplediscordbot version: ${SimpleDiscordBotInfo.version}`);
|
|
837
745
|
Log3.info("Trying to connect to Discord Servers");
|
|
838
746
|
await InternetChecker.checkConnection(3);
|
|
839
|
-
await this.login();
|
|
840
|
-
})()
|
|
747
|
+
return await this.login();
|
|
748
|
+
})().catch((error) => {
|
|
749
|
+
Log3.error(`Failed to start the bot: ${error}`);
|
|
750
|
+
return false;
|
|
751
|
+
});
|
|
841
752
|
}
|
|
842
753
|
async login(maxTries = 3) {
|
|
843
|
-
|
|
754
|
+
if (_Bot._client.isReady()) return true;
|
|
844
755
|
let tries = 0;
|
|
845
|
-
while (
|
|
756
|
+
while (tries < maxTries) {
|
|
846
757
|
try {
|
|
847
|
-
await _Bot._client.login(_Bot.
|
|
848
|
-
|
|
849
|
-
_Bot._client.on(Events.ClientReady, async () => {
|
|
850
|
-
if (_Bot._client.user) {
|
|
851
|
-
await _Bot.log.initDiscordLogging();
|
|
852
|
-
Log3.info(`Connected on ${_Bot._client.guilds.cache.size} servers as ${_Bot._client.user.tag}`);
|
|
853
|
-
_Bot.log.info(EmbedManager.description("Bot Started"));
|
|
854
|
-
}
|
|
855
|
-
});
|
|
758
|
+
await _Bot._client.login(_Bot.token);
|
|
759
|
+
return true;
|
|
856
760
|
} catch (error) {
|
|
857
761
|
Log3.error(`Connection error : ${error}. Trying again...`);
|
|
858
762
|
tries++;
|
|
@@ -861,11 +765,8 @@ var Bot = class _Bot {
|
|
|
861
765
|
);
|
|
862
766
|
}
|
|
863
767
|
}
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
return false;
|
|
867
|
-
}
|
|
868
|
-
return true;
|
|
768
|
+
Log3.error(`Impossible to connect the bot after ${maxTries} attempts`);
|
|
769
|
+
return false;
|
|
869
770
|
}
|
|
870
771
|
static setActivity(message, type) {
|
|
871
772
|
if (_Bot._client.user) {
|
|
@@ -878,6 +779,7 @@ var Bot = class _Bot {
|
|
|
878
779
|
Log3.error("Bot.randomActivity = [{}] is empty");
|
|
879
780
|
return;
|
|
880
781
|
}
|
|
782
|
+
_Bot.stopRandomActivity();
|
|
881
783
|
const pickRandom = () => {
|
|
882
784
|
const random = randomActivity[Math.floor(Math.random() * randomActivity.length)];
|
|
883
785
|
_Bot.setActivity(random.message, random.type);
|
|
@@ -889,12 +791,19 @@ var Bot = class _Bot {
|
|
|
889
791
|
return;
|
|
890
792
|
}
|
|
891
793
|
pickRandom();
|
|
892
|
-
setInterval(
|
|
893
|
-
|
|
894
|
-
}, intervalMs);
|
|
895
|
-
Log3.info(`Random activity started (every ${Math.round(intervalMs / 6e4)}min)`);
|
|
794
|
+
_Bot.randomActivityInterval = setInterval(pickRandom, intervalMs);
|
|
795
|
+
Log3.info(`Random activity started (every ${intervalMs >= 6e4 ? `${Math.round(intervalMs / 6e4)}min` : `${Math.round(intervalMs / 1e3)}s`})`);
|
|
896
796
|
return;
|
|
897
797
|
}
|
|
798
|
+
/**
|
|
799
|
+
* Stop the rotation started by setRandomActivity(), the current activity is kept
|
|
800
|
+
*/
|
|
801
|
+
static stopRandomActivity() {
|
|
802
|
+
if (_Bot.randomActivityInterval) {
|
|
803
|
+
clearInterval(_Bot.randomActivityInterval);
|
|
804
|
+
_Bot.randomActivityInterval = null;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
898
807
|
};
|
|
899
808
|
|
|
900
809
|
// src/index.ts
|
|
@@ -911,15 +820,16 @@ import {
|
|
|
911
820
|
import { readFile } from "fs/promises";
|
|
912
821
|
import { resolve } from "path";
|
|
913
822
|
var WebhookManager = class {
|
|
914
|
-
constructor(client,
|
|
823
|
+
constructor(client, name2, avatarPathOrUrl) {
|
|
915
824
|
this.client = client;
|
|
916
|
-
this.name =
|
|
825
|
+
this.name = name2;
|
|
917
826
|
this.avatarPathOrUrl = avatarPathOrUrl;
|
|
918
827
|
}
|
|
919
828
|
client;
|
|
920
829
|
name;
|
|
921
830
|
avatarPathOrUrl;
|
|
922
|
-
webhook
|
|
831
|
+
/** One webhook per channel id (the parent channel id for threads) */
|
|
832
|
+
webhooks = /* @__PURE__ */ new Map();
|
|
923
833
|
async getAvatar() {
|
|
924
834
|
if (!this.avatarPathOrUrl) return null;
|
|
925
835
|
if (this.avatarPathOrUrl.startsWith("http://") || this.avatarPathOrUrl.startsWith("https://")) {
|
|
@@ -947,42 +857,46 @@ var WebhookManager = class {
|
|
|
947
857
|
return channel;
|
|
948
858
|
}
|
|
949
859
|
/**
|
|
950
|
-
*
|
|
860
|
+
* Channel owning the webhook : the parent channel when targeting a thread
|
|
861
|
+
*/
|
|
862
|
+
getWebhookChannel(channel) {
|
|
863
|
+
if (!(channel instanceof ThreadChannel2)) return channel;
|
|
864
|
+
const parent = channel.parent;
|
|
865
|
+
if (!parent || !(parent instanceof BaseGuildTextChannel) && !(parent instanceof ThreadOnlyChannel)) {
|
|
866
|
+
throw new Error("Targeted channel parent is not a BaseGuildTextChannel or ThreadOnlyChannel");
|
|
867
|
+
}
|
|
868
|
+
return parent;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Get or create webhook (lazy initialization, cached per channel)
|
|
951
872
|
* Create the webhook in the parent channel if it's in a thread
|
|
952
873
|
*/
|
|
953
|
-
async getWebhook(
|
|
954
|
-
if (this.webhook) return this.webhook;
|
|
874
|
+
async getWebhook(channel) {
|
|
955
875
|
try {
|
|
956
|
-
const
|
|
957
|
-
|
|
958
|
-
if (
|
|
959
|
-
const parent = textThreadChannel.parent;
|
|
960
|
-
if (!parent || !(parent instanceof BaseGuildTextChannel) && !(parent instanceof ThreadOnlyChannel)) {
|
|
961
|
-
throw new Error("Targeted channel parent is not a BaseGuildTextChannel or ThreadOnlyChannel");
|
|
962
|
-
}
|
|
963
|
-
textChannel = parent;
|
|
964
|
-
} else {
|
|
965
|
-
textChannel = textThreadChannel;
|
|
966
|
-
}
|
|
876
|
+
const textChannel = this.getWebhookChannel(channel);
|
|
877
|
+
const cached = this.webhooks.get(textChannel.id);
|
|
878
|
+
if (cached) return cached;
|
|
967
879
|
const webhooks = await textChannel.fetchWebhooks();
|
|
968
|
-
|
|
880
|
+
const webhook = webhooks.find(
|
|
969
881
|
(h) => h.name === this.name && h.owner?.id === this.client.user?.id
|
|
970
882
|
) ?? await textChannel.createWebhook({
|
|
971
883
|
name: this.name,
|
|
972
884
|
avatar: await this.getAvatar(),
|
|
973
885
|
reason: "Auto-created by WebhookManager"
|
|
974
886
|
});
|
|
975
|
-
|
|
976
|
-
|
|
887
|
+
this.webhooks.set(textChannel.id, webhook);
|
|
888
|
+
Bot.log.debug(`Webhook ${webhook.id} ready for channel ${channel.id}`);
|
|
889
|
+
return webhook;
|
|
977
890
|
} catch (error) {
|
|
978
|
-
Bot.log.error(`Failed to setup webhook for ${
|
|
891
|
+
Bot.log.error(`Failed to setup webhook for ${channel.id}: ${error}`);
|
|
979
892
|
throw error;
|
|
980
893
|
}
|
|
981
894
|
}
|
|
982
895
|
async send(channelId, content) {
|
|
983
|
-
const
|
|
896
|
+
const channel = await this.getChannel(channelId);
|
|
897
|
+
const webhook = await this.getWebhook(channel);
|
|
984
898
|
let options = {};
|
|
985
|
-
if (SendableComponentBuilder.isSendableComponent(content)) {
|
|
899
|
+
if (SendableComponentBuilder.isSendableComponent(content) || Array.isArray(content)) {
|
|
986
900
|
options = SendableComponentBuilder.buildMessage(content);
|
|
987
901
|
} else if (typeof content === "string") {
|
|
988
902
|
options.content = content;
|
|
@@ -991,8 +905,7 @@ var WebhookManager = class {
|
|
|
991
905
|
} else {
|
|
992
906
|
options.content = String(content);
|
|
993
907
|
}
|
|
994
|
-
|
|
995
|
-
if (channelType instanceof ThreadChannel2) {
|
|
908
|
+
if (channel instanceof ThreadChannel2) {
|
|
996
909
|
options.threadId = channelId;
|
|
997
910
|
}
|
|
998
911
|
try {
|
|
@@ -1018,10 +931,13 @@ var WebhookManager = class {
|
|
|
1018
931
|
* Delete webhook
|
|
1019
932
|
*/
|
|
1020
933
|
async delete(channelId, reason) {
|
|
1021
|
-
|
|
934
|
+
const channel = await this.getChannel(channelId);
|
|
935
|
+
const webhookChannelId = this.getWebhookChannel(channel).id;
|
|
936
|
+
const webhook = this.webhooks.get(webhookChannelId);
|
|
937
|
+
if (!webhook) return;
|
|
1022
938
|
try {
|
|
1023
|
-
await
|
|
1024
|
-
Bot.log.info(`Webhook ${
|
|
939
|
+
await webhook.delete(reason ?? "Deleted by WebhookManager");
|
|
940
|
+
Bot.log.info(`Webhook ${webhook.id} deleted from ${channelId}`);
|
|
1025
941
|
} catch (error) {
|
|
1026
942
|
if (error.message.includes("Unknown Webhook")) {
|
|
1027
943
|
Bot.log.warn(`Webhook already deleted from ${channelId}`);
|
|
@@ -1029,244 +945,37 @@ var WebhookManager = class {
|
|
|
1029
945
|
Bot.log.error(`Failed to delete webhook from ${channelId}: ${error}`);
|
|
1030
946
|
}
|
|
1031
947
|
} finally {
|
|
1032
|
-
this.
|
|
948
|
+
this.webhooks.delete(webhookChannelId);
|
|
1033
949
|
}
|
|
1034
950
|
}
|
|
1035
951
|
};
|
|
1036
952
|
|
|
1037
953
|
// src/manager/messages/ReactionManager.ts
|
|
1038
|
-
import {
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
import { TextChannel as TextChannel4, ChannelType as ChannelType2 } from "discord.js";
|
|
1042
|
-
|
|
1043
|
-
// src/manager/guild/ChannelManager/GuildChannelManager.ts
|
|
1044
|
-
import { Log as Log5 } from "@spatulox/utils";
|
|
1045
|
-
|
|
1046
|
-
// src/manager/guild/ChannelManager/GuildMessageManager.ts
|
|
954
|
+
import {
|
|
955
|
+
parseEmoji
|
|
956
|
+
} from "discord.js";
|
|
1047
957
|
import { Log as Log4 } from "@spatulox/utils";
|
|
1048
|
-
var
|
|
1049
|
-
/**
|
|
1050
|
-
* Impl
|
|
1051
|
-
*/
|
|
1052
|
-
static async send(channelId, content_or_component_or_options) {
|
|
1053
|
-
try {
|
|
1054
|
-
const channel = await GuildTextChannelManager.find(channelId);
|
|
1055
|
-
if (!channel) {
|
|
1056
|
-
Log4.error(`Channel ${channelId} not found in guild`);
|
|
1057
|
-
return null;
|
|
1058
|
-
}
|
|
1059
|
-
let payload;
|
|
1060
|
-
if (typeof content_or_component_or_options === "string") {
|
|
1061
|
-
payload = SendableComponentBuilder.buildMessage(content_or_component_or_options);
|
|
1062
|
-
} else if (SendableComponentBuilder.isSendableComponent(content_or_component_or_options) || Array.isArray(content_or_component_or_options)) {
|
|
1063
|
-
payload = SendableComponentBuilder.buildMessage(content_or_component_or_options);
|
|
1064
|
-
} else {
|
|
1065
|
-
payload = content_or_component_or_options;
|
|
1066
|
-
}
|
|
1067
|
-
return await channel.send(payload);
|
|
1068
|
-
} catch (error) {
|
|
1069
|
-
Log4.error(`Failed to send message to ${channelId}: ${error}`);
|
|
1070
|
-
throw error;
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
/**
|
|
1074
|
-
* Delete message
|
|
1075
|
-
*/
|
|
1076
|
-
static async delete(channelId, messageId) {
|
|
1077
|
-
try {
|
|
1078
|
-
const channel = await GuildTextChannelManager.find(channelId);
|
|
1079
|
-
if (!channel) {
|
|
1080
|
-
Log4.error(`Channel ${channelId} not found`);
|
|
1081
|
-
return false;
|
|
1082
|
-
}
|
|
1083
|
-
const message = await channel.messages.fetch(messageId);
|
|
1084
|
-
await message.delete();
|
|
1085
|
-
Log4.info(`Deleted message ${messageId} from ${channelId}`);
|
|
1086
|
-
return true;
|
|
1087
|
-
} catch (error) {
|
|
1088
|
-
Log4.error(`Failed to delete message ${messageId} from ${channelId}: ${error}`);
|
|
1089
|
-
return false;
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
/**
|
|
1093
|
-
* Fetch messages (last X messages, default 10)
|
|
1094
|
-
*/
|
|
1095
|
-
static async fetch(channelId, limit = 10) {
|
|
1096
|
-
try {
|
|
1097
|
-
const channel = await GuildTextChannelManager.find(channelId);
|
|
1098
|
-
if (!channel) {
|
|
1099
|
-
Log4.error(`Channel ${channelId} not found`);
|
|
1100
|
-
return [];
|
|
1101
|
-
}
|
|
1102
|
-
const messages = await channel.messages.fetch({ limit });
|
|
1103
|
-
Log4.info(`Fetched ${messages.size} messages from ${channelId}`);
|
|
1104
|
-
return Array.from(messages.values());
|
|
1105
|
-
} catch (error) {
|
|
1106
|
-
Log4.error(`Failed to fetch messages from ${channelId}: ${error}`);
|
|
1107
|
-
return [];
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
* Fetch single message
|
|
1112
|
-
*/
|
|
1113
|
-
static async fetchOne(channelId, messageId) {
|
|
1114
|
-
try {
|
|
1115
|
-
const channel = await GuildTextChannelManager.find(channelId);
|
|
1116
|
-
if (!channel) {
|
|
1117
|
-
Log4.error(`Channel ${channelId} not found`);
|
|
1118
|
-
return null;
|
|
1119
|
-
}
|
|
1120
|
-
const message = await channel.messages.fetch(messageId);
|
|
1121
|
-
return message;
|
|
1122
|
-
} catch (error) {
|
|
1123
|
-
Log4.error(`Failed to fetch message ${messageId} from ${channelId}: ${error}`);
|
|
1124
|
-
return null;
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
};
|
|
1128
|
-
|
|
1129
|
-
// src/manager/guild/ChannelManager/GuildChannelManager.ts
|
|
1130
|
-
var GuildChannelManager = class _GuildChannelManager {
|
|
1131
|
-
static message = GuildMessageManager;
|
|
1132
|
-
/**
|
|
1133
|
-
* Recherche un channel par ID dans une guild spécifique
|
|
1134
|
-
*/
|
|
1135
|
-
static async findInGuild(guildId, channelId) {
|
|
1136
|
-
try {
|
|
1137
|
-
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1138
|
-
if (!guild) {
|
|
1139
|
-
Log5.warn(`Guild ${guildId} not found`);
|
|
1140
|
-
return null;
|
|
1141
|
-
}
|
|
1142
|
-
const channel = await guild.channels.fetch(channelId);
|
|
1143
|
-
return channel ?? null;
|
|
1144
|
-
} catch (error) {
|
|
1145
|
-
Log5.error(`Failed to find channel ${channelId} in guild ${guildId}: ${error}`);
|
|
1146
|
-
return null;
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
static async find(channelId) {
|
|
1150
|
-
try {
|
|
1151
|
-
const cached = Bot.client.channels.cache.get(channelId);
|
|
1152
|
-
if (cached && !cached.isDMBased()) return cached;
|
|
1153
|
-
const channel = await Bot.client.channels.fetch(channelId);
|
|
1154
|
-
if (channel && !channel.isDMBased()) return channel;
|
|
1155
|
-
Log5.warn(`Channel ${channelId} not found in any guild`);
|
|
1156
|
-
return null;
|
|
1157
|
-
} catch (error) {
|
|
1158
|
-
Log5.error(`Failed to find channel ${channelId}: ${error}`);
|
|
1159
|
-
return null;
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
static findAll(guildId) {
|
|
1163
|
-
try {
|
|
1164
|
-
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1165
|
-
if (!guild) {
|
|
1166
|
-
Log5.warn(`Guild ${guildId} not found`);
|
|
1167
|
-
return [];
|
|
1168
|
-
}
|
|
1169
|
-
return Array.from(guild.channels.cache.values());
|
|
1170
|
-
} catch (error) {
|
|
1171
|
-
Log5.error(`Failed to find channel ${guildId}: ${error}`);
|
|
1172
|
-
return [];
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
958
|
+
var ReactionManager = class {
|
|
1175
959
|
/**
|
|
1176
|
-
*
|
|
960
|
+
* Any text based channel : guild channels, threads and DMs
|
|
1177
961
|
*/
|
|
1178
|
-
static
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
Log5.warn(`Guild ${guildId} not found`);
|
|
1183
|
-
return [];
|
|
1184
|
-
}
|
|
1185
|
-
return Array.from(guild.channels.cache.values()).filter(
|
|
1186
|
-
(channel) => channel.name.toLowerCase().includes(name.toLowerCase())
|
|
1187
|
-
);
|
|
1188
|
-
} catch (error) {
|
|
1189
|
-
Log5.error(`Failed to find channel ${guildId}: ${error}`);
|
|
1190
|
-
return [];
|
|
1191
|
-
}
|
|
1192
|
-
}
|
|
1193
|
-
static async _create(guildId, options) {
|
|
1194
|
-
try {
|
|
1195
|
-
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1196
|
-
if (!guild) {
|
|
1197
|
-
throw new Error(`Guild ${guildId} not found`);
|
|
1198
|
-
}
|
|
1199
|
-
const channel = await guild.channels.create(options);
|
|
1200
|
-
Log5.info(`Created channel ${channel.name} (${channel.id}) in guild ${guildId}`);
|
|
1201
|
-
return channel;
|
|
1202
|
-
} catch (error) {
|
|
1203
|
-
Log5.error(`Failed to create channel in guild ${guildId}: ${error}`);
|
|
1204
|
-
throw error;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
static async delete(channelId) {
|
|
1208
|
-
try {
|
|
1209
|
-
const channel = await _GuildChannelManager.find(channelId);
|
|
1210
|
-
if (!channel) {
|
|
1211
|
-
throw new Error(`Channel ${channelId} not found`);
|
|
1212
|
-
}
|
|
1213
|
-
await channel.delete();
|
|
1214
|
-
Log5.info(`Deleted channel ${channelId}`);
|
|
1215
|
-
return true;
|
|
1216
|
-
} catch (error) {
|
|
1217
|
-
Log5.error(`Failed to delete channel ${channelId}: ${error}`);
|
|
1218
|
-
throw error;
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
};
|
|
1222
|
-
|
|
1223
|
-
// src/manager/guild/ChannelManager/GuildTextChannelManager.ts
|
|
1224
|
-
import { Log as Log6 } from "@spatulox/utils";
|
|
1225
|
-
var GuildTextChannelManager = class extends GuildChannelManager {
|
|
1226
|
-
static async findInGuild(guildId, channelId) {
|
|
1227
|
-
const channel = await super.findInGuild(guildId, channelId);
|
|
1228
|
-
return channel?.isTextBased() ? channel : null;
|
|
1229
|
-
}
|
|
1230
|
-
static async find(channelId) {
|
|
1231
|
-
const channel = await super.find(channelId);
|
|
1232
|
-
return channel?.isTextBased() ? channel : null;
|
|
1233
|
-
}
|
|
1234
|
-
static findAll(guildId) {
|
|
1235
|
-
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1236
|
-
if (!guild) {
|
|
1237
|
-
Log6.warn(`Guild ${guildId} not found`);
|
|
1238
|
-
return [];
|
|
962
|
+
static async fetchMessage(channelId, messageId) {
|
|
963
|
+
const channel = Bot.client.channels.cache.get(channelId) ?? await Bot.client.channels.fetch(channelId);
|
|
964
|
+
if (!channel?.isTextBased()) {
|
|
965
|
+
throw new Error(`Channel ${channelId} not found or not text based`);
|
|
1239
966
|
}
|
|
1240
|
-
return
|
|
1241
|
-
}
|
|
1242
|
-
static async create(guildId, name, options) {
|
|
1243
|
-
return await super._create(guildId, {
|
|
1244
|
-
name,
|
|
1245
|
-
type: ChannelType2.GuildText,
|
|
1246
|
-
...options
|
|
1247
|
-
});
|
|
967
|
+
return await channel.messages.fetch(messageId);
|
|
1248
968
|
}
|
|
1249
|
-
};
|
|
1250
|
-
|
|
1251
|
-
// src/manager/messages/ReactionManager.ts
|
|
1252
|
-
var ReactionManager = class {
|
|
1253
969
|
/**
|
|
1254
970
|
* Add a reaction to a message
|
|
1255
971
|
*/
|
|
1256
972
|
static async add(channelId, messageId, emoji) {
|
|
1257
973
|
try {
|
|
1258
|
-
const
|
|
1259
|
-
if (!channel) {
|
|
1260
|
-
throw new Error(`Channel ${channelId} not found`);
|
|
1261
|
-
}
|
|
1262
|
-
if (!channel.isTextBased()) {
|
|
1263
|
-
throw new Error(`Channel ${channelId} is not text based`);
|
|
1264
|
-
}
|
|
1265
|
-
const message = await channel.messages.fetch(messageId);
|
|
974
|
+
const message = await this.fetchMessage(channelId, messageId);
|
|
1266
975
|
await message.react(emoji);
|
|
1267
|
-
|
|
976
|
+
Log4.info(`Added reaction ${emoji} to message ${messageId}`);
|
|
1268
977
|
} catch (error) {
|
|
1269
|
-
|
|
978
|
+
Log4.error(`Failed to add reaction to ${messageId}: ${error}`);
|
|
1270
979
|
throw error;
|
|
1271
980
|
}
|
|
1272
981
|
}
|
|
@@ -1275,20 +984,16 @@ var ReactionManager = class {
|
|
|
1275
984
|
*/
|
|
1276
985
|
static async remove(channelId, messageId, emoji, userId) {
|
|
1277
986
|
try {
|
|
1278
|
-
const
|
|
1279
|
-
|
|
1280
|
-
throw new Error(`Channel ${channelId} not found`);
|
|
1281
|
-
}
|
|
1282
|
-
const message = await channel.messages.fetch(messageId);
|
|
1283
|
-
const reaction = message.reactions.resolve(emoji);
|
|
987
|
+
const message = await this.fetchMessage(channelId, messageId);
|
|
988
|
+
const reaction = message.reactions.resolve(parseEmoji(emoji)?.id ?? emoji);
|
|
1284
989
|
if (!reaction) {
|
|
1285
990
|
throw new Error(`Reaction ${emoji} not found on message ${messageId}`);
|
|
1286
991
|
}
|
|
1287
992
|
const user = await Bot.client.users.fetch(userId);
|
|
1288
993
|
await reaction.users.remove(user);
|
|
1289
|
-
|
|
994
|
+
Log4.info(`Removed reaction ${emoji} from user ${userId} on message ${messageId}`);
|
|
1290
995
|
} catch (error) {
|
|
1291
|
-
|
|
996
|
+
Log4.error(`Failed to remove reaction from ${messageId}: ${error}`);
|
|
1292
997
|
throw error;
|
|
1293
998
|
}
|
|
1294
999
|
}
|
|
@@ -1297,11 +1002,7 @@ var ReactionManager = class {
|
|
|
1297
1002
|
*/
|
|
1298
1003
|
static async getAll(channelId, messageId) {
|
|
1299
1004
|
try {
|
|
1300
|
-
const
|
|
1301
|
-
if (!channel) {
|
|
1302
|
-
throw new Error(`Channel ${channelId} not found`);
|
|
1303
|
-
}
|
|
1304
|
-
const message = await channel.messages.fetch(messageId);
|
|
1005
|
+
const message = await this.fetchMessage(channelId, messageId);
|
|
1305
1006
|
const reactions = message.reactions.cache;
|
|
1306
1007
|
const reactionList = [];
|
|
1307
1008
|
for (const reaction of reactions.values()) {
|
|
@@ -1312,10 +1013,10 @@ var ReactionManager = class {
|
|
|
1312
1013
|
users: Array.from(users.values())
|
|
1313
1014
|
});
|
|
1314
1015
|
}
|
|
1315
|
-
|
|
1016
|
+
Log4.info(`Fetched ${reactionList.length} reactions for message ${messageId}`);
|
|
1316
1017
|
return reactionList;
|
|
1317
1018
|
} catch (error) {
|
|
1318
|
-
|
|
1019
|
+
Log4.error(`Failed to fetch reactions for ${messageId}: ${error}`);
|
|
1319
1020
|
throw error;
|
|
1320
1021
|
}
|
|
1321
1022
|
}
|
|
@@ -1324,15 +1025,11 @@ var ReactionManager = class {
|
|
|
1324
1025
|
*/
|
|
1325
1026
|
static async clear(channelId, messageId) {
|
|
1326
1027
|
try {
|
|
1327
|
-
const
|
|
1328
|
-
if (!channel) {
|
|
1329
|
-
throw new Error(`Channel ${channelId} not found`);
|
|
1330
|
-
}
|
|
1331
|
-
const message = await channel.messages.fetch(messageId);
|
|
1028
|
+
const message = await this.fetchMessage(channelId, messageId);
|
|
1332
1029
|
await message.reactions.removeAll();
|
|
1333
|
-
|
|
1030
|
+
Log4.info(`Cleared all reactions from message ${messageId}`);
|
|
1334
1031
|
} catch (error) {
|
|
1335
|
-
|
|
1032
|
+
Log4.error(`Failed to clear reactions from ${messageId}: ${error}`);
|
|
1336
1033
|
throw error;
|
|
1337
1034
|
}
|
|
1338
1035
|
}
|
|
@@ -1344,11 +1041,11 @@ import { Log as Log16 } from "@spatulox/utils";
|
|
|
1344
1041
|
import { Time as Time2 } from "@spatulox/utils";
|
|
1345
1042
|
|
|
1346
1043
|
// src/manager/guild/GuildUserManager.ts
|
|
1347
|
-
import { Log as
|
|
1044
|
+
import { Log as Log6 } from "@spatulox/utils";
|
|
1348
1045
|
|
|
1349
1046
|
// src/manager/direct/BasicUserManager.ts
|
|
1350
1047
|
import { GuildMember as GuildMember2 } from "discord.js";
|
|
1351
|
-
import { Log as
|
|
1048
|
+
import { Log as Log5 } from "@spatulox/utils";
|
|
1352
1049
|
var BasicUserManager = class {
|
|
1353
1050
|
/**
|
|
1354
1051
|
* Find member in specific guild
|
|
@@ -1364,7 +1061,7 @@ var BasicUserManager = class {
|
|
|
1364
1061
|
const cached = targetGuild.members.cache.get(memberId);
|
|
1365
1062
|
if (cached) return cached;
|
|
1366
1063
|
const member = await targetGuild.members.fetch(memberId).catch(() => null);
|
|
1367
|
-
if (!member)
|
|
1064
|
+
if (!member) Log5.error(`UserManager: Member ${memberId} not found in guild ${targetGuild.id}`);
|
|
1368
1065
|
return member;
|
|
1369
1066
|
}
|
|
1370
1067
|
/**
|
|
@@ -1400,26 +1097,27 @@ var BasicUserManager = class {
|
|
|
1400
1097
|
);
|
|
1401
1098
|
} else if (typeof content_or_component_or_options === "string") {
|
|
1402
1099
|
payload = SendableComponentBuilder.buildMessage(content_or_component_or_options);
|
|
1403
|
-
} else if (SendableComponentBuilder.isSendableComponent(content_or_component_or_options)) {
|
|
1100
|
+
} else if (SendableComponentBuilder.isSendableComponent(content_or_component_or_options) || Array.isArray(content_or_component_or_options)) {
|
|
1404
1101
|
payload = SendableComponentBuilder.buildMessage(content_or_component_or_options);
|
|
1405
1102
|
} else {
|
|
1406
1103
|
payload = content_or_component_or_options;
|
|
1407
1104
|
}
|
|
1408
1105
|
const message = await dmChannel.send(payload);
|
|
1409
1106
|
if (user instanceof GuildMember2) {
|
|
1410
|
-
|
|
1107
|
+
Log5.info(`Sent DM to ${user.id} (${user.user.username}): "${(payload.content || "Embed/Component").substring(0, 50)}..."`);
|
|
1411
1108
|
} else {
|
|
1412
|
-
|
|
1109
|
+
Log5.info(`Sent DM to ${user.id} (${user.username}): "${(payload.content || "Embed/Component").substring(0, 50)}..."`);
|
|
1413
1110
|
}
|
|
1414
1111
|
return message;
|
|
1415
1112
|
} catch (error) {
|
|
1416
|
-
|
|
1113
|
+
Log5.error(`Failed to send DM to ${user_id_or_user}: ${error}`);
|
|
1417
1114
|
throw new Error(`Cannot send DM to ${user_id_or_user}`);
|
|
1418
1115
|
}
|
|
1419
1116
|
}
|
|
1420
1117
|
};
|
|
1421
1118
|
|
|
1422
1119
|
// src/manager/guild/GuildUserManager.ts
|
|
1120
|
+
import { RESTJSONErrorCodes } from "discord.js";
|
|
1423
1121
|
import { setTimeout as setTimeout2 } from "timers/promises";
|
|
1424
1122
|
var MAX_NICKNAME_LENGTH = 32;
|
|
1425
1123
|
var GuildUserManager = class extends BasicUserManager {
|
|
@@ -1431,15 +1129,18 @@ var GuildUserManager = class extends BasicUserManager {
|
|
|
1431
1129
|
try {
|
|
1432
1130
|
const oldName = member.displayName;
|
|
1433
1131
|
await member.setNickname(nickname.trim());
|
|
1434
|
-
|
|
1132
|
+
Log6.info(`Renaming user: ${oldName} \u2192 ${nickname.trim()}`);
|
|
1435
1133
|
await setTimeout2(1500);
|
|
1436
1134
|
return true;
|
|
1437
1135
|
} catch (error) {
|
|
1438
|
-
|
|
1136
|
+
Log6.error(`Attempt ${attempts + 1} failed when renaming ${member.displayName} into ${nickname.trim()}: ${error}`);
|
|
1137
|
+
if (error?.code === RESTJSONErrorCodes.MissingPermissions || error?.code === RESTJSONErrorCodes.MissingAccess) {
|
|
1138
|
+
break;
|
|
1139
|
+
}
|
|
1439
1140
|
await setTimeout2(1e3);
|
|
1440
1141
|
}
|
|
1441
1142
|
}
|
|
1442
|
-
Bot.log.
|
|
1143
|
+
Bot.log.error(EmbedManager.error(`Failed to rename ${member.displayName} to ${nickname.trim()}.`));
|
|
1443
1144
|
return false;
|
|
1444
1145
|
}
|
|
1445
1146
|
/**
|
|
@@ -1449,13 +1150,13 @@ var GuildUserManager = class extends BasicUserManager {
|
|
|
1449
1150
|
try {
|
|
1450
1151
|
const guild = await GuildManager.find(guildId);
|
|
1451
1152
|
if (!guild) {
|
|
1452
|
-
|
|
1153
|
+
Log6.warn(`Guild ${guildId} not found`);
|
|
1453
1154
|
return false;
|
|
1454
1155
|
}
|
|
1455
1156
|
const ban = await guild.bans.fetch(userId);
|
|
1456
1157
|
return !!ban;
|
|
1457
1158
|
} catch (error) {
|
|
1458
|
-
|
|
1159
|
+
Log6.debug(`User ${userId} is not banned from guild ${guildId}`);
|
|
1459
1160
|
return false;
|
|
1460
1161
|
}
|
|
1461
1162
|
}
|
|
@@ -1469,170 +1170,356 @@ var GuildUserManager = class extends BasicUserManager {
|
|
|
1469
1170
|
throw new Error(`Member ${memberId} not found or not in a voice channel`);
|
|
1470
1171
|
}
|
|
1471
1172
|
await member.voice.disconnect();
|
|
1472
|
-
|
|
1173
|
+
Log6.info(`Disconnected member ${memberId} from voice in guild ${guildId}`);
|
|
1473
1174
|
} catch (error) {
|
|
1474
|
-
|
|
1175
|
+
Log6.error(`Failed to disconnect member ${memberId} from voice in ${guildId}: ${error}`);
|
|
1475
1176
|
throw error;
|
|
1476
1177
|
}
|
|
1477
1178
|
}
|
|
1478
1179
|
/**
|
|
1479
1180
|
* Check if a member is in voice
|
|
1480
1181
|
*/
|
|
1481
|
-
static async isInVoice(
|
|
1182
|
+
static async isInVoice(guildId, memberId) {
|
|
1183
|
+
try {
|
|
1184
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1185
|
+
if (!member) {
|
|
1186
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1187
|
+
}
|
|
1188
|
+
return member.voice.channelId !== null;
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
Log6.debug(`Member ${memberId} not found or not in voice in guild ${guildId}`);
|
|
1191
|
+
return false;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Mute a member
|
|
1196
|
+
*/
|
|
1197
|
+
static async mute(guildId, memberId, reason) {
|
|
1482
1198
|
try {
|
|
1483
1199
|
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1484
1200
|
if (!member) {
|
|
1485
|
-
throw new Error(`
|
|
1201
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1202
|
+
}
|
|
1203
|
+
await member.voice.setMute(true, reason);
|
|
1204
|
+
Log6.info(`Server muted ${memberId} in guild ${guildId}: ${reason || "No reason"}`);
|
|
1205
|
+
} catch (error) {
|
|
1206
|
+
Log6.error(`Failed to server mute ${memberId} in ${guildId}: ${error}`);
|
|
1207
|
+
throw error;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Unmute a member
|
|
1212
|
+
*/
|
|
1213
|
+
static async unmute(guildId, memberId, reason) {
|
|
1214
|
+
try {
|
|
1215
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1216
|
+
if (!member) {
|
|
1217
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1218
|
+
}
|
|
1219
|
+
await member.voice.setMute(false, reason);
|
|
1220
|
+
Log6.info(`Server unmuted ${memberId} in guild ${guildId}: ${reason || "No reason"}`);
|
|
1221
|
+
} catch (error) {
|
|
1222
|
+
Log6.error(`Failed to server unmute ${memberId} in ${guildId}: ${error}`);
|
|
1223
|
+
throw error;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Deafen a member
|
|
1228
|
+
*/
|
|
1229
|
+
static async deafen(guildId, memberId, reason) {
|
|
1230
|
+
try {
|
|
1231
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1232
|
+
if (!member) {
|
|
1233
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1234
|
+
}
|
|
1235
|
+
await member.voice.setDeaf(true, reason);
|
|
1236
|
+
Log6.info(`Server deafened ${memberId} in guild ${guildId}: ${reason || "No reason"}`);
|
|
1237
|
+
} catch (error) {
|
|
1238
|
+
Log6.error(`Failed to server deafen ${memberId} in ${guildId}: ${error}`);
|
|
1239
|
+
throw error;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* Undeafen member voice
|
|
1244
|
+
*/
|
|
1245
|
+
static async undeafen(guildId, memberId, reason) {
|
|
1246
|
+
try {
|
|
1247
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1248
|
+
if (!member) {
|
|
1249
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1250
|
+
}
|
|
1251
|
+
await member.voice.setDeaf(false, reason);
|
|
1252
|
+
Log6.info(`Server undeafened ${memberId} in guild ${guildId}: ${reason || "No reason"}`);
|
|
1253
|
+
} catch (error) {
|
|
1254
|
+
Log6.error(`Failed to server undeafen ${memberId} in ${guildId}: ${error}`);
|
|
1255
|
+
throw error;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Timeout a member
|
|
1260
|
+
*/
|
|
1261
|
+
static async timeout(guildId, memberId, duration, reason) {
|
|
1262
|
+
try {
|
|
1263
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1264
|
+
if (!member) {
|
|
1265
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1266
|
+
}
|
|
1267
|
+
await member.timeout(duration, reason);
|
|
1268
|
+
Log6.info(`Timed out ${memberId} for ${duration}ms in guild ${guildId}: ${reason || "No reason"}`);
|
|
1269
|
+
} catch (error) {
|
|
1270
|
+
Log6.error(`Failed to timeout ${memberId} in ${guildId}: ${error}`);
|
|
1271
|
+
throw error;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
/**
|
|
1275
|
+
* Remove the timeout of a member
|
|
1276
|
+
*/
|
|
1277
|
+
static async untimeout(guildId, memberId, reason) {
|
|
1278
|
+
try {
|
|
1279
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1280
|
+
if (!member) {
|
|
1281
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1282
|
+
}
|
|
1283
|
+
await member.timeout(null, reason);
|
|
1284
|
+
Log6.info(`Untimed out ${memberId} in guild ${guildId}: ${reason || "No reason"}`);
|
|
1285
|
+
} catch (error) {
|
|
1286
|
+
Log6.error(`Failed to untimeout ${memberId} in ${guildId}: ${error}`);
|
|
1287
|
+
throw error;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* Kick a member
|
|
1292
|
+
*/
|
|
1293
|
+
static async kick(guildId, memberId, reason) {
|
|
1294
|
+
try {
|
|
1295
|
+
const member = await GuildManager.user.findInGuild(guildId, memberId);
|
|
1296
|
+
if (!member) {
|
|
1297
|
+
throw new Error(`Member ${memberId} not found in guild ${guildId}`);
|
|
1298
|
+
}
|
|
1299
|
+
await member.kick(reason);
|
|
1300
|
+
Log6.info(`Kicked ${memberId} from guild ${guildId}: ${reason || "No reason"}`);
|
|
1301
|
+
} catch (error) {
|
|
1302
|
+
Log6.error(`Failed to kick ${memberId} from ${guildId}: ${error}`);
|
|
1303
|
+
throw error;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
static async ban(guildId, userId, banOption) {
|
|
1307
|
+
const guild = await GuildManager.find(guildId);
|
|
1308
|
+
if (!guild) {
|
|
1309
|
+
throw new Error(`Guild ${guildId} not found`);
|
|
1310
|
+
}
|
|
1311
|
+
try {
|
|
1312
|
+
await guild.members.ban(userId, banOption);
|
|
1313
|
+
Log6.info(`Banned user ${userId} from guild ${guildId}`);
|
|
1314
|
+
} catch (error) {
|
|
1315
|
+
Log6.error(`Failed to ban user ${userId} from guild ${guildId}: ${error}`);
|
|
1316
|
+
throw error;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
static async unban(guildId, userId, reason) {
|
|
1320
|
+
const guild = await GuildManager.find(guildId);
|
|
1321
|
+
if (!guild) {
|
|
1322
|
+
throw new Error(`Guild ${guildId} not found`);
|
|
1323
|
+
}
|
|
1324
|
+
try {
|
|
1325
|
+
await guild.members.unban(userId, reason);
|
|
1326
|
+
Log6.info(`Unbanned user ${userId} from guild ${guildId}`);
|
|
1327
|
+
} catch (error) {
|
|
1328
|
+
Log6.error(`Failed to unban user ${userId} from guild ${guildId}: ${error}`);
|
|
1329
|
+
throw error;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
// src/manager/guild/ChannelManager/GuildChannelManager.ts
|
|
1335
|
+
import { Log as Log8 } from "@spatulox/utils";
|
|
1336
|
+
|
|
1337
|
+
// src/manager/guild/ChannelManager/GuildMessageManager.ts
|
|
1338
|
+
import { Log as Log7 } from "@spatulox/utils";
|
|
1339
|
+
var GuildMessageManager = class {
|
|
1340
|
+
/**
|
|
1341
|
+
* Any guild channel able to hold messages : text, announcement, thread, voice / stage text chat
|
|
1342
|
+
*/
|
|
1343
|
+
static async findTextBased(channelId) {
|
|
1344
|
+
const channel = await GuildChannelManager.find(channelId);
|
|
1345
|
+
return channel?.isTextBased() ? channel : null;
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Impl
|
|
1349
|
+
*/
|
|
1350
|
+
static async send(channelId, content_or_component_or_options) {
|
|
1351
|
+
try {
|
|
1352
|
+
const channel = await this.findTextBased(channelId);
|
|
1353
|
+
if (!channel) {
|
|
1354
|
+
Log7.error(`Channel ${channelId} not found in guild`);
|
|
1355
|
+
return null;
|
|
1356
|
+
}
|
|
1357
|
+
let payload;
|
|
1358
|
+
if (typeof content_or_component_or_options === "string") {
|
|
1359
|
+
payload = SendableComponentBuilder.buildMessage(content_or_component_or_options);
|
|
1360
|
+
} else if (SendableComponentBuilder.isSendableComponent(content_or_component_or_options) || Array.isArray(content_or_component_or_options)) {
|
|
1361
|
+
payload = SendableComponentBuilder.buildMessage(content_or_component_or_options);
|
|
1362
|
+
} else {
|
|
1363
|
+
payload = content_or_component_or_options;
|
|
1486
1364
|
}
|
|
1487
|
-
return
|
|
1365
|
+
return await channel.send(payload);
|
|
1488
1366
|
} catch (error) {
|
|
1489
|
-
|
|
1490
|
-
|
|
1367
|
+
Log7.error(`Failed to send message to ${channelId}: ${error}`);
|
|
1368
|
+
throw error;
|
|
1491
1369
|
}
|
|
1492
1370
|
}
|
|
1493
1371
|
/**
|
|
1494
|
-
*
|
|
1372
|
+
* Delete message
|
|
1495
1373
|
*/
|
|
1496
|
-
static async
|
|
1374
|
+
static async delete(channelId, messageId) {
|
|
1497
1375
|
try {
|
|
1498
|
-
const
|
|
1499
|
-
if (!
|
|
1500
|
-
|
|
1376
|
+
const channel = await this.findTextBased(channelId);
|
|
1377
|
+
if (!channel) {
|
|
1378
|
+
Log7.error(`Channel ${channelId} not found`);
|
|
1379
|
+
return false;
|
|
1501
1380
|
}
|
|
1502
|
-
await
|
|
1503
|
-
|
|
1381
|
+
const message = await channel.messages.fetch(messageId);
|
|
1382
|
+
await message.delete();
|
|
1383
|
+
Log7.info(`Deleted message ${messageId} from ${channelId}`);
|
|
1384
|
+
return true;
|
|
1504
1385
|
} catch (error) {
|
|
1505
|
-
|
|
1506
|
-
|
|
1386
|
+
Log7.error(`Failed to delete message ${messageId} from ${channelId}: ${error}`);
|
|
1387
|
+
return false;
|
|
1507
1388
|
}
|
|
1508
1389
|
}
|
|
1509
1390
|
/**
|
|
1510
|
-
*
|
|
1391
|
+
* Fetch messages (last X messages, default 10)
|
|
1511
1392
|
*/
|
|
1512
|
-
static async
|
|
1393
|
+
static async fetch(channelId, limit = 10) {
|
|
1513
1394
|
try {
|
|
1514
|
-
const
|
|
1515
|
-
if (!
|
|
1516
|
-
|
|
1395
|
+
const channel = await this.findTextBased(channelId);
|
|
1396
|
+
if (!channel) {
|
|
1397
|
+
Log7.error(`Channel ${channelId} not found`);
|
|
1398
|
+
return [];
|
|
1517
1399
|
}
|
|
1518
|
-
await
|
|
1519
|
-
|
|
1400
|
+
const messages = await channel.messages.fetch({ limit });
|
|
1401
|
+
Log7.info(`Fetched ${messages.size} messages from ${channelId}`);
|
|
1402
|
+
return Array.from(messages.values());
|
|
1520
1403
|
} catch (error) {
|
|
1521
|
-
|
|
1522
|
-
|
|
1404
|
+
Log7.error(`Failed to fetch messages from ${channelId}: ${error}`);
|
|
1405
|
+
return [];
|
|
1523
1406
|
}
|
|
1524
1407
|
}
|
|
1525
1408
|
/**
|
|
1526
|
-
*
|
|
1409
|
+
* Fetch single message
|
|
1527
1410
|
*/
|
|
1528
|
-
static async
|
|
1411
|
+
static async fetchOne(channelId, messageId) {
|
|
1529
1412
|
try {
|
|
1530
|
-
const
|
|
1531
|
-
if (!
|
|
1532
|
-
|
|
1413
|
+
const channel = await this.findTextBased(channelId);
|
|
1414
|
+
if (!channel) {
|
|
1415
|
+
Log7.error(`Channel ${channelId} not found`);
|
|
1416
|
+
return null;
|
|
1533
1417
|
}
|
|
1534
|
-
await
|
|
1535
|
-
|
|
1418
|
+
const message = await channel.messages.fetch(messageId);
|
|
1419
|
+
return message;
|
|
1536
1420
|
} catch (error) {
|
|
1537
|
-
|
|
1538
|
-
|
|
1421
|
+
Log7.error(`Failed to fetch message ${messageId} from ${channelId}: ${error}`);
|
|
1422
|
+
return null;
|
|
1539
1423
|
}
|
|
1540
1424
|
}
|
|
1425
|
+
};
|
|
1426
|
+
|
|
1427
|
+
// src/manager/guild/ChannelManager/GuildChannelManager.ts
|
|
1428
|
+
var GuildChannelManager = class _GuildChannelManager {
|
|
1429
|
+
static message = GuildMessageManager;
|
|
1541
1430
|
/**
|
|
1542
|
-
*
|
|
1431
|
+
* Recherche un channel par ID dans une guild spécifique
|
|
1543
1432
|
*/
|
|
1544
|
-
static async
|
|
1433
|
+
static async findInGuild(guildId, channelId) {
|
|
1545
1434
|
try {
|
|
1546
|
-
const
|
|
1547
|
-
if (!
|
|
1548
|
-
|
|
1435
|
+
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1436
|
+
if (!guild) {
|
|
1437
|
+
Log8.warn(`Guild ${guildId} not found`);
|
|
1438
|
+
return null;
|
|
1549
1439
|
}
|
|
1550
|
-
await
|
|
1551
|
-
|
|
1440
|
+
const channel = await guild.channels.fetch(channelId);
|
|
1441
|
+
return channel ?? null;
|
|
1552
1442
|
} catch (error) {
|
|
1553
|
-
|
|
1554
|
-
|
|
1443
|
+
Log8.error(`Failed to find channel ${channelId} in guild ${guildId}: ${error}`);
|
|
1444
|
+
return null;
|
|
1555
1445
|
}
|
|
1556
1446
|
}
|
|
1557
|
-
|
|
1558
|
-
* Timeout a member
|
|
1559
|
-
*/
|
|
1560
|
-
static async timeout(guildId, memberId, duration, reason) {
|
|
1447
|
+
static async find(channelId) {
|
|
1561
1448
|
try {
|
|
1562
|
-
const
|
|
1563
|
-
if (!
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
Log9.info(`Timed out ${memberId} for ${duration}ms in guild ${guildId}: ${reason || "No reason"}`);
|
|
1449
|
+
const cached = Bot.client.channels.cache.get(channelId);
|
|
1450
|
+
if (cached && !cached.isDMBased()) return cached;
|
|
1451
|
+
const channel = await Bot.client.channels.fetch(channelId);
|
|
1452
|
+
if (channel && !channel.isDMBased()) return channel;
|
|
1453
|
+
Log8.warn(`Channel ${channelId} not found in any guild`);
|
|
1454
|
+
return null;
|
|
1569
1455
|
} catch (error) {
|
|
1570
|
-
|
|
1571
|
-
|
|
1456
|
+
Log8.error(`Failed to find channel ${channelId}: ${error}`);
|
|
1457
|
+
return null;
|
|
1572
1458
|
}
|
|
1573
1459
|
}
|
|
1574
|
-
|
|
1575
|
-
* Remove the timeout of a member
|
|
1576
|
-
*/
|
|
1577
|
-
static async untimeout(guildId, memberId, reason) {
|
|
1460
|
+
static findAll(guildId) {
|
|
1578
1461
|
try {
|
|
1579
|
-
const
|
|
1580
|
-
if (!
|
|
1581
|
-
|
|
1462
|
+
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1463
|
+
if (!guild) {
|
|
1464
|
+
Log8.warn(`Guild ${guildId} not found`);
|
|
1465
|
+
return [];
|
|
1582
1466
|
}
|
|
1583
|
-
|
|
1584
|
-
Log9.info(`Untimed out ${memberId} in guild ${guildId}: ${reason || "No reason"}`);
|
|
1467
|
+
return Array.from(guild.channels.cache.values());
|
|
1585
1468
|
} catch (error) {
|
|
1586
|
-
|
|
1587
|
-
|
|
1469
|
+
Log8.error(`Failed to find channel ${guildId}: ${error}`);
|
|
1470
|
+
return [];
|
|
1588
1471
|
}
|
|
1589
1472
|
}
|
|
1590
1473
|
/**
|
|
1591
|
-
*
|
|
1474
|
+
* Recherche channels par nom (insensible à la casse)
|
|
1592
1475
|
*/
|
|
1593
|
-
static
|
|
1476
|
+
static findByName(guildId, name2) {
|
|
1594
1477
|
try {
|
|
1595
|
-
const
|
|
1596
|
-
if (!
|
|
1597
|
-
|
|
1478
|
+
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1479
|
+
if (!guild) {
|
|
1480
|
+
Log8.warn(`Guild ${guildId} not found`);
|
|
1481
|
+
return [];
|
|
1598
1482
|
}
|
|
1599
|
-
|
|
1600
|
-
|
|
1483
|
+
return Array.from(guild.channels.cache.values()).filter(
|
|
1484
|
+
(channel) => channel.name.toLowerCase().includes(name2.toLowerCase())
|
|
1485
|
+
);
|
|
1601
1486
|
} catch (error) {
|
|
1602
|
-
|
|
1603
|
-
|
|
1487
|
+
Log8.error(`Failed to find channel ${guildId}: ${error}`);
|
|
1488
|
+
return [];
|
|
1604
1489
|
}
|
|
1605
1490
|
}
|
|
1606
|
-
static async
|
|
1607
|
-
const guild = await GuildManager.find(guildId);
|
|
1608
|
-
if (!guild) {
|
|
1609
|
-
throw new Error(`Guild ${guildId} not found`);
|
|
1610
|
-
}
|
|
1491
|
+
static async _create(guildId, options) {
|
|
1611
1492
|
try {
|
|
1612
|
-
|
|
1613
|
-
|
|
1493
|
+
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1494
|
+
if (!guild) {
|
|
1495
|
+
throw new Error(`Guild ${guildId} not found`);
|
|
1496
|
+
}
|
|
1497
|
+
const channel = await guild.channels.create(options);
|
|
1498
|
+
Log8.info(`Created channel ${channel.name} (${channel.id}) in guild ${guildId}`);
|
|
1499
|
+
return channel;
|
|
1614
1500
|
} catch (error) {
|
|
1615
|
-
|
|
1501
|
+
Log8.error(`Failed to create channel in guild ${guildId}: ${error}`);
|
|
1616
1502
|
throw error;
|
|
1617
1503
|
}
|
|
1618
1504
|
}
|
|
1619
|
-
static async
|
|
1620
|
-
const guild = await GuildManager.find(guildId);
|
|
1621
|
-
if (!guild) {
|
|
1622
|
-
throw new Error(`Guild ${guildId} not found`);
|
|
1623
|
-
}
|
|
1505
|
+
static async delete(channelId) {
|
|
1624
1506
|
try {
|
|
1625
|
-
await
|
|
1626
|
-
|
|
1507
|
+
const channel = await _GuildChannelManager.find(channelId);
|
|
1508
|
+
if (!channel) {
|
|
1509
|
+
throw new Error(`Channel ${channelId} not found`);
|
|
1510
|
+
}
|
|
1511
|
+
await channel.delete();
|
|
1512
|
+
Log8.info(`Deleted channel ${channelId}`);
|
|
1513
|
+
return true;
|
|
1627
1514
|
} catch (error) {
|
|
1628
|
-
|
|
1629
|
-
|
|
1515
|
+
Log8.error(`Failed to delete channel ${channelId}: ${error}`);
|
|
1516
|
+
return false;
|
|
1630
1517
|
}
|
|
1631
1518
|
}
|
|
1632
1519
|
};
|
|
1633
1520
|
|
|
1634
1521
|
// src/manager/guild/RoleManager.ts
|
|
1635
|
-
import { Log as
|
|
1522
|
+
import { Log as Log9 } from "@spatulox/utils";
|
|
1636
1523
|
var RoleManager = class {
|
|
1637
1524
|
/**
|
|
1638
1525
|
* Add role - CACHE ONLY
|
|
@@ -1643,14 +1530,14 @@ var RoleManager = class {
|
|
|
1643
1530
|
try {
|
|
1644
1531
|
const role = member.guild.roles.cache.get(roleId);
|
|
1645
1532
|
if (!role) {
|
|
1646
|
-
|
|
1533
|
+
Log9.warn(`Role ${roleId} not found in cache`);
|
|
1647
1534
|
return false;
|
|
1648
1535
|
}
|
|
1649
1536
|
await member.roles.add(role);
|
|
1650
|
-
|
|
1537
|
+
Log9.info(`\u2705 Added ${role.name} to ${member.displayName}`);
|
|
1651
1538
|
return true;
|
|
1652
1539
|
} catch (error) {
|
|
1653
|
-
|
|
1540
|
+
Log9.error(`Failed to add role ${roleId}: ${error}`);
|
|
1654
1541
|
return false;
|
|
1655
1542
|
}
|
|
1656
1543
|
}
|
|
@@ -1661,23 +1548,16 @@ var RoleManager = class {
|
|
|
1661
1548
|
*/
|
|
1662
1549
|
static async remove(member, roleIdOrName) {
|
|
1663
1550
|
try {
|
|
1664
|
-
|
|
1665
|
-
if (typeof roleIdOrName === "string" && roleIdOrName.length === 18) {
|
|
1666
|
-
role = member.roles.cache.get(roleIdOrName);
|
|
1667
|
-
} else {
|
|
1668
|
-
role = member.roles.cache.find(
|
|
1669
|
-
(r) => r.id === roleIdOrName || r.name.toLowerCase() === roleIdOrName.toString().toLowerCase()
|
|
1670
|
-
);
|
|
1671
|
-
}
|
|
1551
|
+
const role = this.resolve(member.roles.cache, roleIdOrName);
|
|
1672
1552
|
if (!role) {
|
|
1673
|
-
|
|
1553
|
+
Log9.warn(`Role ${roleIdOrName} not found for ${member.displayName}`);
|
|
1674
1554
|
return false;
|
|
1675
1555
|
}
|
|
1676
1556
|
await member.roles.remove(role);
|
|
1677
|
-
|
|
1557
|
+
Log9.info(`\u2705 Removed ${role.name} from ${member.displayName}`);
|
|
1678
1558
|
return true;
|
|
1679
1559
|
} catch (error) {
|
|
1680
|
-
|
|
1560
|
+
Log9.error(`Failed to remove role ${roleIdOrName}: ${error}`);
|
|
1681
1561
|
return false;
|
|
1682
1562
|
}
|
|
1683
1563
|
}
|
|
@@ -1685,9 +1565,9 @@ var RoleManager = class {
|
|
|
1685
1565
|
* Toggle role (add/remove)
|
|
1686
1566
|
*/
|
|
1687
1567
|
static async toggle(member, roleIdOrName) {
|
|
1688
|
-
const role =
|
|
1568
|
+
const role = this.resolve(member.guild.roles.cache, roleIdOrName);
|
|
1689
1569
|
if (!role) {
|
|
1690
|
-
|
|
1570
|
+
Log9.warn(`Role ${roleIdOrName} not found`);
|
|
1691
1571
|
throw new Error(`Role not found`);
|
|
1692
1572
|
}
|
|
1693
1573
|
if (member.roles.cache.has(role.id)) {
|
|
@@ -1702,18 +1582,20 @@ var RoleManager = class {
|
|
|
1702
1582
|
* Check if member has role
|
|
1703
1583
|
*/
|
|
1704
1584
|
static hasRole(member, roleIdOrName) {
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1585
|
+
return !!this.resolve(member.roles.cache, roleIdOrName);
|
|
1586
|
+
}
|
|
1587
|
+
/**
|
|
1588
|
+
* Search by id first, then by name (case insensitive). Snowflakes are 17 to 20 digits long,
|
|
1589
|
+
* so the length of the string can't tell an id from a name
|
|
1590
|
+
*/
|
|
1591
|
+
static resolve(roles, roleIdOrName) {
|
|
1592
|
+
return roles.get(roleIdOrName) ?? roles.find((r) => r.name.toLowerCase() === roleIdOrName.toLowerCase());
|
|
1711
1593
|
}
|
|
1712
1594
|
};
|
|
1713
1595
|
|
|
1714
1596
|
// src/manager/guild/ChannelManager/ForumChannelManager.ts
|
|
1715
|
-
import { ChannelType as
|
|
1716
|
-
import { Log as
|
|
1597
|
+
import { ChannelType as ChannelType2, ForumChannel } from "discord.js";
|
|
1598
|
+
import { Log as Log10 } from "@spatulox/utils";
|
|
1717
1599
|
var ForumChannelManager = class extends GuildChannelManager {
|
|
1718
1600
|
static async findInGuild(guildId, channelId) {
|
|
1719
1601
|
const channel = await super.findInGuild(guildId, channelId);
|
|
@@ -1726,23 +1608,23 @@ var ForumChannelManager = class extends GuildChannelManager {
|
|
|
1726
1608
|
static findAll(guildId) {
|
|
1727
1609
|
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1728
1610
|
if (!guild) {
|
|
1729
|
-
|
|
1611
|
+
Log10.warn(`Guild ${guildId} not found`);
|
|
1730
1612
|
return [];
|
|
1731
1613
|
}
|
|
1732
1614
|
return Array.from(guild.channels.cache.values()).filter((c) => c instanceof ForumChannel);
|
|
1733
1615
|
}
|
|
1734
|
-
static async create(guildId,
|
|
1616
|
+
static async create(guildId, name2, options) {
|
|
1735
1617
|
return await super._create(guildId, {
|
|
1736
|
-
name,
|
|
1737
|
-
type:
|
|
1618
|
+
name: name2,
|
|
1619
|
+
type: ChannelType2.GuildForum,
|
|
1738
1620
|
...options
|
|
1739
1621
|
});
|
|
1740
1622
|
}
|
|
1741
1623
|
};
|
|
1742
1624
|
|
|
1743
1625
|
// src/manager/guild/ChannelManager/NewsChannelManager.ts
|
|
1744
|
-
import { ChannelType as
|
|
1745
|
-
import { Log as
|
|
1626
|
+
import { ChannelType as ChannelType3, NewsChannel } from "discord.js";
|
|
1627
|
+
import { Log as Log11 } from "@spatulox/utils";
|
|
1746
1628
|
var NewsChannelManager = class extends GuildChannelManager {
|
|
1747
1629
|
static async findInGuild(guildId, channelId) {
|
|
1748
1630
|
const channel = await super.findInGuild(guildId, channelId);
|
|
@@ -1755,23 +1637,23 @@ var NewsChannelManager = class extends GuildChannelManager {
|
|
|
1755
1637
|
static findAll(guildId) {
|
|
1756
1638
|
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1757
1639
|
if (!guild) {
|
|
1758
|
-
|
|
1640
|
+
Log11.warn(`Guild ${guildId} not found`);
|
|
1759
1641
|
return [];
|
|
1760
1642
|
}
|
|
1761
1643
|
return Array.from(guild.channels.cache.values()).filter((c) => c instanceof NewsChannel);
|
|
1762
1644
|
}
|
|
1763
|
-
static async create(guildId,
|
|
1645
|
+
static async create(guildId, name2, options) {
|
|
1764
1646
|
return await super._create(guildId, {
|
|
1765
|
-
name,
|
|
1766
|
-
type:
|
|
1647
|
+
name: name2,
|
|
1648
|
+
type: ChannelType3.GuildAnnouncement,
|
|
1767
1649
|
...options
|
|
1768
1650
|
});
|
|
1769
1651
|
}
|
|
1770
1652
|
};
|
|
1771
1653
|
|
|
1772
1654
|
// src/manager/guild/ChannelManager/StageChannelManager.ts
|
|
1773
|
-
import { ChannelType as
|
|
1774
|
-
import { Log as
|
|
1655
|
+
import { ChannelType as ChannelType4, StageChannel } from "discord.js";
|
|
1656
|
+
import { Log as Log12 } from "@spatulox/utils";
|
|
1775
1657
|
var StageChannelManager = class extends GuildChannelManager {
|
|
1776
1658
|
static async findInGuild(guildId, channelId) {
|
|
1777
1659
|
const channel = await super.findInGuild(guildId, channelId);
|
|
@@ -1784,21 +1666,54 @@ var StageChannelManager = class extends GuildChannelManager {
|
|
|
1784
1666
|
static findAll(guildId) {
|
|
1785
1667
|
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1786
1668
|
if (!guild) {
|
|
1787
|
-
|
|
1669
|
+
Log12.warn(`Guild ${guildId} not found`);
|
|
1788
1670
|
return [];
|
|
1789
1671
|
}
|
|
1790
1672
|
return Array.from(guild.channels.cache.values()).filter((c) => c instanceof StageChannel);
|
|
1791
1673
|
}
|
|
1792
|
-
static async create(guildId,
|
|
1674
|
+
static async create(guildId, name2, options) {
|
|
1675
|
+
return await super._create(guildId, {
|
|
1676
|
+
name: name2,
|
|
1677
|
+
type: ChannelType4.GuildStageVoice,
|
|
1678
|
+
...options
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
};
|
|
1682
|
+
|
|
1683
|
+
// src/manager/guild/ChannelManager/GuildTextChannelManager.ts
|
|
1684
|
+
import { TextChannel as TextChannel4, ChannelType as ChannelType5 } from "discord.js";
|
|
1685
|
+
import { Log as Log13 } from "@spatulox/utils";
|
|
1686
|
+
var GuildTextChannelManager = class extends GuildChannelManager {
|
|
1687
|
+
static async findInGuild(guildId, channelId) {
|
|
1688
|
+
const channel = await super.findInGuild(guildId, channelId);
|
|
1689
|
+
return channel instanceof TextChannel4 ? channel : null;
|
|
1690
|
+
}
|
|
1691
|
+
static async find(channelId) {
|
|
1692
|
+
const channel = await super.find(channelId);
|
|
1693
|
+
return channel instanceof TextChannel4 ? channel : null;
|
|
1694
|
+
}
|
|
1695
|
+
static findAll(guildId) {
|
|
1696
|
+
const guild = Bot.client.guilds.cache.get(guildId);
|
|
1697
|
+
if (!guild) {
|
|
1698
|
+
Log13.warn(`Guild ${guildId} not found`);
|
|
1699
|
+
return [];
|
|
1700
|
+
}
|
|
1701
|
+
return Array.from(guild.channels.cache.values()).filter((c) => c instanceof TextChannel4);
|
|
1702
|
+
}
|
|
1703
|
+
static async create(guildId, name2, options) {
|
|
1793
1704
|
return await super._create(guildId, {
|
|
1794
|
-
name,
|
|
1795
|
-
type: ChannelType5.
|
|
1705
|
+
name: name2,
|
|
1706
|
+
type: ChannelType5.GuildText,
|
|
1796
1707
|
...options
|
|
1797
1708
|
});
|
|
1798
1709
|
}
|
|
1799
1710
|
};
|
|
1800
1711
|
|
|
1801
1712
|
// src/manager/guild/ChannelManager/ThreadChannelManager.ts
|
|
1713
|
+
import {
|
|
1714
|
+
NewsChannel as NewsChannel2,
|
|
1715
|
+
TextChannel as TextChannel5
|
|
1716
|
+
} from "discord.js";
|
|
1802
1717
|
var ThreadChannelManager = class {
|
|
1803
1718
|
static async findInGuild(guildId, channelId) {
|
|
1804
1719
|
const channel = await GuildChannelManager.findInGuild(guildId, channelId);
|
|
@@ -1813,16 +1728,15 @@ var ThreadChannelManager = class {
|
|
|
1813
1728
|
return channels.filter((c) => c.isThread());
|
|
1814
1729
|
}
|
|
1815
1730
|
static async createFromChannel(parentId, options) {
|
|
1816
|
-
const channel = await
|
|
1817
|
-
if (!channel
|
|
1818
|
-
throw new Error("Parent must be a text
|
|
1731
|
+
const channel = await GuildChannelManager.find(parentId);
|
|
1732
|
+
if (!(channel instanceof TextChannel5) && !(channel instanceof NewsChannel2)) {
|
|
1733
|
+
throw new Error("Parent must be a text or announcement channel");
|
|
1819
1734
|
}
|
|
1820
1735
|
const thread = await channel.threads.create(options);
|
|
1821
1736
|
return thread;
|
|
1822
1737
|
}
|
|
1823
1738
|
static async createFromMessage(message, options) {
|
|
1824
|
-
|
|
1825
|
-
if (!channel) throw new Error("Message channel not found");
|
|
1739
|
+
if (!message.inGuild()) throw new Error("Cannot start a thread from a message outside of a guild");
|
|
1826
1740
|
return await message.startThread(options);
|
|
1827
1741
|
}
|
|
1828
1742
|
/**
|
|
@@ -1845,11 +1759,11 @@ import { Log as Log14 } from "@spatulox/utils";
|
|
|
1845
1759
|
var GuildVoiceChannelManager = class extends GuildChannelManager {
|
|
1846
1760
|
static async findInGuild(guildId, channelId) {
|
|
1847
1761
|
const channel = await super.findInGuild(guildId, channelId);
|
|
1848
|
-
return channel
|
|
1762
|
+
return channel instanceof VoiceChannel ? channel : null;
|
|
1849
1763
|
}
|
|
1850
1764
|
static async find(channelId) {
|
|
1851
1765
|
const channel = await super.find(channelId);
|
|
1852
|
-
return channel
|
|
1766
|
+
return channel instanceof VoiceChannel ? channel : null;
|
|
1853
1767
|
}
|
|
1854
1768
|
static findAll(guildId) {
|
|
1855
1769
|
const guild = Bot.client.guilds.cache.get(guildId);
|
|
@@ -1859,9 +1773,9 @@ var GuildVoiceChannelManager = class extends GuildChannelManager {
|
|
|
1859
1773
|
}
|
|
1860
1774
|
return Array.from(guild.channels.cache.values()).filter((c) => c instanceof VoiceChannel);
|
|
1861
1775
|
}
|
|
1862
|
-
static async create(guildId,
|
|
1776
|
+
static async create(guildId, name2, options) {
|
|
1863
1777
|
return await super._create(guildId, {
|
|
1864
|
-
name,
|
|
1778
|
+
name: name2,
|
|
1865
1779
|
type: ChannelType6.GuildVoice,
|
|
1866
1780
|
...options
|
|
1867
1781
|
});
|
|
@@ -1880,6 +1794,7 @@ var GuildChannelList = class {
|
|
|
1880
1794
|
};
|
|
1881
1795
|
|
|
1882
1796
|
// src/manager/guild/InviteManager.ts
|
|
1797
|
+
import { setTimeout as setTimeout3 } from "timers/promises";
|
|
1883
1798
|
import { Log as Log15 } from "@spatulox/utils";
|
|
1884
1799
|
var InviteManager = class {
|
|
1885
1800
|
/**
|
|
@@ -1907,9 +1822,19 @@ var InviteManager = class {
|
|
|
1907
1822
|
}
|
|
1908
1823
|
}
|
|
1909
1824
|
/**
|
|
1910
|
-
*
|
|
1825
|
+
* Find an invite by its code or its url (discord.gg/code, discord.com/invite/code)
|
|
1911
1826
|
*/
|
|
1912
|
-
static async
|
|
1827
|
+
static async find(codeOrUrl) {
|
|
1828
|
+
try {
|
|
1829
|
+
return await Bot.client.fetchInvite(codeOrUrl);
|
|
1830
|
+
} catch (error) {
|
|
1831
|
+
Log15.error(`Failed to find invite ${codeOrUrl}: ${error}`);
|
|
1832
|
+
return null;
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
static async delete(inviteOrCode) {
|
|
1836
|
+
const invite = typeof inviteOrCode === "string" ? await this.find(inviteOrCode) : inviteOrCode;
|
|
1837
|
+
if (!invite) return false;
|
|
1913
1838
|
try {
|
|
1914
1839
|
await invite.delete();
|
|
1915
1840
|
Log15.info(`Deleted invite ${invite.code} from guild ${invite.guild?.id}`);
|
|
@@ -1937,6 +1862,33 @@ var InviteManager = class {
|
|
|
1937
1862
|
throw error;
|
|
1938
1863
|
}
|
|
1939
1864
|
}
|
|
1865
|
+
/**
|
|
1866
|
+
* Check if an invite is older than maxAgeMs (default 1 hour).
|
|
1867
|
+
* Permanent invites (maxAge = 0) and excluded codes are never considered old
|
|
1868
|
+
*/
|
|
1869
|
+
static isOld(invite, excludedCodes = [], maxAgeMs = 60 * 60 * 1e3) {
|
|
1870
|
+
if (!invite.createdAt) return false;
|
|
1871
|
+
return invite.maxAge !== 0 && !excludedCodes.includes(invite.code) && invite.createdAt.getTime() < Date.now() - maxAgeMs;
|
|
1872
|
+
}
|
|
1873
|
+
/**
|
|
1874
|
+
* Delete every old invite of a guild, returns the number of deleted invites
|
|
1875
|
+
*/
|
|
1876
|
+
static async cleanup(guildId, excludedCodes = [], maxAgeMs) {
|
|
1877
|
+
try {
|
|
1878
|
+
const invites = await this.list(guildId);
|
|
1879
|
+
let deletedCount = 0;
|
|
1880
|
+
for (const invite of invites) {
|
|
1881
|
+
if (!this.isOld(invite, excludedCodes, maxAgeMs)) continue;
|
|
1882
|
+
if (await this.delete(invite)) deletedCount++;
|
|
1883
|
+
await setTimeout3(100);
|
|
1884
|
+
}
|
|
1885
|
+
Log15.info(`Invite cleanup ${guildId}: ${deletedCount} old invites deleted`);
|
|
1886
|
+
return deletedCount;
|
|
1887
|
+
} catch (error) {
|
|
1888
|
+
Log15.error(`Invite cleanup failed for ${guildId}: ${error}`);
|
|
1889
|
+
return 0;
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1940
1892
|
};
|
|
1941
1893
|
|
|
1942
1894
|
// src/manager/guild/GuildManager.ts
|
|
@@ -1972,9 +1924,9 @@ var GuildManager = class {
|
|
|
1972
1924
|
/**
|
|
1973
1925
|
* Search guild member by ID
|
|
1974
1926
|
*/
|
|
1975
|
-
static async searchMember(
|
|
1927
|
+
static async searchMember(guildId, memberId) {
|
|
1976
1928
|
try {
|
|
1977
|
-
return await this.user.findInGuild(guildId,
|
|
1929
|
+
return await this.user.findInGuild(guildId, memberId);
|
|
1978
1930
|
} catch (error) {
|
|
1979
1931
|
Log16.error(`Failed to fetch member ${memberId} in guild ${guildId}: ${error}`);
|
|
1980
1932
|
return null;
|
|
@@ -1983,9 +1935,9 @@ var GuildManager = class {
|
|
|
1983
1935
|
/**
|
|
1984
1936
|
* Check if member is still in guild
|
|
1985
1937
|
*/
|
|
1986
|
-
static async isMemberInGuild(
|
|
1938
|
+
static async isMemberInGuild(guildId, memberId) {
|
|
1987
1939
|
try {
|
|
1988
|
-
return await this.user.isInGuild(
|
|
1940
|
+
return await this.user.isInGuild(guildId, memberId);
|
|
1989
1941
|
} catch (error) {
|
|
1990
1942
|
return error.code !== 10007;
|
|
1991
1943
|
}
|
|
@@ -2145,7 +2097,7 @@ var ModalManager = class {
|
|
|
2145
2097
|
const modal = this.create(modalTitle ?? Bot.config?.botName ?? "Bot", customId);
|
|
2146
2098
|
const opt = {
|
|
2147
2099
|
...field,
|
|
2148
|
-
customId: `${customId}_input`,
|
|
2100
|
+
customId: field.customId ?? `${customId}_input`,
|
|
2149
2101
|
placeholder: "placeholder" in field && field.placeholder ? field.placeholder : `Enter ${field.label.toLowerCase()}`,
|
|
2150
2102
|
value: field.value
|
|
2151
2103
|
};
|
|
@@ -2155,21 +2107,23 @@ var ModalManager = class {
|
|
|
2155
2107
|
/**
|
|
2156
2108
|
* Title + Description modal preset
|
|
2157
2109
|
*/
|
|
2158
|
-
static titleDescription(customId, modalTitle, title,
|
|
2110
|
+
static titleDescription(customId, modalTitle, title, description2) {
|
|
2159
2111
|
const modal = this.create(modalTitle, customId);
|
|
2160
2112
|
const titleField = {
|
|
2161
2113
|
customId: `${customId}_title`,
|
|
2162
2114
|
label: title.label,
|
|
2163
2115
|
placeholder: title.placeholder ?? `Enter ${title.label.toLowerCase()}`,
|
|
2164
2116
|
type: 0 /* SHORT */,
|
|
2165
|
-
required: title.required
|
|
2117
|
+
required: title.required,
|
|
2118
|
+
value: title.value
|
|
2166
2119
|
};
|
|
2167
2120
|
const descField = {
|
|
2168
2121
|
customId: `${customId}_desc`,
|
|
2169
|
-
label:
|
|
2170
|
-
placeholder:
|
|
2122
|
+
label: description2.label,
|
|
2123
|
+
placeholder: description2.placeholder ?? `Enter ${description2.label.toLowerCase()}`,
|
|
2171
2124
|
type: 1 /* LONG */,
|
|
2172
|
-
required:
|
|
2125
|
+
required: description2.required,
|
|
2126
|
+
value: description2.value
|
|
2173
2127
|
};
|
|
2174
2128
|
modal.addLabelComponents(this._createField(titleField));
|
|
2175
2129
|
modal.addLabelComponents(this._createField(descField));
|
|
@@ -2177,21 +2131,25 @@ var ModalManager = class {
|
|
|
2177
2131
|
}
|
|
2178
2132
|
/**
|
|
2179
2133
|
* Date modal preset
|
|
2134
|
+
* @param suffix true (default) : the modal customId is `${customId}_date` and the input `${customId}_date_input`,
|
|
2135
|
+
* false : the modal keeps `customId` and the input is `${customId}_input`. Same for number() (_number) and phone() (_phone_number)
|
|
2180
2136
|
*/
|
|
2181
|
-
static date(customId, modalTitle = "Select Date", inputLabel = "Date") {
|
|
2182
|
-
return this.simple(`${customId}_date
|
|
2137
|
+
static date(customId, modalTitle = "Select Date", inputLabel = "Date", suffix = true) {
|
|
2138
|
+
return this.simple(suffix ? `${customId}_date` : customId, modalTitle, { label: inputLabel, type: 3 /* DATE */ });
|
|
2183
2139
|
}
|
|
2184
2140
|
/**
|
|
2185
2141
|
* Number modal preset
|
|
2142
|
+
* @param suffix See date() : `${customId}_number` by default, `customId` when false
|
|
2186
2143
|
*/
|
|
2187
|
-
static number(customId, modalTitle = "Enter a Number", inputLabel = "Number") {
|
|
2188
|
-
return this.simple(`${customId}_number
|
|
2144
|
+
static number(customId, modalTitle = "Enter a Number", inputLabel = "Number", suffix = true) {
|
|
2145
|
+
return this.simple(suffix ? `${customId}_number` : customId, modalTitle, { label: inputLabel, type: 2 /* NUMBER */, placeholder: "Enter a number" });
|
|
2189
2146
|
}
|
|
2190
2147
|
/**
|
|
2191
|
-
*
|
|
2148
|
+
* Phone modal preset
|
|
2149
|
+
* @param suffix See date() : `${customId}_phone_number` by default, `customId` when false
|
|
2192
2150
|
*/
|
|
2193
|
-
static phone(customId, modalTitle = "Enter a Phone number", inputLabel = "Phone number") {
|
|
2194
|
-
return this.simple(`${customId}_phone_number
|
|
2151
|
+
static phone(customId, modalTitle = "Enter a Phone number", inputLabel = "Phone number", suffix = true) {
|
|
2152
|
+
return this.simple(suffix ? `${customId}_phone_number` : customId, modalTitle, { label: inputLabel, type: 4 /* PHONE */, placeholder: "Enter a phone number" });
|
|
2195
2153
|
}
|
|
2196
2154
|
static add(modal, field) {
|
|
2197
2155
|
if (Array.isArray(field)) {
|
|
@@ -2202,7 +2160,7 @@ var ModalManager = class {
|
|
|
2202
2160
|
}
|
|
2203
2161
|
const opt = {
|
|
2204
2162
|
...field,
|
|
2205
|
-
customId: `${modal.data.custom_id}_${field.label}`
|
|
2163
|
+
customId: field.customId ?? `${modal.data.custom_id}_${field.label}`
|
|
2206
2164
|
};
|
|
2207
2165
|
modal.addLabelComponents(this._createField(opt));
|
|
2208
2166
|
return modal;
|
|
@@ -2221,32 +2179,36 @@ var ModalManager = class {
|
|
|
2221
2179
|
static parseDate(value) {
|
|
2222
2180
|
if (/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/.test(value)) {
|
|
2223
2181
|
const [year, month, day] = value.split("-").map(Number);
|
|
2224
|
-
|
|
2225
|
-
return isNaN(date.getTime()) ? null : date;
|
|
2182
|
+
return this.buildDate(year, month, day);
|
|
2226
2183
|
}
|
|
2227
2184
|
if (/^(0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-\d{4}$/.test(value)) {
|
|
2228
2185
|
const [day, month, year] = value.split("-").map(Number);
|
|
2229
|
-
|
|
2230
|
-
return isNaN(date.getTime()) ? null : date;
|
|
2186
|
+
return this.buildDate(year, month, day);
|
|
2231
2187
|
}
|
|
2232
2188
|
if (/^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/.test(value)) {
|
|
2233
2189
|
const [day, month, year] = value.split("/").map(Number);
|
|
2234
|
-
|
|
2235
|
-
return isNaN(date.getTime()) ? null : date;
|
|
2190
|
+
return this.buildDate(year, month, day);
|
|
2236
2191
|
}
|
|
2237
2192
|
if (/^\d{4}\/(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])$/.test(value)) {
|
|
2238
2193
|
const [year, month, day] = value.split("/").map(Number);
|
|
2239
|
-
|
|
2240
|
-
return isNaN(date.getTime()) ? null : date;
|
|
2194
|
+
return this.buildDate(year, month, day);
|
|
2241
2195
|
}
|
|
2242
2196
|
return null;
|
|
2243
2197
|
}
|
|
2198
|
+
/**
|
|
2199
|
+
* new Date() silently rolls an impossible date over (31/02 becomes 02/03) : reject any date that doesn't round-trip
|
|
2200
|
+
*/
|
|
2201
|
+
static buildDate(year, month, day) {
|
|
2202
|
+
const date = new Date(year, month - 1, day);
|
|
2203
|
+
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) return null;
|
|
2204
|
+
return date;
|
|
2205
|
+
}
|
|
2244
2206
|
};
|
|
2245
2207
|
|
|
2246
2208
|
// src/manager/messages/ComponentManager.ts
|
|
2247
2209
|
import {
|
|
2248
|
-
ContainerBuilder as
|
|
2249
|
-
TextDisplayBuilder,
|
|
2210
|
+
ContainerBuilder as ContainerBuilder2,
|
|
2211
|
+
TextDisplayBuilder as TextDisplayBuilder2,
|
|
2250
2212
|
SeparatorBuilder,
|
|
2251
2213
|
SeparatorSpacingSize,
|
|
2252
2214
|
MessageFlags as MessageFlags5,
|
|
@@ -2256,17 +2218,17 @@ import {
|
|
|
2256
2218
|
MediaGalleryItemBuilder,
|
|
2257
2219
|
AttachmentBuilder,
|
|
2258
2220
|
FileBuilder,
|
|
2259
|
-
ActionRowBuilder as
|
|
2221
|
+
ActionRowBuilder as ActionRowBuilder3
|
|
2260
2222
|
} from "discord.js";
|
|
2261
2223
|
var ComponentManager = class {
|
|
2262
2224
|
static get DEFAULT_COLOR() {
|
|
2263
|
-
return Bot.config?.defaultSimpleColor
|
|
2225
|
+
return Bot.config?.defaultSimpleColor ?? 6064856 /* default */;
|
|
2264
2226
|
}
|
|
2265
2227
|
/**
|
|
2266
2228
|
* Creates base ComponentV2
|
|
2267
2229
|
*/
|
|
2268
2230
|
static create(option) {
|
|
2269
|
-
const container = new
|
|
2231
|
+
const container = new ContainerBuilder2();
|
|
2270
2232
|
const colorC = option?.color ?? this.DEFAULT_COLOR;
|
|
2271
2233
|
if (colorC !== "transparent" /* transparent */) {
|
|
2272
2234
|
container.setAccentColor(colorC);
|
|
@@ -2274,7 +2236,7 @@ var ComponentManager = class {
|
|
|
2274
2236
|
if (option?.title || option?.thumbnailUrl) {
|
|
2275
2237
|
if (option?.thumbnailUrl) {
|
|
2276
2238
|
const headerSection = new SectionBuilder().addTextDisplayComponents(
|
|
2277
|
-
new
|
|
2239
|
+
new TextDisplayBuilder2().setContent(
|
|
2278
2240
|
option?.title ? option.title : "\u200B"
|
|
2279
2241
|
)
|
|
2280
2242
|
).setThumbnailAccessory(
|
|
@@ -2283,44 +2245,44 @@ var ComponentManager = class {
|
|
|
2283
2245
|
container.addSectionComponents(headerSection);
|
|
2284
2246
|
} else {
|
|
2285
2247
|
container.addTextDisplayComponents(
|
|
2286
|
-
new
|
|
2248
|
+
new TextDisplayBuilder2().setContent(option.title)
|
|
2287
2249
|
);
|
|
2288
2250
|
}
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2251
|
+
}
|
|
2252
|
+
if (option?.description) {
|
|
2253
|
+
container.addTextDisplayComponents(new TextDisplayBuilder2().setContent(option.description));
|
|
2254
|
+
}
|
|
2255
|
+
if (option?.separator && (option.title || option.thumbnailUrl || option.description)) {
|
|
2256
|
+
container.addSeparatorComponents(this.separator(option.separator));
|
|
2295
2257
|
}
|
|
2296
2258
|
return container;
|
|
2297
2259
|
}
|
|
2298
2260
|
/**
|
|
2299
2261
|
* Creates simple ComponentV2 with just description
|
|
2300
2262
|
*/
|
|
2301
|
-
static simple(
|
|
2302
|
-
return this.create({ color }).addTextDisplayComponents(new
|
|
2263
|
+
static simple(description2, color = null) {
|
|
2264
|
+
return this.create({ color }).addTextDisplayComponents(new TextDisplayBuilder2().setContent(description2)).addSeparatorComponents(this.separator());
|
|
2303
2265
|
}
|
|
2304
2266
|
/**
|
|
2305
2267
|
* Creates success ComponentV2
|
|
2306
2268
|
*/
|
|
2307
|
-
static success(
|
|
2308
|
-
return this.create({ title: "Success", color: 65280 /* success */ }).addTextDisplayComponents(new
|
|
2269
|
+
static success(description2) {
|
|
2270
|
+
return this.create({ title: "Success", color: 65280 /* success */ }).addTextDisplayComponents(new TextDisplayBuilder2().setContent(description2)).addSeparatorComponents(this.separator());
|
|
2309
2271
|
}
|
|
2310
2272
|
/**
|
|
2311
2273
|
* Creates debug ComponentV2
|
|
2312
2274
|
*/
|
|
2313
|
-
static debug(
|
|
2275
|
+
static debug(description2) {
|
|
2314
2276
|
return this.create({ title: "Debug", color: 25600 /* minecraft */ }).addTextDisplayComponents(
|
|
2315
|
-
new
|
|
2277
|
+
new TextDisplayBuilder2().setContent(description2)
|
|
2316
2278
|
).addSeparatorComponents(this.separator());
|
|
2317
2279
|
}
|
|
2318
2280
|
/**
|
|
2319
2281
|
* Creates error ComponentV2
|
|
2320
2282
|
*/
|
|
2321
|
-
static error(
|
|
2283
|
+
static error(description2) {
|
|
2322
2284
|
return this.create({ title: "Something went wrong", color: 8912917 /* error */ }).addTextDisplayComponents(
|
|
2323
|
-
new
|
|
2285
|
+
new TextDisplayBuilder2().setContent(description2)
|
|
2324
2286
|
).addSeparatorComponents(this.separator());
|
|
2325
2287
|
}
|
|
2326
2288
|
static separator(spacing = SeparatorSpacingSize.Small) {
|
|
@@ -2330,13 +2292,13 @@ var ComponentManager = class {
|
|
|
2330
2292
|
* Quick field adder
|
|
2331
2293
|
*/
|
|
2332
2294
|
static fieldAddText(container, options) {
|
|
2333
|
-
options.name && container.addTextDisplayComponents(new
|
|
2334
|
-
options.value && container.addTextDisplayComponents(new
|
|
2295
|
+
options.name && container.addTextDisplayComponents(new TextDisplayBuilder2().setContent(`__**${options.name}**__`));
|
|
2296
|
+
options.value && container.addTextDisplayComponents(new TextDisplayBuilder2().setContent(options.value));
|
|
2335
2297
|
}
|
|
2336
2298
|
static field(container, field) {
|
|
2337
2299
|
const hasText = ("name" in field || "value" in field) && (field.name || field.value);
|
|
2338
2300
|
if ("button" in field && Array.isArray(field.button) && field.button.length > 0) {
|
|
2339
|
-
const actionRow = new
|
|
2301
|
+
const actionRow = new ActionRowBuilder3().addComponents(field.button);
|
|
2340
2302
|
if (hasText) this.fieldAddText(container, { name: field.name, value: field.value });
|
|
2341
2303
|
container.addActionRowComponents(actionRow);
|
|
2342
2304
|
} else if ("button" in field && !Array.isArray(field.button) || "thumbnailUrl" in field) {
|
|
@@ -2351,7 +2313,7 @@ var ComponentManager = class {
|
|
|
2351
2313
|
container.addSectionComponents(section);
|
|
2352
2314
|
} else {
|
|
2353
2315
|
if ("button" in field && !Array.isArray(field.button)) {
|
|
2354
|
-
const actionRow = new
|
|
2316
|
+
const actionRow = new ActionRowBuilder3().addComponents(field.button);
|
|
2355
2317
|
container.addActionRowComponents(actionRow);
|
|
2356
2318
|
}
|
|
2357
2319
|
}
|
|
@@ -2420,11 +2382,14 @@ var ComponentManager = class {
|
|
|
2420
2382
|
});
|
|
2421
2383
|
return { container, files };
|
|
2422
2384
|
}
|
|
2385
|
+
/**
|
|
2386
|
+
* Returns a copy with the footer, so calling toMessage() / toInteraction() twice on the same
|
|
2387
|
+
* container doesn't stack footers
|
|
2388
|
+
*/
|
|
2423
2389
|
static footer(container) {
|
|
2424
|
-
container.addTextDisplayComponents(
|
|
2425
|
-
new
|
|
2390
|
+
return new ContainerBuilder2(container.toJSON()).addTextDisplayComponents(
|
|
2391
|
+
new TextDisplayBuilder2().setContent(`-# **${Bot.config?.botName || "Bot"} \xB7 <t:${Math.floor(Date.now() / 1e3)}:d> <t:${Math.floor(Date.now() / 1e3)}:t>**`)
|
|
2426
2392
|
);
|
|
2427
|
-
return container;
|
|
2428
2393
|
}
|
|
2429
2394
|
/**
|
|
2430
2395
|
* Transform ComponentV2 into object for channel.send()
|
|
@@ -2434,7 +2399,7 @@ var ComponentManager = class {
|
|
|
2434
2399
|
*/
|
|
2435
2400
|
static toMessage(container, file = null, footer = true) {
|
|
2436
2401
|
if (footer) {
|
|
2437
|
-
this.footer(container);
|
|
2402
|
+
container = this.footer(container);
|
|
2438
2403
|
}
|
|
2439
2404
|
if (file) {
|
|
2440
2405
|
return {
|
|
@@ -2448,13 +2413,13 @@ var ComponentManager = class {
|
|
|
2448
2413
|
flags: [MessageFlags5.IsComponentsV2]
|
|
2449
2414
|
};
|
|
2450
2415
|
}
|
|
2451
|
-
static toInteraction(container, file = null, footer = true) {
|
|
2416
|
+
static toInteraction(container, file = null, footer = true, ephemeral = false) {
|
|
2452
2417
|
if (footer) {
|
|
2453
|
-
this.footer(container);
|
|
2418
|
+
container = this.footer(container);
|
|
2454
2419
|
}
|
|
2455
2420
|
const base = {
|
|
2456
2421
|
components: [container],
|
|
2457
|
-
flags: [MessageFlags5.IsComponentsV2]
|
|
2422
|
+
flags: ephemeral ? [MessageFlags5.IsComponentsV2, MessageFlags5.Ephemeral] : [MessageFlags5.IsComponentsV2]
|
|
2458
2423
|
};
|
|
2459
2424
|
if (file) {
|
|
2460
2425
|
return {
|
|
@@ -2464,12 +2429,16 @@ var ComponentManager = class {
|
|
|
2464
2429
|
}
|
|
2465
2430
|
return base;
|
|
2466
2431
|
}
|
|
2467
|
-
|
|
2432
|
+
/**
|
|
2433
|
+
* Options to edit an existing message : message.edit(). The Components V2 flag is set, so a legacy message can be turned into a V2 one
|
|
2434
|
+
*/
|
|
2435
|
+
static toMessageUpdate(container, file = null, footer = true) {
|
|
2468
2436
|
if (footer) {
|
|
2469
|
-
this.footer(container);
|
|
2437
|
+
container = this.footer(container);
|
|
2470
2438
|
}
|
|
2471
2439
|
const base = {
|
|
2472
|
-
components: [container]
|
|
2440
|
+
components: [container],
|
|
2441
|
+
flags: [MessageFlags5.IsComponentsV2]
|
|
2473
2442
|
};
|
|
2474
2443
|
if (file) {
|
|
2475
2444
|
return {
|
|
@@ -2479,10 +2448,16 @@ var ComponentManager = class {
|
|
|
2479
2448
|
}
|
|
2480
2449
|
return base;
|
|
2481
2450
|
}
|
|
2451
|
+
/**
|
|
2452
|
+
* Options for interaction.update() and interaction.editReply() (buttons, select menus, modals, deferred replies), with the Components V2 flag
|
|
2453
|
+
*/
|
|
2454
|
+
static toInteractionUpdate(container, file = null, footer = true) {
|
|
2455
|
+
return this.toMessageUpdate(container, file, footer);
|
|
2456
|
+
}
|
|
2482
2457
|
};
|
|
2483
2458
|
|
|
2484
2459
|
// src/manager/messages/ChartManager.ts
|
|
2485
|
-
import { TextDisplayBuilder as
|
|
2460
|
+
import { TextDisplayBuilder as TextDisplayBuilder3 } from "discord.js";
|
|
2486
2461
|
var SPARK_BLOCKS = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"];
|
|
2487
2462
|
var DEFAULT_MAX_POINTS = 50;
|
|
2488
2463
|
var EMPTY = "\u2014";
|
|
@@ -2492,7 +2467,7 @@ var ChartManager = class _ChartManager {
|
|
|
2492
2467
|
*/
|
|
2493
2468
|
static progressBar(label, value, max = 100, options = {}) {
|
|
2494
2469
|
const line = `${label} : ${_ChartManager.renderBar(value, max, options)}`;
|
|
2495
|
-
return new
|
|
2470
|
+
return new TextDisplayBuilder3().setContent(_ChartManager.wrap(line, options.codeBlock));
|
|
2496
2471
|
}
|
|
2497
2472
|
/**
|
|
2498
2473
|
* Several gauges in ONE TextDisplay, so a dashboard of 6 metrics still costs 1 component.
|
|
@@ -2504,7 +2479,7 @@ var ChartManager = class _ChartManager {
|
|
|
2504
2479
|
const opts = { ...options, ...row.options };
|
|
2505
2480
|
return `${row.label.padEnd(width, " ")} : ${_ChartManager.renderBar(row.value, row.max ?? 100, opts)}`;
|
|
2506
2481
|
});
|
|
2507
|
-
return new
|
|
2482
|
+
return new TextDisplayBuilder3().setContent(_ChartManager.wrap(lines.join("\n"), options.codeBlock));
|
|
2508
2483
|
}
|
|
2509
2484
|
/**
|
|
2510
2485
|
* A one line curve : `RAM : ▁▂▃▅▇█▆▄▃▂▁ 62 %`. One block character per point, the last
|
|
@@ -2512,7 +2487,7 @@ var ChartManager = class _ChartManager {
|
|
|
2512
2487
|
*/
|
|
2513
2488
|
static sparkline(label, values, options = {}) {
|
|
2514
2489
|
const line = `${label} : ${_ChartManager.renderSpark(values, options)}`;
|
|
2515
|
-
return new
|
|
2490
|
+
return new TextDisplayBuilder3().setContent(_ChartManager.wrap(line, options.codeBlock));
|
|
2516
2491
|
}
|
|
2517
2492
|
/**
|
|
2518
2493
|
* Several curves in ONE TextDisplay, the sparkline counterpart of progressBars().
|
|
@@ -2525,7 +2500,7 @@ var ChartManager = class _ChartManager {
|
|
|
2525
2500
|
const opts = { ...base, ...row.options };
|
|
2526
2501
|
return `${row.label.padEnd(width, " ")} : ${_ChartManager.renderSpark(row.values, opts)}`;
|
|
2527
2502
|
});
|
|
2528
|
-
return new
|
|
2503
|
+
return new TextDisplayBuilder3().setContent(_ChartManager.wrap(lines.join("\n"), options.codeBlock));
|
|
2529
2504
|
}
|
|
2530
2505
|
/**
|
|
2531
2506
|
* Only fills the bounds that were not given explicitly, and only when asked : a row
|
|
@@ -2590,12 +2565,15 @@ var ChartManager = class _ChartManager {
|
|
|
2590
2565
|
import {
|
|
2591
2566
|
ButtonBuilder as ButtonBuilder2,
|
|
2592
2567
|
ButtonStyle,
|
|
2593
|
-
ActionRowBuilder as
|
|
2568
|
+
ActionRowBuilder as ActionRowBuilder4,
|
|
2594
2569
|
MessageFlags as MessageFlags6
|
|
2595
2570
|
} from "discord.js";
|
|
2596
2571
|
var ButtonManager = class _ButtonManager {
|
|
2597
2572
|
static create(options) {
|
|
2598
|
-
const btn = new ButtonBuilder2().setCustomId(options.customId).
|
|
2573
|
+
const btn = new ButtonBuilder2().setCustomId(options.customId).setStyle(options.style).setDisabled(options.disabled ?? false);
|
|
2574
|
+
if (options.label || !options.emoji) {
|
|
2575
|
+
btn.setLabel(options.label ?? "Button");
|
|
2576
|
+
}
|
|
2599
2577
|
if (options.emoji) {
|
|
2600
2578
|
btn.setEmoji(options.emoji);
|
|
2601
2579
|
}
|
|
@@ -2626,7 +2604,7 @@ var ButtonManager = class _ButtonManager {
|
|
|
2626
2604
|
}
|
|
2627
2605
|
static row(but) {
|
|
2628
2606
|
const buttons = Array.isArray(but) ? but.slice(0, 5) : [but];
|
|
2629
|
-
return new
|
|
2607
|
+
return new ActionRowBuilder4().addComponents(buttons);
|
|
2630
2608
|
}
|
|
2631
2609
|
static toMessage(button) {
|
|
2632
2610
|
return {
|
|
@@ -2639,37 +2617,155 @@ var ButtonManager = class _ButtonManager {
|
|
|
2639
2617
|
flags: ephemeral ? [MessageFlags6.Ephemeral] : []
|
|
2640
2618
|
};
|
|
2641
2619
|
}
|
|
2642
|
-
|
|
2620
|
+
/**
|
|
2621
|
+
* Options to edit an existing message : message.edit()
|
|
2622
|
+
*/
|
|
2623
|
+
static toMessageUpdate(button) {
|
|
2643
2624
|
return {
|
|
2644
2625
|
components: this.createRowsToReturn(button)
|
|
2645
2626
|
};
|
|
2646
2627
|
}
|
|
2628
|
+
/**
|
|
2629
|
+
* Options for interaction.update() and interaction.editReply() (buttons, select menus, modals, deferred replies)
|
|
2630
|
+
*/
|
|
2631
|
+
static toInteractionUpdate(button) {
|
|
2632
|
+
return this.toMessageUpdate(button);
|
|
2633
|
+
}
|
|
2634
|
+
/**
|
|
2635
|
+
* Consecutive buttons are grouped by 5 in the same row (like row() does), action rows are kept as is
|
|
2636
|
+
*/
|
|
2647
2637
|
static createRowsToReturn(button) {
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2638
|
+
const items = Array.isArray(button) ? button : [button];
|
|
2639
|
+
const rows = [];
|
|
2640
|
+
let pending = [];
|
|
2641
|
+
const flush = () => {
|
|
2642
|
+
for (let i = 0; i < pending.length; i += 5) {
|
|
2643
|
+
rows.push(_ButtonManager.row(pending.slice(i, i + 5)));
|
|
2644
|
+
}
|
|
2645
|
+
pending = [];
|
|
2646
|
+
};
|
|
2647
|
+
for (const item of items) {
|
|
2648
|
+
if (item instanceof ActionRowBuilder4) {
|
|
2649
|
+
flush();
|
|
2650
|
+
rows.push(item);
|
|
2651
|
+
} else {
|
|
2652
|
+
pending.push(item);
|
|
2653
|
+
}
|
|
2654
|
+
}
|
|
2655
|
+
flush();
|
|
2656
|
+
return rows;
|
|
2657
|
+
}
|
|
2658
|
+
};
|
|
2659
|
+
|
|
2660
|
+
// src/manager/interactible/PaginationManager.ts
|
|
2661
|
+
import {
|
|
2662
|
+
ActionRowBuilder as ActionRowBuilder5,
|
|
2663
|
+
ComponentType,
|
|
2664
|
+
MessageFlags as MessageFlags7
|
|
2665
|
+
} from "discord.js";
|
|
2666
|
+
import { Log as Log18 } from "@spatulox/utils";
|
|
2667
|
+
var PaginationManager = class {
|
|
2668
|
+
/**
|
|
2669
|
+
* Reply to the interaction (or edit the deferred reply) with the first page and navigation buttons.
|
|
2670
|
+
* Only the user who triggered the interaction can navigate, and the buttons are disabled when the
|
|
2671
|
+
* collector ends.
|
|
2672
|
+
*/
|
|
2673
|
+
static async send(interaction, pages, options = {}) {
|
|
2674
|
+
if (pages.length === 0) {
|
|
2675
|
+
Log18.error("PaginationManager: at least one page is required");
|
|
2676
|
+
return null;
|
|
2677
|
+
}
|
|
2678
|
+
const prefix = `pag_${interaction.id}_`;
|
|
2679
|
+
let currentPage = 0;
|
|
2680
|
+
const payload = (disabled = false) => {
|
|
2681
|
+
const page = pages[currentPage];
|
|
2682
|
+
return {
|
|
2683
|
+
content: page.content ?? "",
|
|
2684
|
+
embeds: [page.embed],
|
|
2685
|
+
components: [this.row(prefix, currentPage, pages.length, disabled)]
|
|
2686
|
+
};
|
|
2687
|
+
};
|
|
2688
|
+
let message;
|
|
2689
|
+
if (interaction.deferred || interaction.replied) {
|
|
2690
|
+
message = await interaction.editReply(payload());
|
|
2691
|
+
} else {
|
|
2692
|
+
const response = await interaction.reply({
|
|
2693
|
+
...payload(),
|
|
2694
|
+
flags: options.ephemeral ? [MessageFlags7.Ephemeral] : [],
|
|
2695
|
+
withResponse: true
|
|
2696
|
+
});
|
|
2697
|
+
message = response.resource?.message ?? await interaction.fetchReply();
|
|
2652
2698
|
}
|
|
2653
|
-
|
|
2699
|
+
const collector = message.createMessageComponentCollector({
|
|
2700
|
+
componentType: ComponentType.Button,
|
|
2701
|
+
filter: (i) => i.customId.startsWith(prefix),
|
|
2702
|
+
time: options.time ?? 6e4,
|
|
2703
|
+
idle: options.idle ?? 15e3
|
|
2704
|
+
});
|
|
2705
|
+
collector.on("collect", async (i) => {
|
|
2706
|
+
try {
|
|
2707
|
+
if (i.user.id !== interaction.user.id) {
|
|
2708
|
+
await i.reply({ content: "Only the user who opened this pagination can use it", flags: [MessageFlags7.Ephemeral] });
|
|
2709
|
+
return;
|
|
2710
|
+
}
|
|
2711
|
+
switch (i.customId.slice(prefix.length)) {
|
|
2712
|
+
case "first":
|
|
2713
|
+
currentPage = 0;
|
|
2714
|
+
break;
|
|
2715
|
+
case "prev":
|
|
2716
|
+
currentPage = Math.max(0, currentPage - 1);
|
|
2717
|
+
break;
|
|
2718
|
+
case "next":
|
|
2719
|
+
currentPage = Math.min(pages.length - 1, currentPage + 1);
|
|
2720
|
+
break;
|
|
2721
|
+
case "last":
|
|
2722
|
+
currentPage = pages.length - 1;
|
|
2723
|
+
break;
|
|
2724
|
+
}
|
|
2725
|
+
await i.update(payload());
|
|
2726
|
+
} catch (error) {
|
|
2727
|
+
Log18.error(`PaginationManager: failed to update the page: ${error}`);
|
|
2728
|
+
}
|
|
2729
|
+
});
|
|
2730
|
+
collector.on("end", async () => {
|
|
2731
|
+
await interaction.editReply({ components: [this.row(prefix, currentPage, pages.length, true)] }).catch((error) => Log18.warn(`PaginationManager: failed to disable the buttons: ${error}`));
|
|
2732
|
+
});
|
|
2733
|
+
return collector;
|
|
2734
|
+
}
|
|
2735
|
+
static row(prefix, currentPage, pageCount, disabled) {
|
|
2736
|
+
const isFirst = currentPage === 0;
|
|
2737
|
+
const isLast = currentPage === pageCount - 1;
|
|
2738
|
+
return new ActionRowBuilder5().addComponents(
|
|
2739
|
+
ButtonManager.secondary({ customId: `${prefix}first`, emoji: "\u23EA", disabled: disabled || isFirst }),
|
|
2740
|
+
ButtonManager.secondary({ customId: `${prefix}prev`, emoji: "\u2B05\uFE0F", disabled: disabled || isFirst }),
|
|
2741
|
+
// Page indicator only : always disabled, it can't be clicked
|
|
2742
|
+
ButtonManager.secondary({ customId: `${prefix}page`, label: `${currentPage + 1}/${pageCount}`, disabled: true }),
|
|
2743
|
+
ButtonManager.secondary({ customId: `${prefix}next`, emoji: "\u27A1\uFE0F", disabled: disabled || isLast }),
|
|
2744
|
+
ButtonManager.secondary({ customId: `${prefix}last`, emoji: "\u23E9", disabled: disabled || isLast })
|
|
2745
|
+
);
|
|
2654
2746
|
}
|
|
2655
2747
|
};
|
|
2656
2748
|
|
|
2657
2749
|
// src/index.ts
|
|
2658
2750
|
import { Time as Time3 } from "@spatulox/utils";
|
|
2659
|
-
import { Log as
|
|
2751
|
+
import { Log as Log19 } from "@spatulox/utils";
|
|
2660
2752
|
import { SimpleMutex } from "@spatulox/utils";
|
|
2661
2753
|
|
|
2662
2754
|
// src/constants/DiscordRegex.ts
|
|
2663
|
-
var DiscordRegex = class {
|
|
2755
|
+
var DiscordRegex = class _DiscordRegex {
|
|
2664
2756
|
// Caractères spéciaux
|
|
2665
2757
|
static SPACE = "\u200B";
|
|
2666
2758
|
// URLs basiques
|
|
2667
2759
|
static URL_REGEX = /(https?:\/\/[^\s<>]+)/i;
|
|
2760
|
+
// URL on a Discord domain (discord.com, discord.gg, discordapp.com, cdn.discordapp.com, discord.media...)
|
|
2761
|
+
static DISCORD_URL = /^https?:\/\/(?:[\w-]+\.)*(?:discord\.com|discord\.gg|discordapp\.com|discordapp\.net|discord\.media)(?:[\/?#]\S*)?$/i;
|
|
2668
2762
|
/* DISCORD REGEX */
|
|
2669
|
-
|
|
2670
|
-
static
|
|
2671
|
-
|
|
2672
|
-
static
|
|
2763
|
+
// Snowflakes (user, bot, channel, role, guild ids) are 17 to 20 digits long, their length says nothing about their type
|
|
2764
|
+
static USER_REGEX = /<@!?\d{17,20}>/;
|
|
2765
|
+
/** @deprecated A bot mention can't be told apart from a user mention : same as USER_REGEX */
|
|
2766
|
+
static BOT_REGEX = _DiscordRegex.USER_REGEX;
|
|
2767
|
+
static CHANNEL_REGEX = /(<#\d{17,20}>)|(<id:(browse|customize|guide)>)/;
|
|
2768
|
+
static ROLE_REGEX = /<@&\d{17,20}>/;
|
|
2673
2769
|
/**
|
|
2674
2770
|
* Mention a User
|
|
2675
2771
|
* Mention a Role
|
|
@@ -2686,32 +2782,33 @@ var DiscordRegex = class {
|
|
|
2686
2782
|
`(${this.DISCORD_PING_REGEX.source})|(${this.CHANNEL_REGEX.source})`
|
|
2687
2783
|
);
|
|
2688
2784
|
// ID Discord (user, channel guild)
|
|
2689
|
-
static
|
|
2690
|
-
static
|
|
2691
|
-
static
|
|
2692
|
-
static
|
|
2785
|
+
static SNOWFLAKE = /^[0-9]{17,20}$/;
|
|
2786
|
+
static USER_ID = _DiscordRegex.SNOWFLAKE;
|
|
2787
|
+
static CHANNEL_ID = _DiscordRegex.SNOWFLAKE;
|
|
2788
|
+
static GUILD_ID = _DiscordRegex.SNOWFLAKE;
|
|
2789
|
+
static BOT_ID = _DiscordRegex.SNOWFLAKE;
|
|
2693
2790
|
// Username Discord (2-32 caractères alphanumériques + _ .)
|
|
2694
|
-
static USERNAME = /^[a-zA-Z0-9_]{2,32}$/;
|
|
2791
|
+
static USERNAME = /^[a-zA-Z0-9_.]{2,32}$/;
|
|
2695
2792
|
// Username + discrim (ancien format)
|
|
2696
2793
|
static USERNAME_DISCRIM = /^[a-zA-Z0-9_]{2,32}#\d{4}$/;
|
|
2697
2794
|
// Channel mention <#123456789012345678>
|
|
2698
|
-
static CHANNEL_MENTION = /^<#([0-9]{
|
|
2795
|
+
static CHANNEL_MENTION = /^<#([0-9]{17,20})>$/;
|
|
2699
2796
|
// User mention <@123456789012345678> ou <@!123456789012345678>
|
|
2700
|
-
static USER_MENTION = /^<@!?([0-9]{
|
|
2797
|
+
static USER_MENTION = /^<@!?([0-9]{17,20})>$/;
|
|
2701
2798
|
// Role mention <@&123456789012345678>
|
|
2702
|
-
static ROLE_MENTION = /^<@&([0-9]{
|
|
2703
|
-
// URL Invite Discord
|
|
2704
|
-
static INVITE = /^discord(?:app
|
|
2705
|
-
// Emoji Discord (custom or unicode)
|
|
2706
|
-
static EMOJI = /^<a?:[a-zA-Z0-9_]{2,32}:[0-9]{
|
|
2799
|
+
static ROLE_MENTION = /^<@&([0-9]{17,20})>$/;
|
|
2800
|
+
// URL Invite Discord : discord.gg/code, discord.com/invite/code, discordapp.com/invite/code (with or without https://)
|
|
2801
|
+
static INVITE = /^(?:https?:\/\/)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com\/invite)\/[a-zA-Z0-9-]+$/i;
|
|
2802
|
+
// Emoji Discord (custom or unicode, skin tones / ZWJ sequences / flags included)
|
|
2803
|
+
static EMOJI = /^<a?:[a-zA-Z0-9_]{2,32}:[0-9]{17,20}>$|^[\p{Extended_Pictographic}\p{Regional_Indicator}][\p{Extended_Pictographic}\p{Regional_Indicator}\p{Emoji_Modifier}\u200D\uFE0F]*$/u;
|
|
2707
2804
|
/**
|
|
2708
|
-
* Validate Discord ID
|
|
2805
|
+
* Validate a Discord ID (snowflake, 17 to 20 digits)
|
|
2709
2806
|
*/
|
|
2710
2807
|
static isDiscordId(id) {
|
|
2711
|
-
return this.
|
|
2808
|
+
return this.SNOWFLAKE.test(id);
|
|
2712
2809
|
}
|
|
2713
2810
|
/**
|
|
2714
|
-
*
|
|
2811
|
+
* @deprecated A bot mention can't be told apart from a user mention : same as isUserMention()
|
|
2715
2812
|
*/
|
|
2716
2813
|
static isBotMention(mention) {
|
|
2717
2814
|
return this.BOT_REGEX.test(mention);
|
|
@@ -2729,7 +2826,7 @@ var DiscordRegex = class {
|
|
|
2729
2826
|
return this.USERNAME.test(username);
|
|
2730
2827
|
}
|
|
2731
2828
|
/**
|
|
2732
|
-
*
|
|
2829
|
+
* @deprecated Same as isDiscordId()
|
|
2733
2830
|
*/
|
|
2734
2831
|
static isDiscordIdType(id) {
|
|
2735
2832
|
return this.isDiscordId(id);
|
|
@@ -2738,7 +2835,7 @@ var DiscordRegex = class {
|
|
|
2738
2835
|
* Any Discord URL
|
|
2739
2836
|
*/
|
|
2740
2837
|
static isDiscordUrl(url) {
|
|
2741
|
-
return this.
|
|
2838
|
+
return this.DISCORD_URL.test(url) || this.INVITE.test(url);
|
|
2742
2839
|
}
|
|
2743
2840
|
/**
|
|
2744
2841
|
* Any discord mention
|
|
@@ -2753,15 +2850,18 @@ var DiscordRegex = class {
|
|
|
2753
2850
|
return {
|
|
2754
2851
|
SPACE: this.SPACE,
|
|
2755
2852
|
URL_REGEX: this.URL_REGEX,
|
|
2853
|
+
DISCORD_URL: this.DISCORD_URL,
|
|
2756
2854
|
USER_REGEX: this.USER_REGEX,
|
|
2757
2855
|
BOT_REGEX: this.BOT_REGEX,
|
|
2758
2856
|
CHANNEL_REGEX: this.CHANNEL_REGEX,
|
|
2759
2857
|
ROLE_REGEX: this.ROLE_REGEX,
|
|
2760
2858
|
DISCORD_PING_REGEX: this.DISCORD_PING_REGEX,
|
|
2761
2859
|
DISCORD_MENTION_REGEX: this.DISCORD_MENTION_REGEX,
|
|
2860
|
+
SNOWFLAKE: this.SNOWFLAKE,
|
|
2762
2861
|
USER_ID: this.USER_ID,
|
|
2763
2862
|
CHANNEL_ID: this.CHANNEL_ID,
|
|
2764
2863
|
GUILD_ID: this.GUILD_ID,
|
|
2864
|
+
BOT_ID: this.BOT_ID,
|
|
2765
2865
|
USERNAME: this.USERNAME,
|
|
2766
2866
|
USERNAME_DISCRIM: this.USERNAME_DISCRIM,
|
|
2767
2867
|
CHANNEL_MENTION: this.CHANNEL_MENTION,
|
|
@@ -2783,9 +2883,10 @@ export {
|
|
|
2783
2883
|
EmbedManager,
|
|
2784
2884
|
FileManager,
|
|
2785
2885
|
GuildManager,
|
|
2786
|
-
|
|
2886
|
+
Log19 as Log,
|
|
2787
2887
|
ModalFieldType,
|
|
2788
2888
|
ModalManager,
|
|
2889
|
+
PaginationManager,
|
|
2789
2890
|
ReactionManager,
|
|
2790
2891
|
SelectMenuManager,
|
|
2791
2892
|
SimpleColor,
|