-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
66 lines (56 loc) · 1.87 KB
/
app.mjs
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
import { Client, Intents } from 'discord.js';
import * as dotenv from 'dotenv'; dotenv.config();
import { cmdIndex } from './commands/index.mjs';
if (!process.version.startsWith("v16")) {
console.error("ShamBot requires Node v16 to run!");
process.exit(1);
}
// -- Client --
const client = new Client({ intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
client.on('ready', () => {
console.log(`Ready to go! Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', (inter) => {
// Find the command by it's name
const cmd = cmdIndex.find((e) => {return e.name === inter.commandName});
// If a command was found
if (cmd) {
// Check if a subcommand was given
const subCmd = inter.options.getSubcommand(false);
if (subCmd) {
// Find the subcommand by it's name
const subCmdCls = cmd.options.find((e) => {return e.name === subCmd});
if (subCmdCls) subCmdCls.onRun(inter);
} else {
cmd.onRun(inter);
}
}
});
// -- Slash Commands --
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9';
const rest = new REST({ version: '9' }).setToken(process.env.TOKEN);
export function refreshSlash() {
return new Promise((res, rej) => {
// Create an array for commands
let commands = [];
cmdIndex.forEach((e) => {
commands.push(e.convert());
});
// Send the request
try {
rest.put(
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.TEST_SERVER),
{ body: commands },
).then(() => {res();});
} catch (err) {
rej(err);
}
});
}
// -- Initialization --
refreshSlash().then(() => {
client.login(process.env.TOKEN);
}).catch((err) => {
console.error(err);
});