LogoRobo.js

Context Menus

User and message context menu commands with type-safe configuration.

Context menus appear when users right-click on a user or message in Discord. They're defined by placing files in src/context/user/ or src/context/message/.

File structure

Get Info.ts"Get Info" user context menu
Pin Message.ts"Pin Message" message context menu

The file name becomes the context menu label. Unlike slash commands, context menu names can include spaces and capital letters.

User context menus

The handler receives a UserContextMenuCommandInteraction and the targeted User:

src/context/user/Get Info.ts
import type { UserContextMenuCommandInteraction, User } from 'discord.js'

export default (interaction: UserContextMenuCommandInteraction, user: User) => {
	return `User: ${user.username} (${user.id})\nCreated: ${user.createdAt.toDateString()}`
}
src/context/user/Get Info.js
export default (interaction, user) => {
	return `User: ${user.username} (${user.id})\nCreated: ${user.createdAt.toDateString()}`
}

Message context menus

The handler receives a MessageContextMenuCommandInteraction and the targeted Message:

src/context/message/Pin Message.ts
import type { MessageContextMenuCommandInteraction, Message } from 'discord.js'

export default async (interaction: MessageContextMenuCommandInteraction, message: Message) => {
	await message.pin()
	return 'Message pinned!'
}
src/context/message/Pin Message.js
export default async (interaction, message) => {
	await message.pin()
	return 'Message pinned!'
}

Return types

Context menu handlers can return the same types as slash command handlers (CommandResult):

  • string -- replied as a text message
  • InteractionReplyOptions -- replied with embeds, components, etc.
  • void -- you handle the reply yourself

When Sage mode is enabled (the default), the return value is automatically sent as a reply to the interaction.

Context config

Export a config object to configure the context menu. All fields are optional.

Prop

Type

You can use the ContextConfig type for annotation:

src/context/user/Get Info.ts
import type { ContextConfig } from '@robojs/discordjs'
import type { UserContextMenuCommandInteraction, User } from 'discord.js'

export const config: ContextConfig = {
	description: 'View user information',
	contexts: ['Guild']
}

export default (interaction: UserContextMenuCommandInteraction, user: User) => {
	return `User: ${user.username} (${user.id})\nCreated: ${user.createdAt.toDateString()}`
}
src/context/user/Get Info.js
export const config = {
	description: 'View user information',
	contexts: ['Guild']
}

export default (interaction, user) => {
	return `User: ${user.username} (${user.id})\nCreated: ${user.createdAt.toDateString()}`
}

Type-safe configs with createContextConfig()

Use createContextConfig() for type-safe context menu configuration:

src/context/message/Pin Message.ts
import { createContextConfig } from '@robojs/discordjs'
import type { MessageContextMenuCommandInteraction, Message } from 'discord.js'

export const config = createContextConfig({
	description: 'Pin this message',
	defaultMemberPermissions: 'ManageMessages',
	contexts: ['Guild']
})

export default async (interaction: MessageContextMenuCommandInteraction, message: Message) => {
	await message.pin()
	return 'Message pinned!'
}
src/context/message/Pin Message.js
export const config = {
	description: 'Pin this message',
	defaultMemberPermissions: 'ManageMessages',
	contexts: ['Guild']
}

export default async (interaction, message) => {
	await message.pin()
	return 'Message pinned!'
}

ContextType constant

The ContextType constant maps context menu types to their Discord API values:

import { ContextType } from '@robojs/discordjs'

ContextType.User    // 2
ContextType.Message // 3

This is useful when you need to check or set context menu types programmatically.

Next steps

On this page