LogoRobo.js

Namespace Controllers

Programmatic access to commands, events, and middleware

Namespace controllers provide programmatic access to your bot's commands, events, context menus, and middleware. They are ready-to-use objects exported directly from @robojs/discordjs.

Most bots don't need these. They are useful when you need dynamic features like custom help commands, programmatic event emission, or runtime introspection of registered handlers.

For per-handler controllers that enable/disable individual commands at runtime, server restrictions, and middleware ordering, see the @robojs/discordjs Namespace Controllers reference.

Overview

Import the controllers directly from the plugin package.

// Any file in your project
import { commands, events, context, middleware } from '@robojs/discordjs'
// Any file in your project
import { commands, events, context, middleware } from '@robojs/discordjs'

Each controller exposes methods for listing, retrieving, and executing handlers that Robo.js has registered from your file system.

Commands Controller

MethodReturnsDescription
commands.list()string[]All registered command keys
commands.get(name)Promise<CommandHandler | null>Get a command handler by name
commands.execute(name, interaction)Promise<void>Execute a command programmatically
import { commands } from '@robojs/discordjs'

const allCommands = commands.list()
console.log('Available commands:', allCommands)

await commands.execute('ping', interaction)
import { commands } from '@robojs/discordjs'

const allCommands = commands.list()
console.log('Available commands:', allCommands)

await commands.execute('ping', interaction)

Events Controller

MethodReturnsDescription
events.list()string[]All event names with registered handlers
events.get(name)Promise<EventHandler[]>Get all handlers for an event
events.emit(name, ...args)Promise<void>Emit an event programmatically
import { events } from '@robojs/discordjs'

const registeredEvents = events.list()
console.log('Registered events:', registeredEvents)

await events.emit('messageCreate', message)
import { events } from '@robojs/discordjs'

const registeredEvents = events.list()
console.log('Registered events:', registeredEvents)

await events.emit('messageCreate', message)

Context Controller

MethodReturnsDescription
context.list()string[]All registered context menu keys
context.get(name)Promise<ContextHandler | null>Get a context menu handler by name
import { context } from '@robojs/discordjs'

const menus = context.list()
console.log('Context menus:', menus)
import { context } from '@robojs/discordjs'

const menus = context.list()
console.log('Context menus:', menus)

Middleware Controller

MethodReturnsDescription
middleware.list()string[]All registered middleware keys
middleware.chain()Promise<MiddlewareChainEntry[]>Get the ordered, enabled middleware chain

Each MiddlewareChainEntry contains the following fields.

FieldTypeDescription
keystringMiddleware identifier
handlerfunctionThe middleware function
ordernumberExecution order
enabledbooleanWhether the middleware is active
import { middleware } from '@robojs/discordjs'

const chain = await middleware.chain()
console.log('Middleware chain:', chain.map((m) => m.key))
import { middleware } from '@robojs/discordjs'

const chain = await middleware.chain()
console.log('Middleware chain:', chain.map((m) => m.key))

Use Cases

  • Dynamic toggling: Check if a command exists before executing it.
  • Programmatic emission: Trigger event handlers from code without waiting for Discord gateway events.
  • Custom help commands: List all registered commands dynamically.
src/commands/help.ts
import { commands } from '@robojs/discordjs'
import type { ChatInputCommandInteraction } from 'discord.js'

export default (interaction: ChatInputCommandInteraction) => {
	const allCommands = commands.list()
	const commandList = allCommands.map((cmd) => `\`/${cmd}\``).join(', ')
	return `Available commands: ${commandList}`
}
src/commands/help.js
import { commands } from '@robojs/discordjs'

export default (interaction) => {
	const allCommands = commands.list()
	const commandList = allCommands.map((cmd) => `\`/${cmd}\``).join(', ')
	return `Available commands: ${commandList}`
}

Discord showing the output of a /help command built with namespace controllers, listing available commands formatted as /ping, /greet, /color

FocusThe dynamically generated command listZoom100%NotesShow a user running /help and the bot responding with a list of available commands. Commands formatted as code spans.

Next Steps

On this page