fix(commands): re-sync global commands when definitions change
This commit is contained in:
parent
c80bcffb08
commit
1432e0d090
|
|
@ -1,4 +1,5 @@
|
|||
import { Events } from 'discord.js';
|
||||
import { createHash } from 'crypto';
|
||||
import { KordClient } from '../client/KordClient';
|
||||
import { logger } from '../utils/logger';
|
||||
import { InviteService } from '../services/InviteService';
|
||||
|
|
@ -20,16 +21,19 @@ export default {
|
|||
EventService.startReminderLoop(client);
|
||||
|
||||
try {
|
||||
const lockKey = 'commands:sync:lock';
|
||||
// EX 300 = 5 minutes lock. Only one instance needs to do this per boot cycle.
|
||||
const commandsData = Array.from(client.commands.values()).map(c => c.data.toJSON());
|
||||
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');
|
||||
|
||||
if (acquired) {
|
||||
const commandsData = Array.from(client.commands.values()).map(c => c.data.toJSON());
|
||||
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 {
|
||||
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) {
|
||||
logger.error('Failed to register global commands', e);
|
||||
|
|
|
|||
Loading…
Reference in New Issue