-
Notifications
You must be signed in to change notification settings - Fork 5
/
commands.ts
61 lines (48 loc) · 1.61 KB
/
commands.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
import { Bot } from "./deps.ts"
import { log } from "./log.ts"
import { ActionRowCommand, SlashCommand } from "./types/command.ts"
export const registerCommands = async (
bot: Bot,
commands: SlashCommand[],
actionRow: ActionRowCommand[],
guildId?: string,
global = false
) => {
log.info(
"Commands:",
commands.map((c) => c.command.name)
)
bot.events.interactionCreate = (b, interaction) => {
const customId = interaction.data?.customId
if (customId) {
const command = actionRow.find((c) => c.command.customId === customId)
if (!command) return
const action = command.action
if (!action) return
action(b, interaction)
} else {
const name = interaction.data?.name
if (!name) return
const command = commands.find((c) => c.command.name === name)
if (!command) return
const action = command.action
if (!action) return
action(b, interaction)
}
}
log.info("Registered handling interactions")
if (global) {
log.info("Registering commands globally")
await bot.helpers.upsertGlobalApplicationCommands(commands.map((c) => c.command))
log.info("Commands registered in global")
} else {
if (guildId) {
log.info("Registering commands locally")
await bot.helpers.upsertGuildApplicationCommands(
guildId,
commands.map((c) => c.command)
)
log.info("Registered commands in guild")
}
}
}