MCPcopy Create free account
hub / github.com/codebymitch/TitanBot / registerGuildCommands

Function registerGuildCommands

src/handlers/commandLoader.js:263–305  ·  view source on GitHub ↗
(client, guildId, commands, totalSubcommands)

Source from the content-addressed store, hash-verified

261}
262
263async function registerGuildCommands(client, guildId, commands, totalSubcommands) {
264 logger.info(`Preparing to register ${totalSubcommands + commands.length} commands for guild ${guildId}`);
265 logger.info('Validating commands before registration...');
266 validateCommands(commands);
267 logger.info('Command validation passed');
268
269 const guild = await client.guilds.fetch(guildId);
270 const existingCommands = await guild.commands.fetch();
271 logger.info(`Found ${existingCommands.size} existing guild commands`);
272
273 const commandsToRegister = prepareCommandsForRegistration(commands, { multiGuild: false });
274
275 if (process.env.NODE_ENV !== 'production') {
276 logger.info(`Registering ${commandsToRegister.length} commands for guild ${guild.name} (${guild.id})`);
277 }
278
279 try {
280 logger.info(`Registering ${commandsToRegister.length} guild commands...`);
281 await guild.commands.set(commandsToRegister);
282 logger.info(`Successfully registered ${commandsToRegister.length} guild commands`);
283
284 const registeredCommands = await guild.commands.fetch();
285 if (registeredCommands.size !== commandsToRegister.length) {
286 logger.warn(`Warning: Expected ${commandsToRegister.length} commands, but Discord reports ${registeredCommands.size} registered`);
287 } else {
288 logger.info(`Verification passed: ${registeredCommands.size} commands successfully registered`);
289 }
290 } catch (error) {
291 logger.error('Failed to register commands:', error);
292
293 if (existingCommands.size > 0) {
294 logger.info('Attempting to restore previous commands due to registration failure...');
295 try {
296 await guild.commands.set(existingCommands.map((cmd) => cmd));
297 logger.info('Successfully restored previous commands');
298 } catch (restoreError) {
299 logger.error('Failed to restore previous commands:', restoreError);
300 }
301 }
302
303 throw error;
304 }
305}
306
307export async function registerCommands(client, options = {}) {
308 const {

Callers 1

registerCommandsFunction · 0.85

Calls 3

validateCommandsFunction · 0.85
setMethod · 0.45

Tested by

no test coverage detected