educ-bot/main.js
2020-04-06 17:17:12 +02:00

59 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

window.onload = function () {
var app = new Vue({
el: '#educbotstatus',
data: {
web_token: '',
channel_id: '',
ws: null,
ws_url: 'wss://educbot.jean-cloud.net',
reactions: {},
connection: false,
message: '',
},
created: function () {
var url = new URL(location.href)
this.web_token = url.searchParams.get('web_token')
this.channel_id = url.searchParams.get('channel_id')
this.ws_port = url.searchParams.get('ws_port') || '8080'
if (this.web_token == '' || this.channel_id == '' || this.ws_port == '') {
this.message = 'Erreur ! Ladresse nest pas valide :( Redemandez-la à Educ-Bot avec la commande « !educpop-web ».'
return
}
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 ».'
}
return
}
this.reactions = reactions
}
this.ws.onopen = () => {
this.sendWs('init')
this.connection = true
}
this.ws.onclose = () => {
this.connection = false
this.message = 'Erreur ! La connection avec le bot a été perdue :( Essayez dactualiser la page.'
}
this.ws.onerror = () => {
this.connection = false
this.message = 'Erreur ! La connection avec le bot a été perdue :( Essayez dactualiser la page.'
}
},
methods: {
sendWs: function (action) {
this.ws.send(JSON.stringify({
'action': action,
'web_token': this.web_token,
'channel': this.channel_id,
}))
}
}
})
}