Skip to content
Extraits de code Groupes Projets
Valider 397bc536 rédigé par Adrien NUNES's avatar Adrien NUNES
Parcourir les fichiers

Aled

parent 5bf1c8e4
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
const complotDB = require('../database/ComplotDB.js'); const {getRandomReason} = require('../database/RandomSentence.js');
const {Category} = require('../database/Category.js');
class CommandPourquoi{ class CommandPourquoi{
constructor(client){ constructor(client){
...@@ -9,7 +9,7 @@ class CommandPourquoi{ ...@@ -9,7 +9,7 @@ class CommandPourquoi{
} }
getRandomPourquoi(){ getRandomPourquoi(){
const reason = complotDB.getRandom(Category.REASON); const reason = getRandomReason();
return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<why>', reason); return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<why>', reason);
} }
......
const complotDB = require('../database/ComplotDB.js'); const {getRandomActor} = require('../database/RandomSentence.js');
const {Category} = require('../database/Category.js');
class CommandQui{ class CommandQui{
constructor(client){ constructor(client){
...@@ -10,8 +9,7 @@ class CommandQui{ ...@@ -10,8 +9,7 @@ class CommandQui{
} }
getRandomQui(){ getRandomQui(){
const actor = complotDB.getRandom(Category.ACTOR); const actor = getRandomActor();
return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<who>', actor); return this.sentences[Math.floor(Math.random()*this.sentences.length)].replace('<who>', actor);
} }
......
const complotDB = require('../database/ComplotDB.js'); const {getRandomComplot, getRandomWelcome} = require('../database/RandomSentence.js');
const channelsID = require('../channels_conf.json'); const channelsID = require('../channels_conf.json');
const {Category} = require('../database/Category.js');
class InformeMoi{ class InformeMoi{
constructor(client){ constructor(client){
this.client = client; this.client = client;
this.channels = this.client.channels.cache; 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){ 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); this.client.channels.cache.get(channelsID.welcome).send(message);
} }
parse(message){ parse(message){
if(this.shouldRespond(message)){ if(this.shouldRespond(message)){
message.reply(this.getRandomComplot()); message.reply(getRandomComplot());
} }
} }
......
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
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter