now channel independant
This commit is contained in:
parent
af7e3d1557
commit
e9c60956b6
34
defaultReactions.json
Normal file
34
defaultReactions.json
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"oui": {
|
||||||
|
"prefix": ":thumbsup:",
|
||||||
|
"description": "Je suis d’accord"
|
||||||
|
},
|
||||||
|
"non": {
|
||||||
|
"prefix": ":thumbsdown:",
|
||||||
|
"description": "Je ne suis pas d’accord"
|
||||||
|
},
|
||||||
|
"parole": {
|
||||||
|
"prefix": ":raised_hand:",
|
||||||
|
"description": "Je veux parler"
|
||||||
|
},
|
||||||
|
"réponse": {
|
||||||
|
"prefix": ":raised_hands:",
|
||||||
|
"description": "Je veux répondre rapidement"
|
||||||
|
},
|
||||||
|
"suffit": {
|
||||||
|
"prefix": ":octagonal_sign:",
|
||||||
|
"description": "On tourne en rond"
|
||||||
|
},
|
||||||
|
"écoute": {
|
||||||
|
"prefix": ":hear_no_evil:",
|
||||||
|
"description": "On ne s’écoute pas"
|
||||||
|
},
|
||||||
|
"love": {
|
||||||
|
"prefix": ":heart_eyes:",
|
||||||
|
"description": "J’adore"
|
||||||
|
},
|
||||||
|
"dab": {
|
||||||
|
"prefix": ":dab:",
|
||||||
|
"description": "Dab"
|
||||||
|
}
|
||||||
|
}
|
275
educ.js
275
educ.js
@ -1,134 +1,227 @@
|
|||||||
const Discord = require('discord.js');
|
/* Run dotenv */
|
||||||
const client = new Discord.Client();
|
require('dotenv').config();
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
client.on('ready', () => {
|
/* Deep clone of objects */
|
||||||
console.log('Logged in as ${client.user.tag}');
|
const clonedeep = require('lodash.clonedeep')
|
||||||
});
|
|
||||||
|
|
||||||
// l'ID du channel de test (test-bot) pour plus tard
|
/* Generate random string for token */
|
||||||
// var testchan_id = '691952512332202064';
|
const randomstring = require("randomstring");
|
||||||
|
|
||||||
|
/* Liste des réactions possibles par défaut et de leurs représentation actuelle */
|
||||||
|
const defaultReactions = JSON.parse(fs.readFileSync('defaultReactions.json', 'utf8'));
|
||||||
|
|
||||||
/* Liste des réactions possibles et de leurs représentants actuel */
|
|
||||||
var reactions = {
|
/* The html page will render data passed in WS */
|
||||||
'oui': {
|
const WebSocket = require('ws')
|
||||||
'prefix': ':thumbsup:',
|
const wss = new WebSocket.Server({ port: process.env.WS_PORT })
|
||||||
'description': 'Je suis d’accord',
|
wss.on('connection', ws => {
|
||||||
},
|
ws.isAlive = true;
|
||||||
'non': {
|
ws.on('pong', heartbeat);
|
||||||
'prefix': ':thumbsdown:',
|
ws.on('message', message => {
|
||||||
'description': 'Je ne suis pas d’accord',
|
const data = JSON.parse(message)
|
||||||
},
|
if (!('channel' in data && 'action' in data && 'web_token' in data)) {
|
||||||
'parole': {
|
ws.send('{"error":"invalid request"}')
|
||||||
'prefix': ':raised_hand:',
|
return
|
||||||
'description': 'Je veux parler',
|
}
|
||||||
},
|
else if (!(data['action'] in wsActions)) {
|
||||||
'réponse': {
|
ws.send('{"error":"bad action"}')
|
||||||
'prefix': ':raised_hands:',
|
return
|
||||||
'description': 'Je veux répondre rapidement',
|
}
|
||||||
},
|
else if (!(data['channel'] in channels && channels[data['channel']].web_token === data['web_token'])) {
|
||||||
'suffit': {
|
ws.send('{"error": "bad channel"}')
|
||||||
'prefix': ':octagonal_sign:',
|
return
|
||||||
'description': 'On tourne en rond',
|
}
|
||||||
},
|
else {
|
||||||
'écoute': {
|
wsActions[data['action']](data, channels[data['channel']], ws)
|
||||||
'prefix': ':hear_no_evil:',
|
}
|
||||||
'description': 'On ne s’écoute pas',
|
})
|
||||||
},
|
})
|
||||||
'love': {
|
function heartbeat() {
|
||||||
'prefix': ':heart_eyes:',
|
this.isAlive = true;
|
||||||
'description': 'J’adore',
|
}
|
||||||
},
|
const interval = setInterval(function ping() {
|
||||||
'dab': {
|
wss.clients.forEach(function (ws) {
|
||||||
'prefix': ':dab:',
|
if (ws.isAlive === false) return ws.terminate();
|
||||||
'description': 'Dab',
|
|
||||||
},
|
ws.isAlive = false;
|
||||||
};
|
ws.ping(()=>{});
|
||||||
for (var index in reactions) {
|
});
|
||||||
reactions[index].people = []
|
}, 30000);
|
||||||
|
|
||||||
|
wss.on('close', function close() {
|
||||||
|
clearInterval(interval);
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO penser à prévenir le web quand un type de réaction est ajoutée/supprimée
|
||||||
|
const wsActions = {
|
||||||
|
'init': wsInit,
|
||||||
|
'add_reaction': wsAddReaction,
|
||||||
|
'remove': wsRemoveReaction,
|
||||||
|
'reset': wsReset,
|
||||||
|
}
|
||||||
|
function wsInit (data, channel, ws) {
|
||||||
|
channel.ws_clients.push(ws)
|
||||||
|
ws.send(JSON.stringify(channel.reactions))
|
||||||
|
}
|
||||||
|
function wsAddReaction (data, channel, ws) {
|
||||||
|
if (!('reaction' in data)) {
|
||||||
|
ws.send('{"error":"No reaction supplied", "action":"add"}')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function wsRemoveReaction (data, channel, ws) {
|
||||||
|
if (!('reaction' in data)) {
|
||||||
|
ws.send('{"error":"No reaction supplied", "action":"add"}')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function wsReset (data, channel, ws) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Liste des channels où on peut lire avec le dernier message que l’on y a envoyé */
|
/* Liste des channels où on peut lire avec le dernier message que l’on y a envoyé */
|
||||||
/* 1282482389371398137183: <msg> */
|
|
||||||
var channels = {}
|
var channels = {}
|
||||||
|
function educpopReset (channel) {
|
||||||
var last_post = null; /* On se souvient du derier post que ce bot a envoyé pour le virer dès que possible */
|
for (var index in channel.reactions) {
|
||||||
|
channel.reactions[index].people = []
|
||||||
client.on('message', msg => {
|
|
||||||
if(msg.content === '!educpop-reset') { //TODO room parametrized
|
|
||||||
for (var index in reactions) {
|
|
||||||
reactions[index].people = []
|
|
||||||
}
|
|
||||||
reply(msg)
|
|
||||||
}
|
}
|
||||||
else if (msg.content === '!educpop-enable') {
|
}
|
||||||
var id = msg.channel.id
|
|
||||||
if (id in channels) {
|
function educpopAddChannel (discordChannel) {
|
||||||
|
channels [discordChannel.id] = {
|
||||||
|
'last_msg': null, /* On se souvient du dernier post que ce bot a envoyé pour le virer dès que possible */
|
||||||
|
'discord_channel': discordChannel,
|
||||||
|
'ws_clients': [],
|
||||||
|
'web_token': randomstring.generate(), /* web auth */
|
||||||
|
'reactions': Object.assign({}, clonedeep(defaultReactions)),
|
||||||
|
'pause': false,
|
||||||
|
}
|
||||||
|
educpopReset(channels[discordChannel.id])
|
||||||
|
}
|
||||||
|
|
||||||
|
function educpopDelChannel (channel) {
|
||||||
|
delete channels[channel.discord_channel.id]
|
||||||
|
}
|
||||||
|
|
||||||
|
function educpopPause (channel) {
|
||||||
|
channel.pause = true
|
||||||
|
}
|
||||||
|
// TODO wait for database before using this
|
||||||
|
function educpopAddReaction (channel, word, prefix, description) {
|
||||||
|
channels[channelId].reactions[word] = {'prefix': prefix, 'description': description, 'people':[]}
|
||||||
|
}
|
||||||
|
function educpopDelReaction (channelId, word) {
|
||||||
|
delete channels[channelId].reactions[word]
|
||||||
|
}
|
||||||
|
|
||||||
|
function educpopAddPerson (channel, username, reaction) {
|
||||||
|
var liste = channel.reactions[reaction].people
|
||||||
|
if (liste.indexOf(username) < 0) {
|
||||||
|
liste.push(username)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function educpopDelPerson (channel, username, reaction) {
|
||||||
|
var liste = channel.reactions[reaction].people
|
||||||
|
var index = liste.indexOf(username)
|
||||||
|
if (index >= 0) {
|
||||||
|
liste.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Connexion à discord */
|
||||||
|
const Discord = require('discord.js');
|
||||||
|
const client = new Discord.Client();
|
||||||
|
client.on('ready', () => {
|
||||||
|
console.log('Connected to Discord as ' + client.user.tag);
|
||||||
|
});
|
||||||
|
/* Discord message center */
|
||||||
|
client.on('message', msg => {
|
||||||
|
if (msg.content === '!educpop-enable') {
|
||||||
|
if (msg.channel.id in channels) {
|
||||||
msg.reply('J’écoute déjà ce canal texte.')
|
msg.reply('J’écoute déjà ce canal texte.')
|
||||||
} else {
|
} else {
|
||||||
channels[id] = null
|
educpopAddChannel(msg.channel)
|
||||||
msg.reply('J’écoute maintenant ce canal texte.')
|
msg.reply('J’écoute maintenant ce canal texte.')
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
else if (msg.content === '!educpop-disable') {
|
if (msg.content === '!educpop-disable') {
|
||||||
var id = msg.channel.id
|
var id = msg.channel.id
|
||||||
if (id in channels) {
|
if (id in channels) {
|
||||||
delete channels[id]
|
educpopDelChannel(id)
|
||||||
msg.reply('Je n’écoute plus ce canal texte.')
|
msg.reply('Je n’écoute plus ce canal texte.')
|
||||||
} else {
|
} else {
|
||||||
msg.reply('Je n’écoutais pas ce canal texte :o')
|
msg.reply('Je n’écoutais pas ce canal texte :o')
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
else if (msg.content === '!educpop-help') {
|
if (msg.content === '!educpop-help') {
|
||||||
var text = 'Tapez simplement le mot-clé ci-dessous pour être comptabilisé. Tapez un - immédiatement suivi du mot-clé pour être retiré du compte : -oui !'
|
msg.reply('Voilà comment m’utiliser. Taper :\n!educpop-enable pour activer le bot sur ce salon ;\n!educpop-disable pour le désactiver ;\n!educpop-list pour voir la liste des réactions possibles ;\n!educpop-reset pour remettre les compteurs à zéro.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Listen only to enabled channels */
|
||||||
|
if(!(msg.channel.id in channels)) { return }
|
||||||
|
var channel = channels[msg.channel.id]
|
||||||
|
var reactions = channel.reactions
|
||||||
|
for( index in channels) {
|
||||||
|
console.log(index)
|
||||||
|
console.log(channels[index].reactions)
|
||||||
|
}
|
||||||
|
console.log(true)
|
||||||
|
|
||||||
|
if(msg.content === '!educpop-reset') {
|
||||||
|
educpopReset
|
||||||
|
reply(channel)
|
||||||
|
} else if (msg.content === '!educpop-list') {
|
||||||
|
var text = 'Tapez simplement le mot-clé ci-dessous pour être comptabilisé. Tapez un - immédiatement suivi du mot-clé pour être retiré du compte : -oui par exemple !'
|
||||||
for (var index in reactions) {
|
for (var index in reactions) {
|
||||||
text += '\n' + reactions[index].prefix + index + ' : ' + reactions[index].description
|
text += '\n' + reactions[index].prefix + index + ' : ' + reactions[index].description
|
||||||
}
|
}
|
||||||
msg.author.send(text)
|
msg.reply(text)
|
||||||
msg.delete()
|
|
||||||
}
|
|
||||||
/* Listen to educ pop stuff only in authorized channels */
|
|
||||||
else if(!(msg.channel.id in channels)) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
/* save and ignore own messages */
|
/* save and ignore own messages */
|
||||||
else if(msg.author.username === 'bod-educ-pop'){ // TODO only log it if answer to educpop
|
else if(msg.author.username === process.env.BOT_USERNAME){
|
||||||
channels[msg.channel.id] = msg;
|
if (msg.content.startsWith(':')) /* Save if its educpop summary */
|
||||||
|
channel.last_msg = msg;
|
||||||
}
|
}
|
||||||
/* Educ pop stuff */
|
/* Educ pop stuff */
|
||||||
else if(msg.content.startsWith('-')) {
|
else if(msg.content.startsWith('-')) {
|
||||||
var content = msg.content.slice(1)
|
const content = msg.content.slice(1)
|
||||||
if (content.toLowerCase() in reactions) {
|
if (content.toLowerCase() in reactions) {
|
||||||
var reaction = reactions[content].people
|
educpopAddPerson(channel, msg.author.username, content.toLowerCase())
|
||||||
var index = reaction.indexOf(msg.author.username)
|
msg.delete()
|
||||||
if (index >= 0) {
|
reply(channel)
|
||||||
reaction.splice(index, 1)
|
|
||||||
}
|
|
||||||
reply(msg)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (msg.content.toLowerCase() in reactions) {
|
else if (msg.content.toLowerCase() in reactions) {
|
||||||
var reaction = reactions[msg.content].people
|
var reaction = msg.content.toLowerCase()
|
||||||
if (reaction.indexOf(msg.author.username) < 0) {
|
educpopAddPerson(channel, msg.author.username, msg.content.toLowerCase())
|
||||||
reaction.push(msg.author.username)
|
msg.delete()
|
||||||
}
|
reply(channel)
|
||||||
reply(msg)
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function reply (msg) {
|
/* Recap educ-pop state on discord. Delete action message and last educ-pop recap */
|
||||||
|
function reply (channel) {
|
||||||
var text = ''
|
var text = ''
|
||||||
for (var index in reactions) {
|
for (var index in channel.reactions) {
|
||||||
if (reactions[index].people.length > 0)
|
if (channel.reactions[index].people.length > 0)
|
||||||
text += '\n' + reactions[index].prefix + String(reactions[index].people)
|
text += '\n' + channel.reactions[index].prefix + String(channel.reactions[index].people)
|
||||||
}
|
}
|
||||||
if (text === '') {
|
if (text === '') {
|
||||||
text = 'Personne ne s’est manifesté :/'
|
text = 'Personne ne s’est manifesté :/'
|
||||||
}
|
}
|
||||||
|
if(channel.last_msg) channel.last_msg.delete()
|
||||||
if(channels[msg.channel.id]) channels[msg.channel.id].delete()
|
channel.discord_channel.send(text)
|
||||||
msg.reply(text)
|
|
||||||
msg.delete()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client.login('NjkxOTUzMDQzMDcxMzAzNzIy.Xnnhng.pYBFO2ogooVs2AyYz8Pk6AKhMoo');
|
|
||||||
|
/* Actual discord login */
|
||||||
|
client.login(process.env.DISCORD_TOKEN);
|
||||||
|
Loading…
Reference in New Issue
Block a user