Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (63 loc) · 2.25 KB
/
Copy pathindex.js
File metadata and controls
73 lines (63 loc) · 2.25 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<
7CD3
/div>
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// load environment variables first
require('dotenv').config();
// imports
const fs = require('fs');
const path = require('path');
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
// client creation (slash commands only need the Guilds intent)
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
// load every command file from the commands folder
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
for (const file of fs.readdirSync(commandsPath).filter(f => f.endsWith('.js'))) {
const command = require(path.join(commandsPath, file));
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.warn(`Skipping ${file}: missing "data" or "execute".`);
}
}
// event listener for when the bot is ready
client.once(Events.ClientReady, c => {
console.log(`Logged in as ${c.user.tag}`);
});
// event listener for interactions (modals and slash commands)
client.on(Events.InteractionCreate, async interaction => {
// 1. modal submissions
if (interaction.isModalSubmit() && interaction.customId.startsWith('setup:')) {
try {
await client.commands.get('setup').handleModal(interaction);
} catch (error) {
console.error(error);
if (!interaction.replied) {
await interaction.reply({
content: 'Something went wrong saving your setup.',
flags: MessageFlags.Ephemeral,
});
}
}
return;
}
// 2. slash commands
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
const reply = {
content: 'Something went wrong running that command.',
flags: MessageFlags.Ephemeral,
};
if (interaction.replied || interaction.deferred) {
await interaction.followUp(reply);
} else {
await interaction.reply(reply);
}
}
});
// log in
client.login(process.env.DISCORD_TOKEN);