Skip to content

Commit 4755c34

Browse files
didineleDanktuary
authored andcommitted
Fix the Mention prefix example (discordjs#188)
1 parent 317e7a7 commit 4755c34

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

guide/popular-topics/miscellaneous-examples.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ const Discord = require('discord.js');
8484
const client = new Discord.Client();
8585
const prefix = '!';
8686

87+
const escapeRegex = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
88+
8789
client.on('message', message => {
88-
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|\\${prefix})\\s*`);
90+
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`);
8991
if (!prefixRegex.test(message.content)) return;
9092

9193
const [, matchedPrefix] = message.content.match(prefixRegex);
@@ -102,6 +104,10 @@ client.on('message', message => {
102104
client.login('your-token-goes-here');
103105
```
104106

107+
::: tip
108+
The `escapeRegex` function is used to convert special characters into literal characters by escaping them, so that they don't terminate the pattern within the [Regular Expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)!
109+
:::
110+
105111
::: tip
106112
If you aren't familiar with the syntax used on the `const [, matchedPrefix] = ...` line, that's called "array destructuring". Feel free to read more about it in the [ES6 syntax](/additional-info/es6-syntax.md#array-destructuring) guide!
107113
:::

0 commit comments

Comments
 (0)