Context Menus

Context Menus are application commands which appear on right clicking or tapping a user or a message.

TIP

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

Registering context menu commands

To create a context menu you construct a new ContextMenuCommandBuilder. 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('echo')
	.setType(ApplicationCommandType.User);
1
2
3
4
5

Receiving context menu command interactions

Context Menus are received via an interaction. You can check if a given interaction is a context menu by invoking the isContextMenuCommand() method, you can use the isMessageContextMenuCommand() and isUserContextMenuCommand() methods to check for the specific type of context menu interaction:

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

 


1
2
3
4

Extracting data from context menus

You can get the targeted user by accessing the targetUser or targetMember property from the UserContextMenuInteractionopen in new window. You can get the message by accessing the targetMessage property from the MessageContextMenuInteractionopen in new window.

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



 


1
2
3
4
5
6