Regex Rules
Delete messages that match, or fail to match, a pattern you write, and optionally tell the author why in a direct message.
Why Use This Feature?
- Enforces channel formats, such as "links only" or "numbers only", by deleting messages that don't match.
- Removes unwanted text, such as invite links, by deleting messages that do match.
- Explains the removal privately, so the author knows what to fix without a public warning.
How It Works
- What it reads — New messages and edits that change a message's text, from members other than bots. The bot sees edits only for messages it has seen since its last reconnect, up to the 200 most recent in each channel.
- How rules apply — Rules run in order, and each one skips messages outside its channel and authors with one of its excluded rolesRoles whose members a rule skips.
- Patterns — Each pattern is a JavaScript regular expressionA text pattern that matches names or messages, and matching is case-sensitive unless you add the
iflag. - Match setting — With Act when the message matches on, a rule acts when its pattern matches, and with it off, the rule acts when the pattern doesn't match.
- What it does — The first rule that applies sends its direct message, if any, then makes one attempt to delete the message, and no later rule runs.
The pattern runs against the message text as written, including Discord markup such as <@123456789012345678>. The bot posts no moderator alert and sets no audit-log reason.
When a regex rule deletes a message, affiliate cleanup and auto replies skip that message.
Requirements
- Message Content intentLets the bot read what messages actually say — Switch on this privileged intentAn intent you must switch on in the Developer Portal first for your application in the Discord Developer Portal.
- Channel permissions — The bot needs View Channel and Manage Messages wherever a rule applies.
- Direct messages — A member who blocks direct messages from server members doesn't receive the explanation, and the delete still happens.
Settings
Each rule has these fields.
| Field | Description |
|---|---|
Name (name) | The rule's name, up to 100 characters. |
Act when the message matches (match) | On to act when the pattern matches, off to act when it doesn't. |
Regular expression (regex) | The Pattern, up to 512 characters, and optional Flags, up to 16 characters. |
Channel (channel) | Optional single channel the rule is limited to. |
Roles to ignoreRoles whose members a rule skips (excluded-roles) | Optional list of 1 to 100 roles whose members the rule skips. |
Direct message → Text (direct-message-payload.content) | Optional text of up to 2,000 characters sent to the author before the delete. |
The direct message is sent as written, so a message variable the form offers, such as %MEMBER_MENTION%, isn't filled in.
Example
[
{
"name": "No invite links",
"match": true,
"regex": { "pattern": "discord\\.gg/", "flags": "i" },
"excluded-roles": [{ "role-id": "223456789012345678" }],
"direct-message-payload": { "content": "Invite links aren't allowed in this server." }
}
]
Limits
- Rules — Up to 100.
- Direct message — Up to 2,000 characters of plain text.
Troubleshooting
- A moderator's message was deleted — Administrators and the server owner aren't skipped automatically, so add their roles to Roles to ignoreRoles whose members a rule skips.
- A rule doesn't apply inside a thread — The channel filter compares the thread's own ID, so a rule limited to a parent channel doesn't cover its threads.
regex_rule_delete_failedin the bot log — The bot usually lacks Manage Messages in that channel, and the entry'serrorgives Discord's reason.regex_rule_direct_message_failedin the bot log — The author doesn't accept direct messages from the bot, and the delete still ran.
Changes from the legacy bot
The legacy botThe old v3.4.3 bot that ran from a config.json file skipped anyone with the Administrator permission, and a skipped member also stopped later rules from running. The rewrite skips only excluded rolesRoles whose members a rule skips, keeps evaluating later rules, and sends the direct message before the delete instead of at the same time.