From 2af5a1e7a75889e286800addaae9889545741eeb Mon Sep 17 00:00:00 2001 From: Loic Bersier Date: Wed, 28 Nov 2018 17:09:24 +0100 Subject: [PATCH] always choose random word --- commands/fun/random.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/commands/fun/random.js b/commands/fun/random.js index 1095949..7e4bf62 100644 --- a/commands/fun/random.js +++ b/commands/fun/random.js @@ -24,21 +24,25 @@ module.exports = class RandomCommand extends Command { if(blacklistJson[message.author.id]) return blacklist(blacklistJson[message.author.id] , message) + // Load all the different files const verb = require('../../dictionary/verbs.json') const noun = require('../../dictionary/nouns.json') const adverb = require('../../dictionary/adjectives.json') const adjective = require('../../dictionary/adverbs.json') -// Generate a random number for each files - let verbRand = Math.floor((Math.random() * verb.length) + 1); - let nounRand = Math.floor((Math.random() * noun.length) + 1); - let adverbRand = Math.floor((Math.random() * adverb.length) + 1); - let adjectiveRand = Math.floor((Math.random() * adjective.length) + 1); +// Generate a random number + function randNumber(file) { + let Rand = Math.floor((Math.random() * file.length) + 1); + return Rand; + } // Replace with a random word from the json - text = text.replace('[verb]', verb[verbRand]) - text = text.replace('[adverb]', adverb[adverbRand]) - text = text.replace('[noun]', noun[nounRand]) - text = text.replace('[adjective]', adjective[adjectiveRand]) + do { + text = text.replace(/\[verb\]/, verb[randNumber(verb)]) + text = text.replace(/\[adverb\]/, adverb[randNumber(adverb)]) + text = text.replace(/\[noun\]/, noun[randNumber(noun)]) + text = text.replace(/\[adjective\]/, adjective[randNumber(adjective)]) + } while( text.includes('[verb]') || text.includes('[adverb]') || text.includes('[noun]') || text.includes('[adjective]') ) + // Send the final text message.say(text); }