Connecté !
Non connecté !
{{message}}
- - {{value.prefix}} {{person}} diff --git a/index.js b/index.js index 98f0ef0..88d1d00 100644 --- a/index.js +++ b/index.js @@ -17,12 +17,19 @@ const defaultReactions = JSON.parse(fs.readFileSync('defaultReactions.json', 'ut /* The html page will render data passed in WS */ const WebSocket = require('ws') -const wss = new WebSocket.Server({ port: process.env.WS_PORT }) +const wss = new WebSocket.Server({ port: process.env.WS_PORT || '8080' }) wss.on('connection', ws => { ws.isAlive = true; ws.on('pong', heartbeat); ws.on('message', message => { - const data = JSON.parse(message) + var data; + try { + data = JSON.parse(message) + } catch (e) { + console.log(e) + return + } + if (!('channel' in data && 'action' in data && 'web_token' in data)) { ws.send('{"error":"invalid request"}') return @@ -199,7 +206,7 @@ client.on('message', msg => { msg.reply(text) } else if (msg.content === '!educpop-web') { - msg.reply('https://educbot.jean-cloud.net?channel_id=' + msg.channel.id + '&web_token=' + channel.web_token) + msg.reply('https://educbot.jean-cloud.net?channel_id=' + msg.channel.id + '&web_token=' + channel.web_token + '&ws_port=' + (process.env.EXT_WS_PORT || process.env.WS_PORT || '8080') } /* save and ignore own messages */ else if(msg.author.username === process.env.BOT_USERNAME){ diff --git a/main.js b/main.js index af025f3..9e54d64 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,11 @@ window.onload = function () { var app = new Vue({ - el: '#educbot-status', + el: '#educbotstatus', data: { web_token: '', channel_id: '', ws: null, - ws_url: 'ws://localhost:8080', + ws_url: 'wss://educbot.jean-cloud.net', reactions: {}, connection: false, message: '', @@ -14,17 +14,19 @@ window.onload = 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') + this.ws_port = url.searchParams.get('ws_port') || '8080' + if (this.web_token == '' || this.channel_id == '' || this.ws_port == '') { + this.message = 'Erreur ! L’adresse n’est pas valide :( Redemandez-la à Educ-Bot avec la commande « !educpop-web ».' return } - this.ws = new WebSocket(this.ws_url) + window.WebSocket = window.WebSocket || window.MozWebSocket + this.ws = new WebSocket(this.ws_url + ':' + this.ws_port) this.ws.onmessage = (data) => { var reactions = JSON.parse(data.data) if ('error' in reactions) { this.message = 'Erreur ! ' if (reactions.error === "bad channel") { - this.message += 'Les identifiants sont incorrect ! Redemandez les à Educ-Bot avec la commande "!educpop-web"' + this.message += 'Les identifiants sont incorrect ! Redemandez les à Educ-Bot avec la commande « !educpop-web ».' } return } @@ -36,9 +38,11 @@ window.onload = function () { } this.ws.onclose = () => { this.connection = false + this.message = 'Erreur ! La connection avec le bot a été perdue :( Essayez d’actualiser la page.' } this.ws.onerror = () => { this.connection = false + this.message = 'Erreur ! La connection avec le bot a été perdue :( Essayez d’actualiser la page.' } }, methods: { diff --git a/package.json b/package.json index 150fc3b..9d58bf9 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ "author": "", "license": "ISC", "dependencies": { - "discord.js": "^12.1.1" + "discord.js": "^12.1.1", + "dotenv": "^8.2.0", + "lodash.clonedeep": "^4.5.0", + "randomstring": "^1.1.5" } }