59 lines
2.3 KiB
JavaScript
59 lines
2.3 KiB
JavaScript
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 ! L’adresse n’est 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 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: {
|
||
sendWs: function (action) {
|
||
this.ws.send(JSON.stringify({
|
||
'action': action,
|
||
'web_token': this.web_token,
|
||
'channel': this.channel_id,
|
||
}))
|
||
}
|
||
}
|
||
})
|
||
}
|