Refactor command registration to support guild-specific commands
- Updated the command registration process to allow for instant synchronization of guild commands, while maintaining global command registration. - Introduced optional BOT_GUILD_ID environment variable for guild-specific command handling. - Enhanced logging to differentiate between guild and global command registrations, improving traceability.
This commit is contained in:
@@ -33,15 +33,36 @@ const contextMenuMap = new Map(utilityContextMenus.map((c) => [c.data.name, c]))
|
|||||||
|
|
||||||
export async function registerCommands() {
|
export async function registerCommands() {
|
||||||
const rest = new REST({ version: '10' }).setToken(env.BOT_TOKEN);
|
const rest = new REST({ version: '10' }).setToken(env.BOT_TOKEN);
|
||||||
await rest.put(Routes.applicationCommands(env.BOT_CLIENT_ID), {
|
const body = [
|
||||||
body: [
|
...commands.map((c) => c.data.toJSON()),
|
||||||
...commands.map((c) => c.data.toJSON()),
|
...utilityContextMenus.map((c) => c.data.toJSON())
|
||||||
...utilityContextMenus.map((c) => c.data.toJSON())
|
];
|
||||||
]
|
|
||||||
});
|
// Guild commands sync instantly; global can take up to ~1h.
|
||||||
|
if (env.BOT_GUILD_ID) {
|
||||||
|
await rest.put(Routes.applicationGuildCommands(env.BOT_CLIENT_ID, env.BOT_GUILD_ID), {
|
||||||
|
body
|
||||||
|
});
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
scope: 'guild',
|
||||||
|
guildId: env.BOT_GUILD_ID,
|
||||||
|
count: commands.length,
|
||||||
|
contextMenus: utilityContextMenus.length
|
||||||
|
},
|
||||||
|
'Registered guild application commands'
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await rest.put(Routes.applicationCommands(env.BOT_CLIENT_ID), { body });
|
||||||
logger.info(
|
logger.info(
|
||||||
{ count: commands.length, contextMenus: utilityContextMenus.length },
|
{
|
||||||
'Registered application commands'
|
scope: 'global',
|
||||||
|
count: commands.length,
|
||||||
|
contextMenus: utilityContextMenus.length
|
||||||
|
},
|
||||||
|
'Registered global application commands'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const EnvSchema = z.object({
|
|||||||
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
||||||
BOT_TOKEN: z.string().min(1),
|
BOT_TOKEN: z.string().min(1),
|
||||||
BOT_CLIENT_ID: z.string().min(1),
|
BOT_CLIENT_ID: z.string().min(1),
|
||||||
|
BOT_GUILD_ID: z.string().min(1).optional(),
|
||||||
DATABASE_URL: z.string().url(),
|
DATABASE_URL: z.string().url(),
|
||||||
REDIS_URL: z.string().url(),
|
REDIS_URL: z.string().url(),
|
||||||
LOG_LEVEL: z.string().default('info'),
|
LOG_LEVEL: z.string().default('info'),
|
||||||
|
|||||||
Reference in New Issue
Block a user