diff --git a/index.html b/index.html new file mode 100644 index 0000000..3cec2b3 --- /dev/null +++ b/index.html @@ -0,0 +1,22 @@ + + + + + + + + + +
+

Connecté !

+

Non connecté !

+ +
+ + diff --git a/index.js b/index.js index daf4678..fd3d09f 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,16 @@ const wsActions = { } function wsInit (data, channel, ws) { channel.ws_clients.push(ws) + wsSendState(channel, ws) +} +function wsSendState(channel, ws) { ws.send(JSON.stringify(channel.reactions)) } +function wsSendStateAll(channel) { + for (var index in channel.ws_clients) { + wsSendState(channel, channel.ws_clients[index]) + } +} function wsAddReaction (data, channel, ws) { if (!('reaction' in data)) { ws.send('{"error":"No reaction supplied", "action":"add"}') @@ -173,13 +181,17 @@ client.on('message', msg => { if(msg.content === '!educpop-reset') { educpopReset reply(channel) - } else if (msg.content === '!educpop-list') { + } + 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) { text += '\n' + reactions[index].prefix + index + ' : ' + reactions[index].description } msg.reply(text) } + else if (msg.content === '!educpop-web') { + msg.reply('?channel_id=' + msg.channel.id + '&web_token=' + channel.web_token) + } /* save and ignore own messages */ else if(msg.author.username === process.env.BOT_USERNAME){ if (msg.content.startsWith(':')) /* Save if its educpop summary */ @@ -204,6 +216,7 @@ client.on('message', msg => { /* Recap educ-pop state on discord. Delete action message and last educ-pop recap */ +/* This function refresh the display */ function reply (channel) { var text = '' for (var index in channel.reactions) { @@ -215,6 +228,7 @@ function reply (channel) { } if(channel.last_msg) channel.last_msg.delete() channel.discord_channel.send(text) + wsSendStateAll(channel) } diff --git a/main.js b/main.js new file mode 100644 index 0000000..d5d10b7 --- /dev/null +++ b/main.js @@ -0,0 +1,46 @@ +window.onload = function () { + var app = new Vue({ + el: '#educbot-status', + data: { + web_token: '', + channel_id: '', + ws: null, + ws_url: 'ws://localhost:8080', + reactions: {}, + connection: false, + }, + created: function () { + var url = new URL(location.href) + this.web_token = url.searchParams.get('web_token') + this.channel_id = url.searchParams.get('channel_id') + if (this.web_token == '' || this.channel_id == '') { + console.err('missing parameters to vue instance') + return + } + this.ws = new WebSocket(this.ws_url) + this.ws.onmessage = (data) => { + this.reactions = JSON.parse(data.data) + console.log(this.reactions) + } + this.ws.onopen = () => { + this.sendWs('init') + this.connection = true + } + this.ws.onclose = () => { + this.connection = false + } + this.ws.onerror = () => { + this.connection = false + } + }, + methods: { + sendWs: function (action) { + this.ws.send(JSON.stringify({ + 'action': action, + 'web_token': this.web_token, + 'channel': this.channel_id, + })) + } + } + }) +}