diff --git a/commands/CommandPourquoi.js b/commands/CommandPourquoi.js index 746d6e9fed96e003a3100d4904788dc8d6ee5f99..e9d493f7342de18961886ec4803868878caec28d 100644 --- a/commands/CommandPourquoi.js +++ b/commands/CommandPourquoi.js @@ -1,5 +1,5 @@ -const complotDB = require('../database/ComplotDB.js'); -const {Category} = require('../database/Category.js'); +const {getRandomReason} = require('../database/RandomSentence.js'); + class CommandPourquoi{ constructor(client){ @@ -9,7 +9,7 @@ class CommandPourquoi{ } getRandomPourquoi(){ - const reason = complotDB.getRandom(Category.REASON); + const reason = getRandomReason(); return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<why>', reason); } diff --git a/commands/CommandQui.js b/commands/CommandQui.js index c17819b2980871f5e3ff4eb0ef9e5094d1ed4a5c..c2d59e1aebc66748149c070c042c30144917f1ed 100644 --- a/commands/CommandQui.js +++ b/commands/CommandQui.js @@ -1,6 +1,5 @@ -const complotDB = require('../database/ComplotDB.js'); -const {Category} = require('../database/Category.js'); +const {getRandomActor} = require('../database/RandomSentence.js'); class CommandQui{ constructor(client){ @@ -10,8 +9,7 @@ class CommandQui{ } getRandomQui(){ - const actor = complotDB.getRandom(Category.ACTOR); - + const actor = getRandomActor(); return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<who>', actor); } diff --git a/commands/InformeMoi.js b/commands/InformeMoi.js index 477bb8e31f0b223975ee1dca20d3892b5821ca72..97249b41f08b178e63959abe6156027c4032076f 100644 --- a/commands/InformeMoi.js +++ b/commands/InformeMoi.js @@ -1,58 +1,22 @@ -const complotDB = require('../database/ComplotDB.js'); +const {getRandomComplot, getRandomWelcome} = require('../database/RandomSentence.js'); const channelsID = require('../channels_conf.json'); -const {Category} = require('../database/Category.js'); class InformeMoi{ constructor(client){ this.client = client; this.channels = this.client.channels.cache; - this.actorRE = new RegExp('<actor>', 'gi'); - this.wantRE = new RegExp('<want>', 'gi'); - this.reasonRE = new RegExp('<reason>', 'gi'); - this.sourceRE = new RegExp('<source>', 'gi'); - this.actionRE = new RegExp('<action>', 'gi'); - } - - fillAllRandomObject(sentence, regularExpr, category){ - const matches = sentence.matchAll(regularExpr); - let objectString = ""; - let newSentence = sentence; - for(let match of matches){ - objectString = complotDB.getRandom(category); - newSentence = newSentence.replace(match, objectString); - } - - return newSentence; - } - - getRandomSentenceFilled(sentence){ - sentence = this.fillAllRandomObject(sentence, this.actorRE, Category.ACTOR); - sentence = this.fillAllRandomObject(sentence, this.actionRE, Category.ACTION); - sentence = this.fillAllRandomObject(sentence, this.reasonRE, Category.REASON); - sentence = this.fillAllRandomObject(sentence, this.sourceRE, Category.SOURCE); - - sentence = sentence.replaceAll(this.wantRE, 'veut'); /**Todo, change */ - return sentence; - } - - getRandomComplot(){ - return this.getRandomSentenceFilled(complotDB.getRandom(Category.SENTENCE)); - } - - getRandomWelcome(){ - return this.getRandomSentenceFilled(complotDB.getRandom(Category.WELCOME)); } sayWelcome(userID){ - let message = this.getRandomWelcome().replace('<new_user>', `<@${userID}>`); + let message = getRandomWelcome().replace('<new_user>', `<@${userID}>`); this.client.channels.cache.get(channelsID.welcome).send(message); } parse(message){ if(this.shouldRespond(message)){ - message.reply(this.getRandomComplot()); + message.reply(getRandomComplot()); } } diff --git a/database/RandomSentence.js b/database/RandomSentence.js new file mode 100644 index 0000000000000000000000000000000000000000..5ef7d4c4953ef05c5558bdf578d817e4a5a0c40c --- /dev/null +++ b/database/RandomSentence.js @@ -0,0 +1,75 @@ + +const complotDB = require('./ComplotDB.js'); +const { Category } = require('./Category.js'); + +const actorRE = new RegExp('<actor>', 'gi'); +const wantRE = new RegExp('<want>', 'gi'); +const reasonRE = new RegExp('<reason>', 'gi'); +const sourceRE = new RegExp('<source>', 'gi'); +const actionRE = new RegExp('<action>', 'gi'); + +const MAX_ITERATIONS = 10; + +function fillAllRandomObject(sentence, regularExpr, category){ + const matches = sentence.matchAll(regularExpr); + let objectString = ""; + let newSentence = sentence; + for (let match of matches) { + objectString = complotDB.getRandom(category); + newSentence = newSentence.replace(match, objectString); + } + + return newSentence; +} + +function shouldBeFilled(sentence) { + return [actorRE, reasonRE, sourceRE, actionRE].some((re) =>{ + const b = re.test(sentence) + if(b){ + const matches = sentence.matchAll(re); + for(let match of matches) console.log(match); + } + }); +} + +function getRandomSentenceFilled(sentence){ + let it = 0; + while(shouldBeFilled(sentence)){ + sentence = fillAllRandomObject(sentence, actorRE, Category.ACTOR); + sentence = fillAllRandomObject(sentence, actionRE, Category.ACTION); + sentence = fillAllRandomObject(sentence, reasonRE, Category.REASON); + sentence = fillAllRandomObject(sentence, sourceRE, Category.SOURCE); + + it++; + if(it >= MAX_ITERATIONS) break; + } + + + sentence = sentence.replaceAll(wantRE, 'veut'); + return sentence; +} + +function getRandomComplot(){ + return getRandomSentenceFilled(complotDB.getRandom(Category.SENTENCE)); +} + +function getRandomActor(){ + return getRandomSentenceFilled(complotDB.getRandom(Category.ACTOR)); +} + +function getRandomReason(){ + return getRandomSentenceFilled(complotDB.getRandom(Category.REASON)); +} + +function getRandomWelcome(){ + return getRandomSentenceFilled(complotDB.getRandom(Category.WELCOME)); +} + + +module.exports = { + getRandomSentenceFilled, + getRandomComplot, + getRandomActor, + getRandomReason, + getRandomWelcome +} \ No newline at end of file