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
| Method | Returns | Description |
|---|---|---|
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
| Method | Returns | Description |
|---|---|---|
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
| Method | Returns | Description |
|---|---|---|
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
| Method | Returns | Description |
|---|---|---|
middleware.list() | string[] | All registered middleware keys |
middleware.chain() | Promise<MiddlewareChainEntry[]> | Get the ordered, enabled middleware chain |
Each MiddlewareChainEntry contains the following fields.
| Field | Type | Description |
|---|---|---|
key | string | Middleware identifier |
handler | function | The middleware function |
order | number | Execution order |
enabled | boolean | Whether 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.
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}`
}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
