Skip to content
Extraits de code Groupes Projets

Comparer les révisions

Les modifications sont affichées comme si la révision source était fusionnée avec la révision cible. En savoir plus sur la comparaison des révisions.

Source

Sélectionner le projet cible
No results found

Cible

Sélectionner le projet cible
  • nunes2020/complobot
1 résultat
Afficher les modifications
Validations sur la source (2)
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);
}
......
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);
}
......
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());
}
}
......
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 result = re.test(sentence)
re.lastIndex = 0;
return result;
});
}
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