How to get ID of command?

How can I easily get the ID of a registered command for the purpose of mentioning it? I'll be mentoning the command from another command.
Solution:
The id field isn't stored in Sapphire because it is stored in DiscordJS already. Sapphire will have logged the IDs on initial register if you didn't use bulk overwrite (in which case you should have also added it to idHints) but to get it dynamically this is the code: For Global Commands ```js import { container } from '@sapphire/framework';...
Jump to solution
2 Replies
Ruin 🐲
Ruin 🐲5mo ago
i got as far as interaction.client.stores.get("commands").get(name), but i couldn't find any ID field
Solution
Favna
Favna5mo ago
The id field isn't stored in Sapphire because it is stored in DiscordJS already. Sapphire will have logged the IDs on initial register if you didn't use bulk overwrite (in which case you should have also added it to idHints) but to get it dynamically this is the code: For Global Commands
import { container } from '@sapphire/framework';

/**
* Retrieve the command id.
* @param name - The name of the command to find
* @returns The command id if a command matching name was found in the cache, or undefined if it was not
*/
function getGlobalCommandId(name: string): string | undefined {
const application = container.client.application;

if (!application) return null;

return application.commands.cache.find(command => command.name === name)?.id;
}
import { container } from '@sapphire/framework';

/**
* Retrieve the command id.
* @param name - The name of the command to find
* @returns The command id if a command matching name was found in the cache, or undefined if it was not
*/
function getGlobalCommandId(name: string): string | undefined {
const application = container.client.application;

if (!application) return null;

return application.commands.cache.find(command => command.name === name)?.id;
}
For Guild Commands
import type { Guild } from 'discord.js';

/**
* Retrieve the command id for a specified guild.
* @param guild - The guild instance to get a command for
* @param name - The name of the command to find
* @returns The command id if a command matching name was found in the cache, or undefined if it was not
*/
function getGuildCommandId(guild: Guild, name: string): string | undefined {
return guild.commands.cache.find(command => command.name === name)?.id;
}
import type { Guild } from 'discord.js';

/**
* Retrieve the command id for a specified guild.
* @param guild - The guild instance to get a command for
* @param name - The name of the command to find
* @returns The command id if a command matching name was found in the cache, or undefined if it was not
*/
function getGuildCommandId(guild: Guild, name: string): string | undefined {
return guild.commands.cache.find(command => command.name === name)?.id;
}