context menus not working

code:
const { ContextMenuCommandBuilder, ApplicationCommandType } = require("discord.js")

module.exports = {
data: new ContextMenuCommandBuilder()
.setName("Test")
.setType(ApplicationCommandType.User),
async run (interaction) {
await interaction.reply({ content: "This worked", ephemeral: true })
}
}
const { ContextMenuCommandBuilder, ApplicationCommandType } = require("discord.js")

module.exports = {
data: new ContextMenuCommandBuilder()
.setName("Test")
.setType(ApplicationCommandType.User),
async run (interaction) {
await interaction.reply({ content: "This worked", ephemeral: true })
}
}
interactionCreate event:
client.on('interactionCreate', async (interaction) => {
if (!interaction.isUserContextMenuCommand()) return;
const { commands } = client
const { commandName } = interaction
const contextCommand = commands.get(commandName)
if(!contextCommand) return;
console.log(interaction.commandId)

try {
await contextCommand.run(interaction)
} catch (e) {
console.log(e)
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isUserContextMenuCommand()) return;
const { commands } = client
const { commandName } = interaction
const contextCommand = commands.get(commandName)
if(!contextCommand) return;
console.log(interaction.commandId)

try {
await contextCommand.run(interaction)
} catch (e) {
console.log(e)
}
});
Issue: bot returns The application did not respond For some reason console.log(contextCommand) doesn't return anything
15 Replies
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
-Carlos👑
-Carlos👑•2y ago
someone?
duck
duck•2y ago
if console.log(interaction.commandId) didn't log anything, then it did not execute this means that either this listener didn't execute due to it not being registered properly, or it was unable to find a command in your commands collection with that key
d.js docs
d.js docs•2y ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. • Once you do, log relevant values and if-conditions • More sophisticated debugging methods are breakpoints and runtime inspections: learn more
-Carlos👑
-Carlos👑•2y ago
got this
client.on('interactionCreate', async (interaction) => {
if (!interaction.isUserContextMenuCommand()) return;
const { commands } = client
const { commandName } = interaction
console.log(commandName)
const contextCommand = commands.get(commandName)
console.log(contextCommand)
if(!contextCommand) return;

try {
console.log(contextCommand.run(interaction))
await contextCommand.run(interaction)
} catch (e) {
console.log(e)
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isUserContextMenuCommand()) return;
const { commands } = client
const { commandName } = interaction
console.log(commandName)
const contextCommand = commands.get(commandName)
console.log(contextCommand)
if(!contextCommand) return;

try {
console.log(contextCommand.run(interaction))
await contextCommand.run(interaction)
} catch (e) {
console.log(e)
}
});
duck
duck•2y ago
is Test the name of the command you used?
-Carlos👑
-Carlos👑•2y ago
yes
duck
duck•2y ago
then it was unable to find a command with the key Test in your commands collection
-Carlos👑
-Carlos👑•2y ago
what could be the reason
duck
duck•2y ago
not setting it properly, since that's your collection that you created
-Carlos👑
-Carlos👑•2y ago
I logged commands, idk if this helps in anything
duck
duck•2y ago
since the issue is that it wasn't set properly, the issue will be where you set the commands in the commands collection 🙂
-Carlos👑
-Carlos👑•2y ago
here?
client.commands = new Collection();
fs.readdirSync("./Commands").forEach((dir) => {
const commands = fs.readdirSync(`./Commands/${dir}`).filter((file) => file.endsWith(".js"))
for (let file of commands) {
let command = require(`./Commands/${dir}/${file}`);
client.commands.set(command.name, command)
}
});
client.commands = new Collection();
fs.readdirSync("./Commands").forEach((dir) => {
const commands = fs.readdirSync(`./Commands/${dir}`).filter((file) => file.endsWith(".js"))
for (let file of commands) {
let command = require(`./Commands/${dir}/${file}`);
client.commands.set(command.name, command)
}
});
duck
duck•2y ago
yep command in this context will be the object exported by your command files, so that means it has data and run likely you wanted to access command.data.name for the command's name, rather than command.name
-Carlos👑
-Carlos👑•2y ago
fixed it, ty