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 { 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);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue