Message Create
Handling prefix-based message commands in nsCore
Overview
The messageCreate event enables prefix-based commands in nsCore.
This system exists alongside slash commands and is ideal for:
- Legacy command support
- Quick text-based interactions
- Lightweight moderation or utility commands
Message commands coexist with slash commands and follow the same permission and safety rules.
When Is messageCreate Triggered?
This event fires every time a message is sent in a guild where the bot is present.
nsCore immediately ignores:
- Messages from bots
- Direct messages
- Messages without the configured prefix
if (message.author.bot || !message.guild) return
if (!message.content.startsWith(BOT.PREFIX)) returnIgnoring DMs prevents security issues and keeps command behavior consistent.
Prefix Parsing
The command name and arguments are extracted using:
const args = message.content.slice(BOT.PREFIX.length).trim().split(/ +/g)
const commandName = args.shift()?.toLowerCase()This allows flexible spacing and natural argument input.
Command Resolution
Commands are resolved from the message command collection:
client.messageCommands.get(commandName)If the command does not exist:
- A warning is logged
- The user receives a friendly error embed
Permission Handling
User Permissions
Before execution, the user’s permissions are validated.
PermissionsBitField.resolve(command.userPermissions)If missing:
- Execution stops
- A warning embed is sent
Bot Permissions
The bot’s permissions are also verified.
message.guild.members.cache
.get(client.user.id)
?.permissions.has(...)If missing:
- The user is notified
- The command is not executed
Developer-Only Commands
Commands marked as devOnly can only be used by registered developers.
config.DEVELOPER_IDS.includes(message.author.id)Command Execution
After all validations pass, the command executes:
await command.executeMessage(message, args, client)On success:
- Execution is logged
On failure:
- Errors are logged
- A generic error embed is sent to the user
Centralized execution ensures predictable behavior across all message commands.
Error Handling Strategy
The entire execution is wrapped in a try/catch block to:
- Prevent crashes
- Maintain uptime
- Provide consistent user feedback
Best Practices
- Prefer slash commands for complex interactions
- Keep message commands simple and fast
- Avoid heavy computations inside this event
- Always validate permissions before execution
Summary
The messageCreate event:
- Powers prefix-based commands
- Supports legacy workflows
- Shares permission and security logic with slash commands
- Enhances flexibility without complexity
It is a supporting client event, not a replacement for slash commands.
Last updated on