-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (58 loc) · 2.18 KB
/
index.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
import 'dotenv/config'
import { Client, Events, GatewayIntentBits, REST, Routes, PermissionFlagsBits } from 'discord.js'
import { continueMenu, handleAutocomplete } from './interaction.js'
import { getTicket } from './db.js'
import commands from './commands/index.js'
import modals from './modals/index.js'
const token = process.env.APP_DISCORD_TOKEN
const guildId = process.env.APP_DISCORD_GUILD
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers]
})
const rest = new REST().setToken(token)
client.once(Events.ClientReady, async (evt) => {
console.log(`Ready! Logged in as ${evt.user.tag}`)
await rest.put(
Routes.applicationGuildCommands(evt.user.id, guildId),
{ body: Array.from(commands.values(), c => c.data.toJSON()) },
)
console.log('Successfully reloaded commands.')
})
client.on(Events.InteractionCreate, async (interaction) => {
try {
if (interaction.isChatInputCommand()) {
const command = commands.get(interaction.commandName)
await command.execute(interaction)
} else if (interaction.isAutocomplete()) {
await handleAutocomplete(interaction)
} else if (interaction.isButton()) {
await continueMenu(interaction, parseInt(interaction.customId))
} else if (interaction.isStringSelectMenu()) {
await continueMenu(interaction, parseInt(interaction.values[0]))
} else if (interaction.isModalSubmit()) {
const meta = JSON.parse(interaction.customId)
const modal = modals.get(meta.type)
await modal.execute(interaction, meta)
}
} catch (e) {
console.error(e)
await interaction.reply({
content: ':x: There was an error!',
ephemeral: true,
})
}
})
client.on(Events.ThreadMembersUpdate, async (_addedMembers, removedMembers, thread) => {
// archive and lock ticket threads if user leaves
const ticket = getTicket(thread.id)
if (!ticket) {
return
}
if (removedMembers.has(ticket.user_id)) {
await thread.edit({
archived: true,
locked: true,
})
}
})
client.login(token)