Escaping trivia dataset before passing it into regex.
This commit is contained in:
+10
-1
@@ -226,6 +226,15 @@ function onCommand(args, context) {
|
||||
}
|
||||
}
|
||||
|
||||
// answers are plain text and may contain regex characters, like '*batteries not included'
|
||||
function getAnswerRegexp(answer) {
|
||||
const pattern = answer
|
||||
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
.replace(/\\[.?*]|!/g, '$&?'); // punctuation is optional, so T.S. Eliot also matches TS Eliot, and *NSYNC matches NSYNC
|
||||
|
||||
return new RegExp(pattern, 'i');
|
||||
}
|
||||
|
||||
async function onMessage(message, context) {
|
||||
const game = games.get(context.room?.id);
|
||||
|
||||
@@ -235,7 +244,7 @@ async function onMessage(message, context) {
|
||||
|
||||
const { answer, fullAnswer } = game.questions[game.round];
|
||||
|
||||
if (new RegExp(answer, 'i').test(decode(message.originalBody || message.body)) && !game.answers.has(context.user.id)) { // resolve HTML entities in case original body is not available, such as & to &
|
||||
if (getAnswerRegexp(answer).test(decode(message.originalBody || message.body)) && !game.answers.has(context.user.id)) { // resolve HTML entities in case original body is not available, such as & to &
|
||||
game.answers.set(context.user.id, {
|
||||
user: context.user,
|
||||
answer: message.body,
|
||||
|
||||
Reference in New Issue
Block a user