From 111bd0211571c4fb1cc878e90ce81023ce7e62a3 Mon Sep 17 00:00:00 2001 From: Adrien Nunes <adrien.nunes@ensiie.fr> Date: Thu, 7 Oct 2021 18:41:58 +0200 Subject: [PATCH] Add vote system --- commands/CommandParser.js | 7 +-- commands/CommandVoteParser.js | 24 ++++++++++ database/complot_db.json | 2 +- index.js | 19 +++++++- vote/VoteParser.js | 83 +++++++++++++++++++++++++++++++++++ vote/channels_conf.json | 6 +++ 6 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 commands/CommandVoteParser.js create mode 100644 vote/VoteParser.js create mode 100644 vote/channels_conf.json diff --git a/commands/CommandParser.js b/commands/CommandParser.js index 690c753..edcb15a 100644 --- a/commands/CommandParser.js +++ b/commands/CommandParser.js @@ -1,19 +1,20 @@ -const { parse } = require('dotenv'); const InformeMoi = require('./InformeMoi.js'); const DatabaseCommand = require('./DatabaseCommand.js'); - +const CommandVoteParser = require('./CommandVoteParser.js'); class CommandParser{ - constructor(client){ + constructor(client, voteParser){ this.client = client; this.informeMoi = new InformeMoi(client); this.databaseCommand = new DatabaseCommand(client); + this.commandVoteParser = new CommandVoteParser(voteParser); } parse(message){ this.informeMoi.parse(message); this.databaseCommand.parse(message); + this.commandVoteParser.parse(message); } } diff --git a/commands/CommandVoteParser.js b/commands/CommandVoteParser.js new file mode 100644 index 0000000..3f3c5e5 --- /dev/null +++ b/commands/CommandVoteParser.js @@ -0,0 +1,24 @@ + + +class CommandVoteParser{ + constructor(voteParser){ + this.voteParser = voteParser; + this.command = "!complot score "; + this.channel = '892676980699455500'; /**TODO CONF */ + } + + parse(message){ + if(message.channelId === this.channel){ + const content = message.content.toLowerCase(); + if(content.startsWith(this.command)){ + const score = parseInt(content.slice(this.command.length)); + if(score > 1){ + this.voteParser.setScore(score); + message.reply(`Fait, score à ${score}`); + } + } + } + } +} + +module.exports = CommandVoteParser; \ No newline at end of file diff --git a/database/complot_db.json b/database/complot_db.json index b5c4eb6..f75ae6b 100644 --- a/database/complot_db.json +++ b/database/complot_db.json @@ -1 +1 @@ -{"actors":["Macron","TSP","Le BDE","Les Francs-Maçons","La CIA","Laurent Prével","Mouilleron","Rioboo","Forest","Jean Lassalle","Sylvain Duriff","Rocket","Titi","Nathalie Arthaud","Le fantôme d'Hitler","JeanMarIIE","Le Bar-C","Le C-19","Michel Drucker","Sardoche","i-TV","Ta mère","Diiese","Le CBDE"],"actions":["pirater Arise","vacciner la population","installer des antennes 5G","creer un nouveau variant du coronavirus","détruire Evry","bombarder TSP","faire des TikTok","comprendre les cours de Rioboo","vendre des informations aux Japonais","racheter MyPizza","annexer l'IMT-BS","radier le tout le BdE de l'AEIIE","interdire la vente d'alcool à Le Bar (c)","installer Windows sur les PCs de l'école","enlever les hentai du Baka","mettre Jean MarIIE à la tête du Bde"],"reasons":["gagner l'élection 2022","contrôler l'humanité","éradiquer l'humanité","instaurer une dictature","capter la 5G","gagner contre Pignôle au babyfoot","faire fermer l'ENSIIE","deban tigriz","dissoudre le Bakaclub","supprimer internet","gagner le carambar d'or","intégrer TSP","éradiquer tous les viieux","gagner la campagne BdE","prendre la place de tactac","rouvrir l'antenne de Strasbourg","obtenir tous les boucliers verts"]} \ No newline at end of file +{"actors":["Macron","TSP","Le BDE","Les Francs-Maçons","La CIA","Laurent Prével","Mouilleron","Rioboo","Forest","Jean Lassalle","Sylvain Duriff","Rocket","Titi","Nathalie Arthaud","Le fantôme d'Hitler","JeanMarIIE","Le Bar-C","Le C-19","Michel Drucker","Sardoche","i-TV","Ta mère","Diiese","Le CBDE"],"actions":["pirater Arise","vacciner la population","installer des antennes 5G","creer un nouveau variant du coronavirus","détruire Evry","bombarder TSP","faire des TikTok","comprendre les cours de Rioboo","vendre des informations aux Japonais","racheter MyPizza","annexer l'IMT-BS","radier le tout le BdE de l'AEIIE","interdire la vente d'alcool à Le Bar (c)","installer Windows sur les PCs de l'école","enlever les hentai du Baka","mettre Jean MarIIE à la tête du Bde"],"reasons":["gagner l'élection 2022","contrôler l'humanité","éradiquer l'humanité","instaurer une dictature","capter la 5G","gagner contre Pignôle au babyfoot","faire fermer l'ENSIIE","deban tigriz","dissoudre le Bakaclub","supprimer internet","gagner le carambar d'or","intégrer TSP","éradiquer tous les viieux","gagner la campagne BdE","prendre la place de tactac","rouvrir l'antenne de Strasbourg","obtenir tous les boucliers verts","passer master sur LoL"]} \ No newline at end of file diff --git a/index.js b/index.js index f9a6ea2..69c7109 100644 --- a/index.js +++ b/index.js @@ -3,13 +3,17 @@ const { token, clientId } = require('./config.json'); 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] }); +const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS], + partials: ['MESSAGE', 'CHANNEL', 'REACTION'] }); let parser = null; +let voteParser = null; client.once('ready', () => { - parser = new CommandParser(client); + voteParser = new VoteParser(client); + parser = new CommandParser(client, voteParser); console.log('Ready!'); }); @@ -23,4 +27,15 @@ client.on("messageCreate", (message)=>{ } }); +client.on('messageReactionAdd', async (reaction, user) => { + if (reaction.partial) { + try { + await reaction.fetch(); + } catch (error) { + console.error('Something went wrong when fetching the message:', error); + return; + } + } + voteParser.parse(reaction.message); +}); client.login(token); diff --git a/vote/VoteParser.js b/vote/VoteParser.js new file mode 100644 index 0000000..9175cef --- /dev/null +++ b/vote/VoteParser.js @@ -0,0 +1,83 @@ +const channelsID = require('./channels_conf.json'); +const complotDB = require('../database/ComplotDB.js'); + +const CATEGORY = { + ACTION : 0, + REASON : 1, + ACTOR : 2 +}; + +class VoteParser{ + + constructor(client){ + this.client = client; + this.upVote = 'ðŸ‘'; + this.score = 3; + this.categories = new Map([ + [channelsID.actor, CATEGORY.ACTOR], + [channelsID.reason, CATEGORY.REASON], + [channelsID.action, CATEGORY.ACTION] + ]); + + this.infoMessage = null; + this.channelBase = this.client.channels.cache.get(channelsID.base); + + } + + setScore(score) { + this.score = score; + } + + parse(message){ + if(this.categories.has(message.channelId)){ + const category = this.categories.get(message.channelId); + const reactions = message.reactions.resolve(this.upVote); + if(reactions && reactions.count >= score){ + this.add(message, category); + } + } + } + + async add(message, category){ + let content = message.content; + const char = content.charAt(0); + const reste = content.slice(1); + content = (category === CATEGORY.ACTOR) ? char.toUpperCase() + reste: char.toLowerCase() + reste; + this.addFormatedString(content, category); + message.delete(); + message.channel.send(`"${content}" ajouté ! \n Par <@${message.author.id}>`); + const dbString = complotDB.toString(); + if(!this.infoMessage){ + this.channelBase.send(dbString).then(message =>{ + this.infoMessage = message; + }); + }else{ + + await this.deleteAllMessageInfo(); + this.infoMessage.edit(dbString); + } + } + + + + addFormatedString(messageString, category){ + switch (category){ + case CATEGORY.ACTOR: complotDB.addActor(messageString); break; + case CATEGORY.ACTION: complotDB.addAction(messageString); break; + case CATEGORY.REASON: complotDB.addReason(messageString); break; + } + } + + async deleteAllMessageInfo(){ + + let messages = await this.channelBase.messages.fetch(); + let workers = []; + messages.forEach(m=>{ + workers.push(m.delete()); + }); + await Promise.all(workers); + } + +} + +module.exports = VoteParser; \ No newline at end of file diff --git a/vote/channels_conf.json b/vote/channels_conf.json new file mode 100644 index 0000000..5952254 --- /dev/null +++ b/vote/channels_conf.json @@ -0,0 +1,6 @@ +{ + "actor" : "895047791645179944", + "action" : "895047929713266709", + "reason" : "895047902123143210", + "base": "895047755800641576" +} \ No newline at end of file -- GitLab