Context Menus

Context Menus are application commands which appear when right clicking or tapping a user or a message, in the Apps submenu.

TIP

This page is a follow-up to the slash commands section. Please carefully read those pages first so that you can understand the methods used in this section.

Registering context menu commands

To create a context menu command, use the ContextMenuCommandBuilderopen in new window class. You can then set the type of the context menu (user or message) using the setType() method.

const { ContextMenuCommandBuilder, ApplicationCommandType } = require('discord.js');

const data = new ContextMenuCommandBuilder()
	.setName('User Information')
	.setType(ApplicationCommandType.User);
1
2
3
4
5

Receiving context menu command interactions

Context menus commands, just like slash commands, are received via an interaction. You can check if a given interaction is a context menu by invoking the isContextMenuCommand() method, or the isMessageContextMenuCommand() and isUserContextMenuCommand() methods to check for the specific type of context menu interaction:

client.on(Events.InteractionCreate, interaction => {
	if (!interaction.isUserContextMenuCommand()) return;
	console.log(interaction);
});

 


1
2
3
4

Extracting data from context menus

For user context menus, you can get the targeted user by accessing the targetUser or targetMember property from the UserContextMenuCommandInteractionopen in new window.

For message context menus, you can get the targeted message by accessing the targetMessage property from the MessageContextMenuCommandInteractionopen in new window.

client.on(Events.InteractionCreate, interaction => {
	if (!interaction.isUserContextMenuCommand()) return;
	// Get the User's username from context menu
	const { username } = interaction.targetUser;
	console.log(username);
});



 


1
2
3
4
5
6

Notes

  • Context menu commands cannot have subcommands or any options.
  • Responding to context menu commands functions the same as slash commands. Refer to our slash command responses guide for more information.
  • Context menu command permissions also function the same as slash commands. Refer to our slash command permissions guide for more information.