fix(commands): re-sync global commands when definitions change

This commit is contained in:
안명현 2026-04-09 12:25:04 +09:00
parent c80bcffb08
commit 1432e0d090
1 changed files with 10 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import { Events } from 'discord.js'; import { Events } from 'discord.js';
import { createHash } from 'crypto';
import { KordClient } from '../client/KordClient'; import { KordClient } from '../client/KordClient';
import { logger } from '../utils/logger'; import { logger } from '../utils/logger';
import { InviteService } from '../services/InviteService'; import { InviteService } from '../services/InviteService';
@ -20,16 +21,19 @@ export default {
EventService.startReminderLoop(client); EventService.startReminderLoop(client);
try { try {
const lockKey = 'commands:sync:lock'; const commandsData = Array.from(client.commands.values()).map(c => c.data.toJSON());
// EX 300 = 5 minutes lock. Only one instance needs to do this per boot cycle. const commandsHash = createHash('sha256')
.update(JSON.stringify(commandsData))
.digest('hex');
const lockKey = `commands:sync:lock:${commandsHash}`;
// Lock per command definition set so updated commands can still sync on the next boot.
const acquired = await redis.set(lockKey, '1', 'EX', 300, 'NX'); const acquired = await redis.set(lockKey, '1', 'EX', 300, 'NX');
if (acquired) { if (acquired) {
const commandsData = Array.from(client.commands.values()).map(c => c.data.toJSON());
await client.application?.commands.set(commandsData); await client.application?.commands.set(commandsData);
logger.info(`Successfully registered ${commandsData.length} global application commands.`); logger.info(`Successfully registered ${commandsData.length} global application commands. hash=${commandsHash}`);
} else { } else {
logger.info('Global commands registration skipped (already handled by another instance).'); logger.info(`Global commands registration skipped for hash=${commandsHash} (already handled by another instance).`);
} }
} catch (e) { } catch (e) {
logger.error('Failed to register global commands', e); logger.error('Failed to register global commands', e);