How do I make a Message Context Action

Currently I have:
const logSearchContextMenu = new ContextMenuCommandBuilder()
.setName('Get Time')
.setType(ApplicationCommandType.Message);
const logSearchContextMenu = new ContextMenuCommandBuilder()
.setName('Get Time')
.setType(ApplicationCommandType.Message);
I don't get any errors or warnings, but I feel like just building it doesn't actually register it with Discord. I'm not seeing the context action on any messages in the server the bot is in. I'm following this guide: https://discordjs.guide/interactions/context-menus.html#registering-context-menu-commands I have this to handle it in my "interactionCreate" handler:
if(commandName === "Get Time")
{
interaction.reply({
content: `${interaction.targetMessage.createdAt}`,
ephemeral: true
})
}
if(commandName === "Get Time")
{
interaction.reply({
content: `${interaction.targetMessage.createdAt}`,
ephemeral: true
})
}
But I can't get the context action to even show up to test it. I feel like I'm missing something basic.
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
9 Replies
d.js toolkit
d.js toolkit3mo 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 OP
treble/luna
treble/luna3mo ago
show where you register your commands
Kiwi
KiwiOP3mo ago
let commands = bot.application?.commands

commands?.create({
name: "info",
description: "Gives info"
})

const logSearchContextMenu = new ContextMenuCommandBuilder()
.setName('Get Time')
.setType(ApplicationCommandType.Message);
let commands = bot.application?.commands

commands?.create({
name: "info",
description: "Gives info"
})

const logSearchContextMenu = new ContextMenuCommandBuilder()
.setName('Get Time')
.setType(ApplicationCommandType.Message);
bot.on('interactionCreate', async (interaction) => {

logger.info('interactionCreate emitted', {"username": interaction.user.username, "userID": interaction.user.id,
"interactionType": interaction.type, "commandName": interaction.commandName, "guildName": interaction.member.guild.name, "channelID": interaction.channelId,
"message": interaction.targetMessage || undefined
})

const {commandName, options} = interaction

// Skip interactions that aren't commands or context menus since we don't do those
if(!interaction.isCommand() || !interaction.isContextMenu())
{
return;
}

if(commandName === "info")
{
interaction.reply({
content: `<REDACTED>`,
ephemeral: true
})
}

if(commandName === "Get Time")
{
interaction.reply({
content: `${interaction.targetMessage.createdAt}`,
ephemeral: true
})
}



})
bot.on('interactionCreate', async (interaction) => {

logger.info('interactionCreate emitted', {"username": interaction.user.username, "userID": interaction.user.id,
"interactionType": interaction.type, "commandName": interaction.commandName, "guildName": interaction.member.guild.name, "channelID": interaction.channelId,
"message": interaction.targetMessage || undefined
})

const {commandName, options} = interaction

// Skip interactions that aren't commands or context menus since we don't do those
if(!interaction.isCommand() || !interaction.isContextMenu())
{
return;
}

if(commandName === "info")
{
interaction.reply({
content: `<REDACTED>`,
ephemeral: true
})
}

if(commandName === "Get Time")
{
interaction.reply({
content: `${interaction.targetMessage.createdAt}`,
ephemeral: true
})
}



})
treble/luna
treble/luna3mo ago
thats how you handle them not where you register them you create a single command there
Kiwi
KiwiOP3mo ago
This is the only command I have thats successfully registered
treble/luna
treble/luna3mo ago
which you also should not do on every start yes because you actually register it just defining the builder wont register it
d.js docs
d.js docs3mo ago
:guide: Creating Your Bot: Registering slash commands read more :method: BaseInteraction#isContextMenuCommand() @14.16.2 Indicates whether this interaction is a ContextMenuCommandInteraction
treble/luna
treble/luna3mo ago
your typeguard also is wrong
Kiwi
KiwiOP3mo ago
Ohh isContextMenuCommand() right I see now you just register it like a regular slash command Thank you!! It worked
Want results from more Discord servers?
Add your server