-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbf.js
834 lines (729 loc) · 32.4 KB
/
dbf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/**
* Made by The Yule (theyule.xyz) at Yoo-Babobo (yoo-babobo.com).
* A Discord bot library to make the development of a bot a little bit easier.
*/
"use strict";
const fs = require("fs");
/**
* Main class for managing a Discord bot
*/
class DiscordBot {
/**
* Constructor for creating a Discord bot
* @param {Object} data All the data that the library needs to function
* @returns {DiscordBot} Discord bot
*/
constructor(data = {
/**
* Discord API
*/
discordApi: null,
/**
* Rest API
*/
restApi: null,
/**
* Discord client. Used to access the bot itself
*/
client: null,
/**
* Discord client options. Discord client intents are required
*/
clientOptions: [],
/**
* Discord bot token `REQUIRED`
*/
token: "",
/**
* Discord bot prefixes. Defaults to `!`. Note that this *is* an array, so you can have multiple prefixes
*/
prefixes: ["!"],
/**
* Discord bot owners
*/
owners: [],
/**
* Discord bot blocked users. **This feature is currently broken**
*/
blockedUsers: [],
/**
* Discord bot statuses
* ```json
* [
{
"name": "!help",
"type": "PLAYING",
"status": "idle"
},
{
"name": "I'm a Discord bot!",
"type": "WATCHING",
"status": "dnd"
}
]
* ```
*/
statuses: [],
/**
* Discord bot status interval in which the Discord bot status should be updated. Defaults to `15000` (15 seconds)
*/
statusInterval: 15000,
/**
* Discord bot responses. Defaults to:
* ```json
* "command_unknown": [
"This command doesn't exist"
],
"command_error": [
"Something went wrong: ```{{error}}```"
],
"command_cooldown": [
"Please wait `{{cooldown}}` second{{s}} before using this command again"
],
"command_guild_only": [
"This command can only be used in a server"
],
"command_dms_only": [
"This command can only be used in DMs"
],
"command_owners_only": [
"Only bot owners can use this command"
],
"command_blocked": [
"You are blocked from using this command"
],
"command_no_permission": [
"You don't have permission to use this command"
],
"command_no_bot_permission": [
"I don't have permission to use this command"
],
"command_incorrect_usage": [
"Please use this command correctly: `{{usage}}`"
]
* ```
*/
responses: {
"command_unknown": [
"This command doesn't exist"
],
"command_error": [
"Something went wrong: ```{{error}}```"
],
"command_cooldown": [
"Please wait `{{cooldown}}` second{{s}} before using this command again"
],
"command_guild_only": [
"This command can only be used in a server"
],
"command_dms_only": [
"This command can only be used in DMs"
],
"command_owners_only": [
"Only bot owners can use this command"
],
"command_blocked": [
"You are blocked from using this command"
],
"command_no_permission": [
"You don't have permission to use this command"
],
"command_no_bot_permission": [
"I don't have permission to use this command"
],
"command_incorrect_usage": [
"Please use this command correctly: `{{usage}}`"
]
},
/**
* Discord bot slash commands. Defaults to `false`
*/
slash: false,
/**
* Discord bot root directory path in which all subfolder that need to be loaded are found. Defaults to `./`
*/
path: ".",
/**
* Discord bot commands folder in which all Discord bot command files are located. Defaults to `command`
*/
commandsPath: "commands",
/**
* Discord bot commands folder in which all Discord bot event files are located. Defaults to `events`
*/
eventsPath: "events",
/**
* Discord bot buttons folder in which all Discord bot event button are located. Defaults to `buttons`
*/
buttonsPath: "buttons",
/**
* Discord bot select menus folder in which all Discord bot select menu files are located. Defaults to `selects`
*/
selectMenusPath: "selects",
/**
* Discord bot slash commands folder in which all Discord bot slash command files are located. Defaults to `slash`
*/
slashPath: "slash",
/**
* Log information to console. Defaults to `true`
*/
logging: true,
/**
* Discord ot development. Defaults to `true`
*/
dev: true,
/**
* Discord bot development guild id
*/
devGuildId: ""
}) {
/**
* Discord API
*/
this.discord = data.discordApi || null;
/**
* Discord client. Used to access the bot itself
*/
this.client = new this.discord.Client(data.clientOptions || []);
/**
* Discord bot token `REQUIRED`
*/
this.token = data.token || "";
/**
* Discord bot data
*/
this.data = data || {};
/**
* Discord client options. Discord client intents are required for v13 and higher
*/
this.options = data.clientOptions || [];
/**
* Discord bot responce functions. Example:
* ```javascript
* const bot = new DiscordBot({ data });
*
* bot.client.on("messageCreate", message => {
* bot.responses.command_unknown(message, {
* command_name: "command",
* prefix: "!"
* });
* });
* ```
*/
this.responses = {
/**
* Unknown command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_unknown: (message, data) => {
const msg = this.data.responses.command_unknown[Math.floor(Math.random() * this.data.responses.command_unknown.length)]
.replace(/{{command}}/g, data.command_name)
.replace(/{{prefix}}/g, data.prefix);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setTitle("Unknown Command")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Error command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_error: (message, data) => {
const msg = this.data.responses.command_error[Math.floor(Math.random() * this.data.responses.command_error.length)]
.replace(/{{error}}/g, data.error)
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setTitle("Error")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Cooldown command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_cooldown: (message, data) => {
const msg = this.data.responses.command_cooldown[Math.floor(Math.random() * this.data.responses.command_cooldown.length)]
.replace(/{{s}}/g, data.s)
.replace(/{{cooldown}}/g, data.time_left)
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Guild-only command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_guild_only: (message, data) => {
const msg = this.data.responses.command_guild_only[Math.floor(Math.random() * this.data.responses.command_guild_only.length)]
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Dms-only command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_dms_only: (message, data) => {
const msg = this.data.responses.command_dms_only[Math.floor(Math.random() * this.data.responses.command_dms_only.length)]
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Owners-only command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_owners_only: (message, data) => {
const msg = this.data.responses.command_owners_only[Math.floor(Math.random() * this.data.responses.command_owners_only.length)]
.replace(/{{s}}/g, data.s)
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Blocked command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_blocked: (message, data) => {
const msg = this.data.responses.command_blocked[Math.floor(Math.random() * this.data.responses.command_blocked.length)]
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setTitle("Blocked")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* No permission command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_no_permission: (message, data) => {
const msg = this.data.responses.command_no_permission[Math.floor(Math.random() * this.data.responses.command_no_permission.length)]
.replace(/{{permissions}}/g, data.permissions)
.replace(/{{s}}/g, data.s)
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* No bot permission command responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_no_bot_permission: (message, data) => {
const msg = this.data.responses.command_no_bot_permission[Math.floor(Math.random() * this.data.responses.command_no_bot_permission.length)]
.replace(/{{permissions}}/g, data.permissions)
.replace(/{{s}}/g, data.s)
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
},
/**
* Incorrect command usage responce
* @param {Message} message Class with data from the message
* @param {Object} data Object with data for the responce
* @returns {Promise<Message>} The sent message
*/
command_incorrect_usage: (message, data) => {
const msg = this.data.responses.command_incorrect_usage[Math.floor(Math.random() * this.data.responses.command_incorrect_usage.length)]
.replace(/{{usage}}/g, data.usage)
.replace(/{{author}}/g, message.author.username)
.replace(/{{command}}/g, data.command_name);
const embed = new this.discord.MessageEmbed()
.setColor("RED")
.setDescription(msg);
return message.channel.send({ embeds: [embed] });
}
};
this.client.once("ready", () => this.__ready());
this.client.on("messageCreate", message => this.__message(message));
this.client.on("interactionCreate", interaction => this.__interaction(interaction));
return this;
}
/**
* Logs a message to the console. Useful only for this class. Use console.log instead
* @param {...String} message Message to log
* @returns {DiscordBot} Discord bot
*/
__log(...message) {
if (this.data.dev && (typeof this.data.logging === "undefined" ? true : this.data.logging)) console.log("[DBF] => " + message.join(" "));
return this;
}
/**
* Logs the bot into Discord
* @returns {DiscordBot} Discord bot
*/
login() {
this.client.login(this.token);
return this;
}
/**
* Logs the bot out of Discord
* @returns {DiscordBot} Discord bot
*/
logout() {
this.client.destroy();
return this;
}
/**
* Gets the first user mentioned in a message
* @param {String} message Message to parse the mentions
* @returns {User} Mentioned user
*/
getUserFromMessage(message) {
const matches = message.match(this.discord.MessageMentions.USERS_PATTERN);
if (!matches) return;
const id = matches[0].replace(/[^0-9]/g, "");
return this.client.users.cache.get(id);
}
/**
* Callback for when the `ready` event is triggered on the bot
*/
onready() {}
/**
* This is the internal `ready` callback for the bot. Do not re-assign. Re-assign `onready()` instead
*/
__ready() {
this.onready();
this.__status();
this.statusLoop = setInterval(() => this.__status(), this.data.statusInterval || 15000);
this.__loadCommands((this.data.path || ".") + "/" + (this.data.commandsPath || "commands"));
this.__registerEvents((this.data.path || ".") + "/" + (this.data.eventsPath || "events"));
this.__registerButtons((this.data.path || ".") + "/" + (this.data.buttonsPath || "buttons"));
this.__registerSelectMenus((this.data.path || ".") + "/" + (this.data.selectMenusPath || "selects"));
this.__deployCommands((this.data.path || ".") + "/" + (this.data.slashPath || "slash"), this.data.restApi || null, this.data.version || "9", this.data.dev || false, this.data.devId || "");
console.log("\n[DBF] => Bot (" + this.client.user.username + ") has successfully started\n");
}
/**
* Callback for when the `messageCreate` event is triggered on the bot
* @param {Message} message Message class
*/
onmessage(message) {}
/**
* This is the internal `messageCreate` callback for the bot. Do not re-assign. Re-assign `onmessage()` instead
* @param {Message} message Class with data from the message
*/
__message(message) {
this.onmessage(message);
const client = message.client;
var command_name = "";
try {
if (message.author.bot) return;
if (message.webhookID) return;
var prefix = false;
for (const thisPrefix of this.data.prefixes || ["!"]) if (message.content.startsWith(thisPrefix)) prefix = thisPrefix;
if (!prefix) return;
const raw_args = message.content.slice(prefix.length).trim().split(/ +/);
command_name = raw_args.shift().toLowerCase();
const args = raw_args.slice(0);
if (command_name === "" || command_name === null) return;
if (!client.commands.has(command_name) && !client.aliases.has(command_name)) {
return this.responses.command_unknown(message, {
command_name,
prefix
});
}
var owner = false;
for (const thisOwner of this.data.owners || []) if (message.author.id === thisOwner) owner = thisOwner;
const command = client.commands.get(command_name) || client.aliases.get(command_name);
const num_args = command.args || 0;
const { cooldowns } = client;
if (!this.data.blockedUsers) this.data.blockedUsers = [];
if (!command.blockedUsers) command.blockedUsers = [];
if (!command.unblockedUsers) command.unblockedUsers = [];
// TODO this doesn't work... SO MAKE IT WORK
if (this.data.blockedUsers.includes(message.author.id) || command.blockedUsers.includes(message.author.id)) {
if (!command.unblockedUsers.includes(message.author.id)) {
return this.responses.command_blocked(message, { command_name });
}
}
if (!cooldowns.has(command.name)) cooldowns.set(command.name, new this.discord.Collection());
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldown_amount = (command.cooldown || 1) * 1000;
if (timestamps.has(message.author.id)) {
const expiration_time = timestamps.get(message.author.id) + cooldown_amount;
if (now < expiration_time) {
const time_left = (expiration_time - now) / 1000;
return this.responses.command_cooldown(message, {
s: s(parseFloat(time_left.toFixed(1))),
time_left: time_left.toFixed(1).replace("0.", ".").replace(".0", ""),
command_name
})
}
}
if (command.guildOnly && message.channel.type !== "text") return this.responses.command_guild_only(message, { command_name });
if (command.dmsOnly && message.channel.type !== "dm") return this.responses.command_dms_only(message, { command_name });
if (command.ownersOnly && !owner) {
return this.responses.command_owners_only(message, {
s: s(this.data.owners.length),
command_name
});
}
if (command.permissions) {
const author_perms = message.channel.permissionsFor(message.author);
if (!author_perms || !author_perms.has(command.permissions) || !author_perms.has("ADMINISTRATOR")) {
const array = Array.isArray(command.permissions);
return this.responses.command_no_permission(message, {
permissions: (array ? command.permissions.join(", ") : command.permissions).toUpperCase(),
s: array ? s(command.permissions.length) : "",
command_name
});
}
}
if (command.botPermissions) {
const bot_perms = message.channel.permissionsFor(message.client.user);
if (!bot_perms || !bot_perms.has(command.botPermissions) || !bot_perms.has("ADMINISTRATOR")) {
const array = Array.isArray(command.botPermissions);
return this.responses.command_no_bot_permission(message, {
permissions: (array ? command.botPermissions.join(", ") : command.botPermissions).toUpperCase(),
s: array ? s(command.botPermissions.length) : "",
command_name
});
}
}
if (num_args > args.length && num_args !== 0) {
const result = command.usage ? " " + command.usage : num_args === 0 ? "" : " <" + num_args + " required argument" + s(num_args) + ">";
return this.responses.command_incorrect_usage(message, {
usage: prefix + command_name + result,
command_name
});
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldown_amount);
command.execute(message, args);
} catch (error) {
console.error(error);
return this.responses.command_error(message, {
error,
command_name
});
}
}
/**
* Callback for when the `interactionCreate` event is triggered on the bot
* @param {Integration} interaction Interaction class
*/
oninteraction(interaction) {}
/**
* This is the internal `interactionCreate` callback for the bot. Do not re-assign. Re-assign `oninteraction()` instead
* @param {Integration} interaction Class with data from the interaction
*/
async __interaction(interaction) {
this.oninteraction(interaction);
const responses = this.data.responses;
if (interaction.isCommand()) {
const command = interaction.client.slash.get(interaction.commandName);
if (!command) return;
try { await command.execute(interaction); }
catch (error) {
console.error(error);
msg = responses.command_error[Math.floor(Math.random() * responses.command_error.length)]
.replace(/{{error}}/g, error);
embed = new this.discord.MessageEmbed()
.setColor("RED")
.setTitle("Error")
.setDescription(msg);
await interaction.reply({ embeds: [embed], ephemeral: true });
}
} else if (interaction.isButton()) {
const button = interaction.client.buttons.get(interaction.customId);
if (!button) return;
try { await button.execute(interaction); }
catch (error) { console.error(error); }
} else if (interaction.isSelectMenu()) {
const select = interaction.client.selects.get(interaction.customId);
if (!select) return;
try { await select.execute(interaction); }
catch (error) { console.error(error); }
}
}
/**
* Internal function to set status(s) of the bot, selected randomly from `data.responses`
* @returns {DiscordBot} Discord bot
*/
__status() {
const status = this.data.statuses[Math.floor(Math.random() * this.data.statuses.length)];
this.client.user.setPresence({
activities: [
{
name: status.name
.replace(/{{name}}/g, this.client.user.username)
.replace(/{{prefix}}/g, !this.data.prefixes ? "!" : this.data.prefixes[0])
.replace(/{{servers}}/g, this.client.guilds.cache.size)
.replace(/{{channels}}/g, this.client.channels.cache.size)
.replace(/{{users}}/g, this.client.users.cache.size) || (!this.data.prefixes ? "!" : this.data.prefixes[0]) + "help",
type: status.type || "PLAYING",
url: status.url || null
}
],
status: status.status || "online"
});
return this;
}
/**
* Internal function for loading commands from their files
* @param {String} path Path to commands folder. Defaults to `./commands`
* @returns {DiscordBot} Discord bot
*/
__loadCommands(path = "./commands") {
var files = [];
try { files = fs.readdirSync(path).filter(file => file.endsWith(".js")); }
catch { return this; }
this.client.commands = new this.discord.Collection();
this.client.aliases = new this.discord.Collection();
this.client.cooldowns = new this.discord.Collection();
if (files.length < 1) return this;
this.__log("Registering commands");
for (const file of files) {
const command = require(path + "/" + file);
this.__log("Registering command " + command.name);
this.client.commands.set(command.name, command);
if (!!command.aliases) command.aliases.map(alias => this.client.aliases.set(alias, command));
this.__log("Successfully registered command " + command.name);
}
this.__log("All commands have been registered");
return this;
}
/**
* Internal function for loading events from their files
* @param {String} path Path to events folder. Defaults to `./events`
* @returns {DiscordBot} Discord bot
*/
__registerEvents(path = "./events") {
var files = [];
try { files = fs.readdirSync(path).filter(file => file.endsWith(".js")); }
catch { return this; }
if (files.length < 1) return this;
this.__log("Registering events");
for (const file of files) {
const event = require(path + "/" + file);
this.__log("Registering event " + event.name);
if (event.once) this.client.once(event.name, (...args) => event.execute(...args));
else this.client.on(event.name, (...args) => event.execute(...args));
this.__log("Successfully registered event " + event.name);
}
this.__log("All events have been registered");
return this;
}
/**
* Internal function for loading buttons from their files
* @param {String} path Path to buttons folder. Defaults to `./buttons`
* @returns {DiscordBot} Discord bot
*/
__registerButtons(path = "./buttons") {
var files = [];
try { files = fs.readdirSync(path).filter(file => file.endsWith(".js")); }
catch { return this; }
this.client.buttons = new this.discord.Collection();
if (files.length < 1) return this;
this.__log("Registering buttons");
for (const file of files) {
const button = require(path + "/" + file);
this.__log("Registering button " + button.id);
this.client.buttons.set(button.id, button);
this.__log("Successfully registered button " + button.id);
}
this.__log("All buttons have been registered");
return this;
}
/**
* Internal function for loading select menus from their files
* @param {String} path Path to select menus folder. Defaults to `./selects`
* @returns {DiscordBot} Discord bot
*/
__registerSelectMenus(path = "./selects") {
var files = [];
try { files = fs.readdirSync(path).filter(file => file.endsWith(".js")); }
catch { return this; }
this.client.selects = new this.discord.Collection();
if (files.length < 1) return this;
this.__log("Registering select menus");
for (const file of files) {
const select = require(path + "/" + file);
this.__log("Registering select menu " + select.id);
this.client.selects.set(select.id, select);
this.__log("Successfully registered select menu " + select.id);
}
this.__log("All select menus have been registered");
return this;
}
/**
* Internal function for loading slash commands from their files
* @param {String} path Path to slash commands folder. Defaults to `./slash`
* @param {Function} rest Rest API
* @param {String} version Rest API version to use. Defaults to `9` (current)
* @param {Boolean} dev Discord bot development. Defaults to `true`
* @param {String} devId Discord bot guild id
* @returns {DiscordBot} Discord bot
*/
__deployCommands(path = "./slash", rest = null, version = "9", dev = true, devId = "") {
try {
if (!(this.data.slash || false)) return;
const id = this.client.user.id;
var files = [];
try { files = fs.readdirSync(path).filter(file => file.endsWith(".js")); }
catch { return this; }
const commands = [];
this.client.slash = new this.discord.Collection();
for (const file of files) {
const command = require(path + "/" + file);
commands.push(command.data.toJSON());
this.client.slash.set(command.data.name, command);
}
const _rest = new rest({ version: version }).setToken(this.token || "");
(async () => {
try {
if (!dev) {
await _rest.put("/applications/" + id + "/commands", { body: commands });
this.__log("Successfully registered application commands globally");
} else {
await _rest.put("/applications/" + id + "/guilds/" + devId + "/commands", { body: commands });
this.__log("Successfully registered application commands for development guild");
}
} catch (error) { console.error(error); }
})();
} catch (error) { console.error(error); }
return this;
}
}
function s(int) { return int == 1 ? "" : "s"; }
module.exports = DiscordBot;