-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (49 loc) · 1.93 KB
/
app.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
import fs from 'node:fs/promises'
import path from 'node:path'
import { Client, Collection, GatewayIntentBits, REST, Routes } from 'discord.js'
import { logger } from './lib/logger.js'
const token = process.env.DISCORD_TOKEN
const clientId = process.env.DISCORD_CLIENT_ID
const guildId = process.env.DISCORD_GUILD_ID
const __dirname = path.resolve(path.dirname(''))
async function run() {
const client = new Client({ intents: [GatewayIntentBits.Guilds] })
// Load commands
client.commands = new Collection()
const commandsPath = path.join(__dirname, 'commands')
const commandFiles = (await fs.readdir(commandsPath)).filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file)
const { command } = await import(filePath)
if ('definition' in command && 'execute' in command) {
client.commands.set(command.definition.name, command)
logger.info(`The command '${command.definition.name}' at ${filePath} has been successfully loaded.`)
} else {
logger.warn(`The command at ${filePath} is missing a required "definition" or "execute" property.`)
}
}
// Register / commands
const rest = new REST().setToken(token)
try {
const data = await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commands.map(command => command.definition.toJSON())
})
logger.info(`Successfully reloaded ${data.length} application (/) commands.`)
} catch (error) {
logger.error(error)
}
// Events
const eventsPath = path.join(__dirname, 'events')
const eventFiles = (await fs.readdir(eventsPath)).filter(file => file.endsWith('.js'))
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file)
const { event } = await import(filePath)
if (event.once) {
client.once(event.name, event.execute)
} else {
client.on(event.name, event.execute)
}
}
client.login(token)
}
run()