MAJ
MAJ
DIAdiscord.js - Imagine an app
Created by MAJ on 11/24/2023 in #djs-questions
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 });
}
},
};
6 replies
DIAdiscord.js - Imagine an app
Created by MAJ on 11/17/2023 in #djs-questions
Help with a code from ChatGPT.
Hey, im mostly new to coding. I have a basic understanding of how the code should look. I can do basic reads. But this is my first time coding a bot. I used chat gpt along with the discord.js api docs Can anyone take a look at my code and help me correct the Gateway intents. I tried 2 different formats and neither of them work Yall can skim through the code and tell me other mistakes. but my main this is that when i run the code it tells me something with wrong with my gateway intent line of code
PS C:\Users\MAJ\Desktop\Bot 5> node index.js
C:\Users\MAJ\Desktop\Bot 5\index.js:47
const commands = client.commands.map((command) => command.data.toJSON());
^

TypeError: command.data.toJSON is not a function
at C:\Users\MAJ\Desktop\Bot 5\index.js:47:68
at C:\Users\MAJ\Desktop\Bot 5\node_modules\discord.js\node_modules\@discordjs\collection\dist\index.js:242:14
at Function.from (<anonymous>)
at _Collection.map (C:\Users\MAJ\Desktop\Bot 5\node_modules\discord.js\node_modules\@discordjs\collection\dist\index.js:240:18)
at Object.<anonymous> (C:\Users\MAJ\Desktop\Bot 5\index.js:47:38)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)

Node.js v21.1.0
PS C:\Users\MAJ\Desktop\Bot 5> node index.js
C:\Users\MAJ\Desktop\Bot 5\index.js:47
const commands = client.commands.map((command) => command.data.toJSON());
^

TypeError: command.data.toJSON is not a function
at C:\Users\MAJ\Desktop\Bot 5\index.js:47:68
at C:\Users\MAJ\Desktop\Bot 5\node_modules\discord.js\node_modules\@discordjs\collection\dist\index.js:242:14
at Function.from (<anonymous>)
at _Collection.map (C:\Users\MAJ\Desktop\Bot 5\node_modules\discord.js\node_modules\@discordjs\collection\dist\index.js:240:18)
at Object.<anonymous> (C:\Users\MAJ\Desktop\Bot 5\index.js:47:38)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)

Node.js v21.1.0
https://github.com/MAJ-TT/Bot5
8 replies