Archnemix commited on
Commit
3fc73f4
·
verified ·
1 Parent(s): 3823d57

Upload 22 files

Browse files
src/commands/setup.js ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ SlashCommandBuilder,
3
+ ChannelType,
4
+ PermissionFlagsBits,
5
+ EmbedBuilder,
6
+ } from 'discord.js'
7
+ import { loadGuildConfig, setGuildConfig } from '../storage/cache.js'
8
+
9
+ export const data = new SlashCommandBuilder()
10
+ .setName('setup')
11
+ .setDescription('Configure the bot for this server')
12
+ .addSubcommand((sub) =>
13
+ sub
14
+ .setName('channels')
15
+ .setDescription('Set the welcome and leave channels')
16
+ .addChannelOption((opt) =>
17
+ opt
18
+ .setName('welcome')
19
+ .setDescription('Channel to post welcome messages in')
20
+ .addChannelTypes(ChannelType.GuildText)
21
+ .setRequired(true)
22
+ )
23
+ .addChannelOption((opt) =>
24
+ opt
25
+ .setName('leave')
26
+ .setDescription('Channel to post leave messages in')
27
+ .addChannelTypes(ChannelType.GuildText)
28
+ .setRequired(true)
29
+ )
30
+ )
31
+ .addSubcommand((sub) =>
32
+ sub
33
+ .setName('autorole-add')
34
+ .setDescription('Add a role to be assigned when a member joins')
35
+ .addRoleOption((opt) =>
36
+ opt
37
+ .setName('role')
38
+ .setDescription('Role to auto-assign')
39
+ .setRequired(true)
40
+ )
41
+ )
42
+ .addSubcommand((sub) =>
43
+ sub
44
+ .setName('autorole-remove')
45
+ .setDescription('Remove a role from auto-assign list')
46
+ .addRoleOption((opt) =>
47
+ opt
48
+ .setName('role')
49
+ .setDescription('Role to remove')
50
+ .setRequired(true)
51
+ )
52
+ )
53
+ .addSubcommand((sub) =>
54
+ sub
55
+ .setName('autorole-list')
56
+ .setDescription('Show all currently configured auto-assign roles')
57
+ )
58
+ .addSubcommand((sub) =>
59
+ sub
60
+ .setName('dm')
61
+ .setDescription('Toggle welcome DM on or off')
62
+ .addBooleanOption((opt) =>
63
+ opt
64
+ .setName('enabled')
65
+ .setDescription('Send a DM to new members when they join')
66
+ .setRequired(true)
67
+ )
68
+ )
69
+
70
+ export async function execute(interaction) {
71
+ // All subcommands require Manage Server
72
+ if (!interaction.memberPermissions.has(PermissionFlagsBits.ManageGuild)) {
73
+ await interaction.reply({
74
+ content: 'You need Manage Server permission to use this command.',
75
+ ephemeral: true,
76
+ })
77
+ return
78
+ }
79
+
80
+ const sub = interaction.options.getSubcommand()
81
+ const guildId = interaction.guildId
82
+
83
+ if (sub === 'channels') {
84
+ const welcome = interaction.options.getChannel('welcome')
85
+ const leave = interaction.options.getChannel('leave')
86
+
87
+ const config = await loadGuildConfig(guildId)
88
+ config.welcomeChannelId = welcome.id
89
+ config.leaveChannelId = leave.id
90
+ await setGuildConfig(guildId, config)
91
+
92
+ const embed = new EmbedBuilder()
93
+ .setColor(0x5865f2)
94
+ .setTitle('Channels Updated')
95
+ .setDescription(
96
+ `✅ Welcome channel set to ${welcome}\n✅ Leave channel set to ${leave}`
97
+ )
98
+
99
+ await interaction.reply({ embeds: [embed], ephemeral: true })
100
+ return
101
+ }
102
+
103
+ if (sub === 'autorole-add') {
104
+ const role = interaction.options.getRole('role')
105
+ const config = await loadGuildConfig(guildId)
106
+
107
+ if (config.autoRoles.includes(role.id)) {
108
+ await interaction.reply({
109
+ content: 'That role is already in the auto-assign list.',
110
+ ephemeral: true,
111
+ })
112
+ return
113
+ }
114
+
115
+ config.autoRoles.push(role.id)
116
+ await setGuildConfig(guildId, config)
117
+
118
+ const embed = new EmbedBuilder()
119
+ .setColor(0x57f287)
120
+ .setTitle('Auto-Role Added')
121
+ .setDescription(`New members will now receive <@&${role.id}>`)
122
+
123
+ await interaction.reply({ embeds: [embed], ephemeral: true })
124
+ return
125
+ }
126
+
127
+ if (sub === 'autorole-remove') {
128
+ const role = interaction.options.getRole('role')
129
+ const config = await loadGuildConfig(guildId)
130
+
131
+ if (!config.autoRoles.includes(role.id)) {
132
+ await interaction.reply({
133
+ content: 'That role is not in the list.',
134
+ ephemeral: true,
135
+ })
136
+ return
137
+ }
138
+
139
+ config.autoRoles = config.autoRoles.filter((id) => id !== role.id)
140
+ await setGuildConfig(guildId, config)
141
+
142
+ const embed = new EmbedBuilder()
143
+ .setColor(0xed4245)
144
+ .setTitle('Auto-Role Removed')
145
+ .setDescription(`<@&${role.id}> will no longer be assigned to new members`)
146
+
147
+ await interaction.reply({ embeds: [embed], ephemeral: true })
148
+ return
149
+ }
150
+
151
+ if (sub === 'autorole-list') {
152
+ const config = await loadGuildConfig(guildId)
153
+
154
+ if (!config.autoRoles.length) {
155
+ await interaction.reply({
156
+ content: 'No auto-roles configured.',
157
+ ephemeral: true,
158
+ })
159
+ return
160
+ }
161
+
162
+ const roleList = config.autoRoles.map((id) => `<@&${id}>`).join('\n')
163
+ const embed = new EmbedBuilder()
164
+ .setColor(0x5865f2)
165
+ .setTitle('Auto-Assign Roles')
166
+ .setDescription(roleList)
167
+
168
+ await interaction.reply({ embeds: [embed], ephemeral: true })
169
+ return
170
+ }
171
+
172
+ if (sub === 'dm') {
173
+ const enabled = interaction.options.getBoolean('enabled')
174
+ const config = await loadGuildConfig(guildId)
175
+
176
+ config.dmEnabled = enabled
177
+ await setGuildConfig(guildId, config)
178
+
179
+ const embed = new EmbedBuilder()
180
+ .setColor(enabled ? 0x57f287 : 0xed4245)
181
+ .setTitle('Welcome DM Updated')
182
+ .setDescription(
183
+ `Welcome DMs are now **${enabled ? 'enabled' : 'disabled'}**`
184
+ )
185
+
186
+ await interaction.reply({ embeds: [embed], ephemeral: true })
187
+ return
188
+ }
189
+ }
src/deploy-commands.js ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { REST } from '@discordjs/rest'
2
+ import { Routes } from 'discord-api-types/v10'
3
+ import { readdirSync } from 'node:fs'
4
+ import { resolve, dirname } from 'node:path'
5
+ import { fileURLToPath, pathToFileURL } from 'node:url'
6
+
7
+ const __dirname = dirname(fileURLToPath(import.meta.url))
8
+
9
+ export async function deployCommands() {
10
+ const token = process.env.DISCORD_TOKEN
11
+ if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')
12
+
13
+ const clientId = process.env.DISCORD_CLIENT_ID
14
+ if (!clientId) throw new Error('Missing required environment variable: DISCORD_CLIENT_ID')
15
+
16
+ // If DEPLOY_GUILD_ID is not set, this test guild ID is used as a fallback.
17
+ // Remove this hardcoded value (and the ?? below) before switching to global deploy.
18
+ const TEST_GUILD_FALLBACK = '1508872676242882741'
19
+ const guildId = process.env.DEPLOY_GUILD_ID ?? TEST_GUILD_FALLBACK
20
+
21
+ const commandsDir = resolve(__dirname, 'commands')
22
+ const files = readdirSync(commandsDir).filter((f) => f.endsWith('.js'))
23
+
24
+ const commandData = []
25
+
26
+ for (const file of files) {
27
+ const mod = await import(pathToFileURL(resolve(commandsDir, file)).href)
28
+ if (!mod?.data) {
29
+ console.warn(`[deploy] Skipping ${file} — no 'data' export found`)
30
+ continue
31
+ }
32
+ console.log(`[deploy] Found command: ${mod.data.name}`)
33
+ commandData.push(mod.data.toJSON())
34
+ }
35
+
36
+ const rest = new REST({ version: '10' }).setToken(token)
37
+
38
+ try {
39
+ if (guildId) {
40
+ await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commandData })
41
+ console.log(`[deploy] Successfully registered ${commandData.length} commands to guild ${guildId}`)
42
+ } else {
43
+ await rest.put(Routes.applicationCommands(clientId), { body: commandData })
44
+ console.log(`[deploy] Successfully registered ${commandData.length} commands globally`)
45
+ }
46
+ } catch (err) {
47
+ console.error('[deploy] Failed to register commands:', err)
48
+ process.exit(1)
49
+ }
50
+ }
51
+
52
+ if (process.argv[1].endsWith('deploy-commands.js')) {
53
+ deployCommands().catch((err) => {
54
+ console.error(err)
55
+ process.exit(1)
56
+ })
57
+ }
src/events/interactionCreate.js ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { fileURLToPath } from 'node:url'
2
+ import { dirname, resolve } from 'node:path'
3
+ import { readdirSync } from 'node:fs'
4
+ import { pathToFileURL } from 'node:url'
5
+
6
+ const __dirname = dirname(fileURLToPath(import.meta.url))
7
+
8
+ // commandMap is populated once at module load time — not on every interaction
9
+ const commandMap = new Map()
10
+
11
+ ;(async () => {
12
+ const commandsDir = resolve(__dirname, '../commands')
13
+ const files = readdirSync(commandsDir).filter((f) => f.endsWith('.js'))
14
+
15
+ await Promise.all(
16
+ files.map(async (file) => {
17
+ const mod = await import(pathToFileURL(resolve(commandsDir, file)).href)
18
+ if (mod?.data?.name) {
19
+ commandMap.set(mod.data.name, mod)
20
+ }
21
+ })
22
+ )
23
+
24
+ console.log(`[commands] Loaded ${commandMap.size} command(s): ${[...commandMap.keys()].join(', ')}`)
25
+ })()
26
+
27
+ export const name = 'interactionCreate'
28
+ export const once = false
29
+
30
+ export async function execute(interaction) {
31
+ if (!interaction.isChatInputCommand()) return
32
+
33
+ const command = commandMap.get(interaction.commandName)
34
+
35
+ if (!command) {
36
+ await interaction.reply({ content: 'Unknown command.', ephemeral: true })
37
+ return
38
+ }
39
+
40
+ try {
41
+ await command.execute(interaction)
42
+ } catch (err) {
43
+ console.error(`[commands] Error in ${interaction.commandName}: ${err.message}`)
44
+
45
+ if (!interaction.replied && !interaction.deferred) {
46
+ await interaction.reply({ content: 'Something went wrong. Please try again.', ephemeral: true })
47
+ } else if (!interaction.replied) {
48
+ await interaction.editReply({ content: 'Something went wrong. Please try again.' })
49
+ }
50
+ }
51
+ }
src/index.js CHANGED
@@ -9,6 +9,18 @@ import { startBot } from './bot.js'
9
  process.exit(1)
10
  }
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  await startHealthServer().catch((err) => {
13
  console.error('Failed to start health server:', err)
14
  process.exit(1)
 
9
  process.exit(1)
10
  }
11
 
12
+ if (process.env.DEPLOY_COMMANDS === 'true') {
13
+ console.log('[deploy] DEPLOY_COMMANDS=true detected, registering commands...')
14
+ try {
15
+ const { deployCommands } = await import('./deploy-commands.js')
16
+ await deployCommands()
17
+ console.log('[deploy] Commands registered successfully')
18
+ } catch (err) {
19
+ console.error('[deploy] Command registration failed:', err.message)
20
+ process.exit(1)
21
+ }
22
+ }
23
+
24
  await startHealthServer().catch((err) => {
25
  console.error('Failed to start health server:', err)
26
  process.exit(1)