-
Notifications
You must be signed in to change notification settings - Fork 228
/
deploy.ts
62 lines (53 loc) · 1.77 KB
/
deploy.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
import { REST } from '@discordjs/rest'
import { Routes } from 'discord-api-types/v10'
import logger from 'logger'
import config from 'config'
import { basename, sep } from 'path'
import { promisify } from 'util'
import glob from 'glob'
import { type RESTPostAPIChatInputApplicationCommandsJSONBody } from 'discord.js'
import type { StructureImport } from 'types'
import type { Command } from '@structures'
const { token, clientId, guildId } = config
const glob_ = promisify(glob)
const _loadCommands = async (): Promise<
RESTPostAPIChatInputApplicationCommandsJSONBody[]
> => {
const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []
const files = await glob_(
`${__dirname.split(sep).join('/')}/src/commands/*/*{.ts,.js}`,
)
if (files.length === 0) {
logger.warn('No commands found')
return commands
}
for (const f of files) {
const name = basename(f, '.ts')
try {
const command = ((await import(f)) as StructureImport<Command>).default
commands.push(command.data.toJSON())
} catch (err) {
if (err instanceof Error) {
logger.error(`Command failed to import: ${name}`)
logger.error(err.stack)
} else logger.error(err)
}
}
return commands
}
const rest = new REST({ version: '10' }).setToken(token)
logger.info('Deploying commands...')
const applicationCommands =
process.env.NODE_ENV === 'production'
? Routes.applicationCommands(clientId)
: Routes.applicationGuildCommands(clientId, guildId)
;(async (): Promise<void> => {
try {
const commands = await _loadCommands()
await rest.put(applicationCommands, { body: commands })
logger.info(`Commands successfully deployed`)
} catch (err) {
if (err instanceof Error) logger.error(err.stack)
else logger.error(err)
}
})()