From c2ceff28ffb6d05d0f36bac246f9e2b4b6b21a3a Mon Sep 17 00:00:00 2001
From: Adrien Nunes <adrien.nunes@ensiie.fr>
Date: Thu, 25 Nov 2021 19:03:08 +0100
Subject: [PATCH] Welcome message

---
 vote/channels_conf.json => channels_conf.json |  3 ++-
 commands/InformeMoi.js                        | 19 ++++++++++++++++---
 database/ComplotDB.js                         | 11 +++++++++++
 index.js                                      |  7 ++++++-
 vote/VoteParser.js                            |  2 +-
 5 files changed, 36 insertions(+), 6 deletions(-)
 rename vote/channels_conf.json => channels_conf.json (72%)

diff --git a/vote/channels_conf.json b/channels_conf.json
similarity index 72%
rename from vote/channels_conf.json
rename to channels_conf.json
index 3de5905..d839938 100644
--- a/vote/channels_conf.json
+++ b/channels_conf.json
@@ -4,5 +4,6 @@
     "reason" : "895047902123143210",
     "source" : "896855116425072711",
     "sentence": "896854956286558218",
-    "base": "895047755800641576"
+    "base": "895047755800641576",
+    "welcome": "886264368948445244"
 }
\ No newline at end of file
diff --git a/commands/InformeMoi.js b/commands/InformeMoi.js
index fe23e3c..33e6893 100644
--- a/commands/InformeMoi.js
+++ b/commands/InformeMoi.js
@@ -1,5 +1,7 @@
 
 const complotDB = require('../database/ComplotDB.js');
+const channelsID = require('../channels_conf.json');
+
 
 class InformeMoi{
     constructor(client){
@@ -24,9 +26,7 @@ class InformeMoi{
         return newSentence;
     }
 
-    getRandomComplot(){
-        let sentence = complotDB.getRandomSentence();
-
+    getRandomSentenceFilled(sentence){
         sentence = this.fillAllRandomObject(sentence, this.actorRE, complotDB.getRandomActor.bind(complotDB));
         sentence = this.fillAllRandomObject(sentence, this.actionRE, complotDB.getRandomAction.bind(complotDB));
         sentence = this.fillAllRandomObject(sentence, this.reasonRE, complotDB.getRandomReason.bind(complotDB));
@@ -36,6 +36,19 @@ class InformeMoi{
         return sentence;
     }
 
+    getRandomComplot(){
+        return this.getRandomSentenceFilled(complotDB.getRandomSentence());
+    }
+
+    getRandomWelcome(){
+        return this.getRandomSentenceFilled(complotDB.getRandomWelcome());
+    }
+
+    sayWelcome(userID){
+        let message = this.getRandomWelcome().replace('<new_user>', `<@${userID}>`);
+        this.client.channels.cache.get(channelsID.welcome).send(message);
+    }
+
     parse(message){
         if(this.shouldRespond(message)){
             message.reply(this.getRandomComplot());
diff --git a/database/ComplotDB.js b/database/ComplotDB.js
index 52701b9..1ed291e 100644
--- a/database/ComplotDB.js
+++ b/database/ComplotDB.js
@@ -8,6 +8,7 @@ class ComplotDB{
         this.reasons = [];
         this.sentences = [];
         this.sources = [];
+        this.welcome = [];
         this.readFile();
     }
 
@@ -48,6 +49,10 @@ class ComplotDB{
         return this.reasons;
     }
 
+    getAllWelcome(){
+        return this.welcome;
+    }
+
     #getRandomItem(liste){
         return liste[Math.floor(Math.random()*liste.length)];
     }
@@ -72,6 +77,10 @@ class ComplotDB{
         return this.#getRandomItem(this.sentences);
     }
 
+    getRandomWelcome(){
+        return this.#getRandomItem(this.welcome);
+    }
+
     #enumListString(list){
         let string = '';
         for(let i = 0; i < list.length; i++){
@@ -125,6 +134,7 @@ class ComplotDB{
             this.actions = json.actions ? json.actions : [];
             this.sources = json.sources ? json.sources : [];
             this.sentences = json.sentences ? json.sentences : [];
+            this.welcome = json.welcome ? json.welcome : [];
         });
         
     }
@@ -136,6 +146,7 @@ class ComplotDB{
             reasons: this.reasons,
             sources : this.sources,
             sentences : this.sentences,
+            welcome: this.welcome,
         });
 
         fs.writeFile(this.filename, data, (err)=>{
diff --git a/index.js b/index.js
index 69c7109..04de1aa 100644
--- a/index.js
+++ b/index.js
@@ -5,7 +5,7 @@ const CommandParser = require('./commands/CommandParser.js');
 const complotDB = require('./database/ComplotDB.js');
 const VoteParser = require('./vote/VoteParser');
 
-const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
+const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MEMBERS],
 	partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
 
 let parser = null;
@@ -38,4 +38,9 @@ client.on('messageReactionAdd', async (reaction, user) => {
 	}
 	voteParser.parse(reaction.message);
 });
+
+
+client.on('guildMemberAdd', member => {
+	parser.informeMoi.sayWelcome(member.user.id);
+});
 client.login(token);
diff --git a/vote/VoteParser.js b/vote/VoteParser.js
index ec3c8b3..cc55aca 100644
--- a/vote/VoteParser.js
+++ b/vote/VoteParser.js
@@ -1,4 +1,4 @@
-const channelsID = require('./channels_conf.json');
+const channelsID = require('../channels_conf.json');
 const complotDB = require('../database/ComplotDB.js');
 
 const CATEGORY = {
-- 
GitLab