'use strict'; const config = require('config'); const crypto = require('crypto'); const timers = require('timers/promises'); const riddles = require('../../assets/riddles.json'); const style = require('../utils/style'); const open = new Map(); const emojis = ['😆', 'ðŸĪĢ', '😂', '😅', 'ðŸĪŠ', '😛', '😝', '🙃', 'ðŸĨē', 'ðŸĪ­', '😏', 'ðŸĨī', '💀', 'ðŸĪĶ']; async function onCommand(args, context) { if (open.has(context.room.id)) { context.sendMessage(open.get(context.room.id).riddle, context.room.id); return; } const riddle = riddles[crypto.randomInt(0, riddles.length)]; const ac = new AbortController(); // eslint-disable-line no-undef open.set(context.room.id, { ...riddle, ac }); context.sendMessage(riddle.riddle, context.room.id); context.logger.info(`Riddle asked "${riddle.riddle}" with answer: ${riddle.answers[0]}`); try { await timers.setTimeout(config.riddle.timeout * 1000, null, { signal: ac.signal }); open.delete(context.room.id); context.sendMessage(`No one guessed the riddle: ${style.bold(riddle.fullAnswer)}! ${emojis[crypto.randomInt(0, emojis.length)]}`, context.room.id); } catch (error) { // timer aborted because someone guessed the answer } } function onMessage(message, context) { const riddle = open.get(context.room?.id); if (riddle && riddle.answers.some((answer) => new RegExp(`\\b${answer}s?\\b`, 'i').test(message.body))) { riddle.ac.abort(); open.delete(context.room.id); if (config.riddle.score) { context.setPoints(context.user, 1); context.sendMessage(`${style.bold(style.yellow(riddle.fullAnswer))} is the right answer! ${context.user.prefixedUsername} gets a point.`, context.room.id); return; } context.sendMessage(`${style.bold(riddle.fullAnswer)} is the right answer!`, context.room.id); } } module.exports = { onCommand, onMessage, };