-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommands.js
More file actions
80 lines (67 loc) · 2.82 KB
/
commands.js
File metadata and controls
80 lines (67 loc) · 2.82 KB
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
import { REST } from '@discordjs/rest';
import { Routes as DiscordRestRoutes } from 'discord-api-types/v9';
import {glob} from 'glob';
import client from './client.js';
import path from 'path';
const rest = new REST({ version: '9' }).setToken(process.env.DISCORD_BOT_TOKEN);
const COMMANDS = {};
client.once('ready', async c => {
let commandsList = [];
let commandFileList = glob.sync('./commands/*.js');
let subcommandFileList = glob.sync('./commands/*/*.js');
for (let commandPath of commandFileList) {
let commandName = path.basename(commandPath, '.js');
let command;
try { command = await import('./'+commandPath); }
catch (err) {
console.error('Failed to load command:', commandName);
console.error(err);
continue;
}
if (!command.config) {console.warn('⚠ Command "'+commandPath+'" has no config export'); continue;}
commandsList.push(command.config);
COMMANDS[command.config.name] = command.execute || null;
console.log('Loaded command:', '/'+command.config.name);
}
// Load subcommands as COMMANDS[command.subcommand]
for (let subPath of subcommandFileList) {
let rel = path.relative('./commands', subPath).replace(/\\/g, '/');
let [parent, sub] = rel.split('/');
sub = sub.replace('.js','');
let subcommand;
try { subcommand = (await import('./commands/'+parent+'/'+sub+'.js')).default; }
catch (err) {
console.error('Failed to load subcommand:', parent+'.'+sub);
console.error(err);
continue;
}
COMMANDS[parent+'.'+sub] = subcommand;
console.log('Loaded subcommand:', parent+'.'+sub);
}
if (commandsList.length == 0) return console.warn('No commands found');
client.guilds.cache.forEach(guild => {
console.log('Joined server:', guild.name);
rest.put(DiscordRestRoutes.applicationGuildCommands(client.user.id,guild.id), {body: commandsList})
.then(e => console.log('Added commands to "'+guild.name+'" guild'))
.catch(err=> console.error('Failed to add commands to "'+guild.name+'" guild'+ err));
});
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
console.log('/'+ interaction.commandName, 'triggered');
let command = interaction.commandName;
let subcommand = interaction.options.getSubcommand(false);
let commandName = command;
if (subcommand) commandName += '.'+subcommand;
if (!COMMANDS[commandName]) return console.warn('command "'+commandName+'" not found');
try {
console.log('running command "'+commandName+'"');
await COMMANDS[commandName](interaction);
}
catch (err) {
console.error('/'+commandName, 'encountered an error:', err);
if (interaction.deferred || interaction.replied) await interaction.editReply({content: 'Failed to run command: \n\n```'+err+'```', ephemeral: true});
else await interaction.reply({content: 'Failed to run command: \n\n```'+err+'```', ephemeral: true});
}
});
export default COMMANDS;