AutoMod allows you to filter messages based on regular expression (regex) patterns in your Custom Keyword rules.
Regular expressions let you define patterns that you want AutoMod to detect in messages and respond to accordingly, and can be a more efficient and effective way to filter out unwanted messages in your community.
How it Works
A regex pattern is like a bunch of keywords all packed into one. By filtering based on patterns instead of individual keywords, you can save time and effort from having to think of 50 variations of “free Nitro” that you’d want to filter out and adding each of them to your rules - instead, you could write a singular regex pattern that’ll do the same job.
This also makes regex an effective way to prevent people who try to bypass your keyword filters by intentionally misspelling words or using word variants, or block messages containing non-words like phone numbers, addresses (email, IP, physical), and that weird-lookin’ zalgo text.
For Example: Trying to block or receive alerts whenever someone posts a message containing an email address in your server? You can’t enter every possible email address that exists into your keyword filters (and you really don’t want to), but you can use a regex pattern like the above to pattern-match the format of a typical email address. This lets AutoMod flag messages containing all kinds of email addresses!
As you begin, note the following:
- Find regex filtering in Server Settings > AutoMod > any of your Custom Keyword Rules (must have “Manage Server” or “Administrator” permissions)
- We use the Rust flavor of regex and recommend writing your regex in Rust syntax to minimize errors. To edit and test your regex syntax in Rust, we recommend using Rustexp.
- Case-insensitive and unicode support flags are on by default for every regex pattern.
Be careful!
Regex filtering is advanced and not for the faint of heart. Improper regular expressions could prevent all users from being able to send messages that are acceptable in your community. We recommend only using this if you know what you’re doing.
Be sure to use an external tool like Rustexp to validate your Regex and make sure it works as expected.
FAQ
Q: What do I do if my regex syntax is invalid or doesn’t work?
A: Considerations to keep in mind to validate your regex:
- Always test and validate your syntax before saving your changes. Valid syntax means your regex does not contain any syntax errors.
- Ensure your syntax compiles below our limits - even if your regex has valid syntax, it might still not be accepted if it is too complex and exceeds some of our technical limits.
- Regular expressions must not include backreferences since those are not supported.
If you find your regex isn’t working, go through these steps:
- Check that your syntax is valid. Because AutoMod uses the Rust-flavor of regex, we recommend testing your regular expressions in an engine that also uses Rust, such as Rustexp.
- Make sure the word or type of message you're targeting works as expected.
- Paste your regex into AutoMod and confirm your syntax doesn't contain errors. Save the rule to enable the regular expression.
- It is recommended to send a few messages that you expect to be blocked, as well as a few messages that you expect to go through from a non-moderator account to ensure your regular expression is behaving as expected.
Q: What are all the different types of regex errors and what do they mean OR and how do I fix them?
A: The different type of errors include:
- Invalid syntax - Your regex contains a syntax error(s).
- Compile size exceeded limit. The regular expression compiled above our size limit.
- Too long. Regular expressions are limited to 10 expressions total with 75 characters each.
Q: How can I enable or disable regular expression flags, such as case insensitivity?
A: Regular expressions can be used with flags to instruct the compiler how to evaluate the regular expression. The Rust regex crate supports the following flags:
- u unicode
- i case insensitive
- s dot matches newline
- m multiline
Case-insensitive and unicode support flags are on by default for every regex pattern. However, you can overwrite these flags or enable other flags by prefixing your regular expression with a modifier. The syntax to enable a modifier is: (?a), and the syntax to disable a monitor is:(?-a), where a is the flag you wish to enable to disable.
Q: Do you have any examples of starter regular expressions?
A:Match all braille characters detailed in this link here!
- [\u2800-\u28FF]+
Match all Greek letters except alpha:
- [\p{Greek}--β]+