Skip to content
Spraxium

Primary navigation

Execution and Guild Prefixes

Prefix resolution follows a specific precedence hierarchy that starts with guild overrides. Learn how to configure global and per-guild prefixes, cooldowns, and the prefix resolution pipeline.

Prefix resolution precedence

When the framework receives a message, the PrefixParser checks whether the text starts with a recognized prefix. Resolution follows a strict precedence order: guild-specific prefixes are checked first, then the global prefix, and finally bot mention if enabled. The first match wins and the remaining options are skipped.

If ? is a guild prefix and ! is the global one, both ?roll 6 and !roll 6 can work in that guild. The parser looks for a matching guild prefix first. It tries the global prefixes if none matches the message, then a mention of the bot when enabled. A guild prefix of !! is tried before a global prefix of !.

The resolution order is important for bots that serve multiple communities with different conventions. A moderation-focused guild might prefer . as a prefix because it is quiet and unobtrusive, while a gaming guild might choose >> for visibility. The guild prefix system lets each community set its own convention without affecting others.

Check guild overrides

The parser calls PrefixGuildManager.getGuildPrefix with the guild ID from the incoming message. If the guild has registered one or more prefixes, the parser tests the message against each of them in order. The first matching prefix is used and the parser moves directly to tokenization. Guild prefixes can be a single string or an array of strings.

Check global prefix

If no guild override matches (or the message is from a DM), the parser tests the message against the global prefix defined in PrefixConfig. The global prefix also supports arrays, so you can define multiple global prefixes like ['!', '?']. If the message starts with any of the global prefixes, the first match is used.

Check mention prefix

If mentionPrefix is enabled (the default), the parser checks whether the message starts with a bot mention like <@BOT_ID>. This acts as a universal prefix that always works regardless of guild or global configuration. Mention prefix is useful when users forget the configured prefix and need a reliable fallback.

PrefixConfig reference

Set the prefix block in spraxium.config.ts with defineConfig from @spraxium/core. The prefix field within that block is required. Enable the Message Content intent in the app and Discord Developer Portal. Without the config block, the framework does not bind the prefix dispatcher to incoming messages.

spraxium.config.ts
import { defineConfig } from '@spraxium/core';
import { loadGuildPrefixes } from './src/prefix/guild-prefix.provider';

export default defineConfig({
  prefix: {
    prefix: '!',
    caseSensitive: false,
    mentionPrefix: true,
    defaultCooldown: 3,
    replyOnNotFound: true,
    guildPrefixProvider: loadGuildPrefixes,
  },
});

Your config file should define only global policy and initial hydration for the prefix subsystem. Prefix read and write logic, auditing, and synchronization should live in services that inject PrefixDispatcher. The Prefixes API via Services page shows this architecture in depth for production workflows.

Managing guild prefixes at runtime

The PrefixGuildManager is accessible through the PrefixDispatcher and provides a complete API for reading, writing, and removing guild-specific prefix overrides. Changes take effect immediately for the next incoming message without requiring a restart. This lets you build admin commands or dashboard integrations that update prefixes dynamically.

Each guild entry stores the guild ID, one or more prefixes, and an optional metadata record. The metadata field is not used by the framework itself, but it gives you a place to store context like who changed the prefix and when. This is useful for audit logging in moderation bots.

The loadFromProvider method is called automatically at startup if you define a guildPrefixProvider in the config. You can also call it manually to reload guild prefixes from your database at any time, for example after a bulk import or migration.

Runtime prefix mutation example

A common pattern is building an admin command that lets guild owners change their prefix through a message command. The handler injects the PrefixDispatcher to access the guildPrefixes manager and persists the change to a database. The runtime update takes effect immediately for subsequent messages.

import { PrefixArg, PrefixCommand } from '@spraxium/common';

@PrefixCommand({ name: 'set-prefix' })
export class SetPrefixCommand {
  @PrefixArg.String('newPrefix', { minLength: 1, maxLength: 5 })
  build() {}
}

Cooldown system

A cooldown is stored in memory per user and top-level command. Guards run first, so a user without permission gets the guard response. If an authorized user calls the command again too soon, Spraxium passes a CooldownException to the exception handler with the remaining seconds. .check(commandName, userId) returns the remaining seconds and .clear() resets the stored entries.

The per-command cooldown field in @PrefixCommand overrides the global defaultCooldown. If a command sets cooldown: 5 and the global default is 3, that command uses a 5-second cooldown.