Prefix commands
Slash commands, context menus, buttons, select menus, modals and autocomplete are interactions: Shardlens records them
on its own. Prefix commands (!ban @someone, or @YourBot ban @someone) are ordinary messages, so Shardlens can’t tell
one from chat, and it never reads message content to find out. Your bot already knows: tell Shardlens when your own
parser has matched a command.
import type { Shardlens } from '@shardlens/discordjs';import { type Client, Events, type Message } from 'discord.js';
declare const client: Client;declare const lens: Shardlens;
interface PrefixCommand { name: string; execute(message: Message, args: string[]): Promise<void>;}declare const commands: Map<string, PrefixCommand>;declare const aliases: Map<string, string>;
const PREFIX = '!';
client.on(Events.MessageCreate, async (message) => { if (message.author.bot || !message.content.startsWith(PREFIX)) return; const [typed = '', ...args] = message.content.slice(PREFIX.length).trim().split(/\s+/); const command = commands.get(aliases.get(typed.toLowerCase()) ?? typed.toLowerCase()); // Only commands your bot knows are recorded, so random text after the prefix never reaches Shardlens. if (!command) return;
// Pass the command's own name, never `typed`. Shardlens doesn't read the message's content. const run = lens.wrapMessageCommand(command.name, (m: Message) => command.execute(m, args)); await run(message);});wrapMessageCommand(command, handler) records how long the handler took and whether it threw, then re-throws the error
unchanged, like wrap() does for interactions. Pass { command, sub, group } instead of a
name when the command has subcommands.
If your bot catches errors itself
Section titled “If your bot catches errors itself”Wrap the call inside your try: the error passes through the wrapper, which records it, and on to your catch.
try { await lens.wrapMessageCommand(command.name, (m: Message) => command.execute(m, args))(message);} catch { await message.reply('Something went wrong.');}Where a wrapper doesn’t fit, report the run once it is over. failed: true records a failure your own catch
handled; error records the error’s class and Discord error code as well:
const started = performance.now();let failed = false;try { await command.execute(message, args);} catch (error) { failed = true; await message.reply('Something went wrong.');}lens.trackMessageCommand(message, { command: command.name, durationMs: performance.now() - started, failed });What is recorded
Section titled “What is recorded”A prefix command is stored with your interactions, so the Commands page, unique users, servers, funnels, alerts on
error rate and the Prometheus and OpenTelemetry exporters all count it. On the Commands page, a prefix command has a
“prefix” badge, and its name has a / only if it also runs as a slash command. Filter or group by kind
(message_command, shown as “Prefix command”) to see the two apart.
From the message, the SDK reads the author’s ID (hashed in your bot, as for interactions), the server, the channel
type, the server’s language and the author’s permissions (for the admin, manager or member split). From your call, it
takes the command name, subcommand, the names of the arguments you list in options and, only for names in
captureOptionValues, their values, except any holding a Discord ID such as a
mention. Never the message text, attachments, mentions or the prefix itself. The full list is on Data collected.
A message has no response deadline, no user language and no entitlements, so prefix commands are left out of the
3-second deadline figures, response time, the Reliability page and premium share. Their failures show on the
Commands page and in the error_rate alert.
Slash and prefix commands that share one handler
Section titled “Slash and prefix commands that share one handler”If both kinds of command end in the same function (a prefix command dressed up as an interaction, for example), you can
wrap that function with lens.wrap(). It records interactions and passes anything else straight to your handler, so
report the prefix run with trackMessageCommand where you still have the Message.
Opt-outs apply to prefix commands too, and disable: ['interactions'] turns them off along with interactions.