-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
67 lines (55 loc) · 2.06 KB
/
bot.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
const { Client, Events, GatewayIntentBits, Collection } = require('discord.js')
const { token } = require('./config.json')
const fs = require('fs')
const path = require('node:path')
const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] })
bot.commands = new Collection()
const cmdsDir = path.join(__dirname, 'cmds')
const cmdsFiles = fs.readdirSync(cmdsDir).filter(file => file.endsWith('.js'))
for (const file of cmdsFiles) {
const filePath = path.join(cmdsDir, file)
const command = require(filePath)
if ('data' in command && 'execute' in command) {
bot.commands.set(command.data.name, command)
} else {
console.warn(`The command ${file} is missing required fields.`);
}
}
bot.once(Events.ClientReady, c => {
console.log(`Logged in as ${c.user.tag}`);
})
bot.login(token)
bot.on(Events.InteractionCreate, async interaction => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName)
if (!command) {
interaction.reply({
content: 'What are you on about?',
ephemeral: true
})
console.error(`No command ${interaction.commandName} found`)
return
}
try {
await command.execute(interaction)
} catch (error) {
console.error(error)
await interaction.reply({
content: 'Error executing this command',
ephemeral: true
})
}
} else if (interaction.isAutocomplete()) {
const command = interaction.client.commands.get(interaction.commandName)
if (!command) return console.error(`Command ${interaction.commandName} not found`)
try {
await command.autocomplete(interaction)
} catch (error) {
console.error(error)
await interaction.reply({
content: 'Error executing this command',
ephemeral: true
})
}
}
})