-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
75 lines (66 loc) · 2.03 KB
/
index.ts
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
// Require the necessary discord.js classes
import {
Client,
CommandInteraction,
Events,
GatewayIntentBits,
Routes,
} from 'discord.js';
import 'dotenv/config';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { Command } from './types';
const filePath = fileURLToPath(import.meta.url);
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands: Command[] = [];
const commandsPath = path.join(path.dirname(filePath), 'commands');
const commandFiles = await fs.readdir(commandsPath);
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command: Command = await import(filePath);
commands.push(command);
}
const handleSlashCommand = async (
client: Client,
interaction: CommandInteraction,
): Promise<void> => {
const slashCommand = commands.find(
c => c.command.name === interaction.commandName,
);
if (!slashCommand) {
interaction.followUp({ content: 'An error has occurred' });
return;
}
await interaction.deferReply();
try {
await slashCommand.run(client, interaction);
} catch (error) {
console.error(error);
await interaction.editReply('An error has occurred');
}
};
client.once(Events.ClientReady, async c => {
await c.rest.put(Routes.applicationCommands(c.application.id), {
body: commands.map(c => ({
...c.command.toJSON(),
// Make the command run in all contexts
contexts: [0, 1, 2],
integration_types: [0, 1],
})),
});
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, async interaction => {
if (!(interaction.isCommand() || interaction.isContextMenuCommand())) return;
console.log(
`Command ran: ${interaction.commandName} (${interaction.user.username}, ${
interaction.user.id
})
- args: {${interaction.options.data
.map(o => `${o.name}: ${JSON.stringify(o.value)}`)
.join(', ')}}`,
);
await handleSlashCommand(client, interaction);
});
client.login(process.env.TOKEN);