2022-10-28 01:45:50 +00:00
'use strict' ;
2022-10-28 03:06:48 +00:00
const config = require ( 'config' ) ;
const crypto = require ( 'crypto' ) ;
const timers = require ( 'timers/promises' ) ;
2022-10-28 01:45:50 +00:00
const riddles = require ( '../../assets/riddles.json' ) ;
2022-10-28 03:06:48 +00:00
const style = require ( '../utils/style' ) ;
2022-10-28 01:45:50 +00:00
const open = new Map ( ) ;
2022-10-28 03:06:48 +00:00
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 {
2022-10-28 03:12:33 +00:00
await timers . setTimeout ( config . riddle . timeout * 1000 , null , { signal : ac . signal } ) ;
2022-10-28 03:06:48 +00:00
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 ) ;
}
2022-10-28 01:45:50 +00:00
}
module . exports = {
onCommand ,
2022-10-28 03:06:48 +00:00
onMessage ,
2022-10-28 01:45:50 +00:00
} ;