Mentioning slash commands not working

Hi, i want to mention the slash command so that its clickable. I think i did the code right can you help
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const fs = require('fs');
const path = require('path');

module.exports = {
data: new SlashCommandBuilder()
.setName('help')
.setDescription('Show help menu'),

async execute(interaction) {
try {
const commandsPath = path.join(__dirname, '..', 'Utility');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

const commandMentions = [];
for (const file of commandFiles) {
const command = require(path.join(commandsPath, file));
const commandData = command.data;

// Fetch command ID
const commands = await interaction.client.application.commands.fetch();
const cmd = commands.find(c => c.name === commandData.name);
if (cmd) {
commandMentions.push(`<@&${cmd.id}>: ${commandData.description}`);
}
else {
commandMentions.push(`/${commandData.name}: ${commandData.description}`);
}
}

const embed = new EmbedBuilder()
.setColor('#000001')
.setTitle('Available Commands')
.setDescription(commandMentions.join('\n'));

await interaction.reply({ embeds: [embed], ephemeral: true });
}
catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while fetching commands.', ephemeral: true });
}
},
};
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const fs = require('fs');
const path = require('path');

module.exports = {
data: new SlashCommandBuilder()
.setName('help')
.setDescription('Show help menu'),

async execute(interaction) {
try {
const commandsPath = path.join(__dirname, '..', 'Utility');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

const commandMentions = [];
for (const file of commandFiles) {
const command = require(path.join(commandsPath, file));
const commandData = command.data;

// Fetch command ID
const commands = await interaction.client.application.commands.fetch();
const cmd = commands.find(c => c.name === commandData.name);
if (cmd) {
commandMentions.push(`<@&${cmd.id}>: ${commandData.description}`);
}
else {
commandMentions.push(`/${commandData.name}: ${commandData.description}`);
}
}

const embed = new EmbedBuilder()
.setColor('#000001')
.setTitle('Available Commands')
.setDescription(commandMentions.join('\n'));

await interaction.reply({ embeds: [embed], ephemeral: true });
}
catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while fetching commands.', ephemeral: true });
}
},
};
4 Replies
d.js toolkit
d.js toolkit14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
Syjalo
Syjalo14mo ago
1. Don't fetch commands for each command. Fetch outside of the for loop 1 time. 2. <@&id> is the role mention. For slash commands it's </name:id>
MAJ
MAJOP14mo ago
that would be this line of code your refering to
commandMentions.push(`</${commandData.name}:${cmd.id}>: ${commandData.description}`);
commandMentions.push(`</${commandData.name}:${cmd.id}>: ${commandData.description}`);
and it should be changed to
commandMentions.push(`</${commandData.name}:${cmd.name}>: ${commandData.description}`);
commandMentions.push(`</${commandData.name}:${cmd.name}>: ${commandData.description}`);
Preetham Hegade
Preetham Hegade14mo ago
there is a function for this right ? chatInputApplicationCommandMention() i use this but idk if its there in new versions
Want results from more Discord servers?
Add your server