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
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:
import type { UserContextMenuCommandInteraction, User } from 'discord.js'
export default (interaction: UserContextMenuCommandInteraction, user: User) => {
return `User: ${user.username} (${user.id})\nCreated: ${user.createdAt.toDateString()}`
}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:
import type { MessageContextMenuCommandInteraction, Message } from 'discord.js'
export default async (interaction: MessageContextMenuCommandInteraction, message: Message) => {
await message.pin()
return 'Message pinned!'
}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 messageInteractionReplyOptions-- 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:
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()}`
}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:
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!'
}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 // 3This is useful when you need to check or set context menu types programmatically.
