Creating a basic context menu command
In order to register Context Menu Commands, we first need to override the registerApplicationCommands
method. For
example:
Example
- JavaScript
- ESM
- TypeScript
const { Command } = require('@sapphire/framework');const { ApplicationCommandType } = require('discord-api-types/v9');class UserCommand extends Command { constructor(context, options) { super(context, { ...options }); } registerApplicationCommands(registry) { registry.registerContextMenuCommand((builder) => builder // .setName('ping') .setType(ApplicationCommandType.Message) ); }}module.exports = { UserCommand};
import { Command } from '@sapphire/framework';import { ApplicationCommandType } from 'discord-api-types/v9';export class UserCommand extends Command { constructor(context, options) { super(context, { ...options }); } registerApplicationCommands(registry) { registry.registerContextMenuCommand((builder) => builder // .setName('ping') .setType(ApplicationCommandType.Message) ); }}
import { Command } from '@sapphire/framework';import { ApplicationCommandType } from 'discord-api-types/v9';export class UserCommand extends Command { public constructor(context: Command.Context, options: Command.Options) { super(context, { ...options }); } public override registerApplicationCommands(registry: Command.Registry) { registry.registerContextMenuCommand((builder) => builder // .setName('ping') .setType(ApplicationCommandType.Message) ); }}
The above will create a Context Menu
command called ping
of ApplicationCommandType.Message
type.
Creating the contextMenuRun
method
To handle the command callback when the context menu command is ran we need to override the contextMenuRun
method of
command class.
- JavaScript
- ESM
- TypeScript
const { Command } = require('@sapphire/framework');class PingCommand extends Command { constructor(context, options) { // ... } async contextMenuRun(interaction) { return interaction.reply('Pong'); } registerApplicationCommands(registry) { // ... }}module.exports = { PingCommand};
import { Command } from '@sapphire/framework';export class PingCommand extends Command { constructor(context, options) { // ... } async contextMenuRun(interaction) { return interaction.reply('Pong'); } registerApplicationCommands(registry) { // ... }}
import { Command } from '@sapphire/framework';import type { Message } from 'discord.js';export class PingCommand extends Command { public constructor(context: Command.Context, options: Command.Options) { // ... } public async contextMenuRun(interaction: Command.ContextMenuInteraction) { return interaction.reply('Pong'); } public override registerApplicationCommands(registry: Command.Registry) { // ... }}