File size: 2,407 Bytes
3fc73f4
039603d
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
039603d
 
 
 
3fc73f4
 
f2c5563
853fde1
f2c5563
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { REST } from '@discordjs/rest'
import { Agent } from 'undici'
import { Routes } from 'discord-api-types/v10'
import { readdirSync } from 'node:fs'
import { resolve, dirname } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))

export async function deployCommands() {
  const token = process.env.DISCORD_TOKEN
  if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')

  const clientId = process.env.DISCORD_CLIENT_ID
  if (!clientId) throw new Error('Missing required environment variable: DISCORD_CLIENT_ID')

  // If DEPLOY_GUILD_ID is not set, this test guild ID is used as a fallback.
  // Remove this hardcoded value (and the ?? below) before switching to global deploy.
  const TEST_GUILD_FALLBACK = '1508872676242882741'
  const guildId = process.env.DEPLOY_GUILD_ID ?? TEST_GUILD_FALLBACK

  const commandsDir = resolve(__dirname, 'commands')
  const files = readdirSync(commandsDir).filter((f) => f.endsWith('.js'))

  const commandData = []

  for (const file of files) {
    const mod = await import(pathToFileURL(resolve(commandsDir, file)).href)
    if (!mod?.data) {
      console.warn(`[deploy] Skipping ${file} — no 'data' export found`)
      continue
    }
    console.log(`[deploy] Found command: ${mod.data.name}`)
    commandData.push(mod.data.toJSON())
  }

  const rest = new REST({
    version: '10',
    agent: new Agent({ connect: { timeout: 60_000 } })
  }).setToken(token)

  try {
    console.log('[deploy] Commands array:', JSON.stringify(commandData))
    console.log('[deploy] Registering to guild:', guildId, 'app:', clientId)

    if (guildId) {
      await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commandData })
      console.log(`[deploy] Successfully registered ${commandData.length} commands to guild ${guildId}`)
    } else {
      await rest.put(Routes.applicationCommands(clientId), { body: commandData })
      console.log(`[deploy] Successfully registered ${commandData.length} commands globally`)
    }
  } catch (err) {
    console.error('[deploy] Failed to register commands:', err)
    process.exit(1)
  }
}

if (process.argv[1].endsWith('deploy-commands.js')) {
  deployCommands().catch((err) => {
    console.error(err)
    process.exit(1)
  })
}