Jaxel
Well-known member
I'm writing some scripts for a Discord bot for my website. Almost everything I've done with the bot has worked perfectly fine. Unfortunately, in order to CREATE things with the API, you have to go over their REST endpoints. People on the Discord Dev server have informed me that in order to get access to the REST endpoints on the Discord API, I must have my bot connect to the Discord gateway at least once over websockets. Now I know nothing about websockets; its not anything I've ever done or had to use. But I need to send a single websocket heartbeat to the Discord gateway.
Someone on the Dev server, gave me this code to run on NodeJS:
I would like to convert this code to PHP. Does anyone know how this would be done?
Someone on the Dev server, gave me this code to run on NodeJS:
Code:
"use strict";
var token="PUT YOUR TOKEN HERE",
WebSocket = require("ws"), ws = new WebSocket("wss://gateway.discord.gg/?encoding=json&v=6"), sequence = 0;
ws.onopen = function() {
return console.log("OPEN!")
}, ws.onerror = function(a) {
console.error(a), process.exit(1)
}, ws.onclose = function(a) {
console.error(a), process.exit(1)
}, ws.onmessage = function(a) {
try {
var b = JSON.parse(a.data);
if (0 === b.op) return;
console.log(b), sequence = b.s, 10 === b.op && (ws.send(JSON.stringify({
op: 2,
d: {
token: token,
properties: {
$browser: "b1nzy is a meme"
},
large_threshold: 50
}
})), setInterval(function() {
ws.send(JSON.stringify({
op: 1,
d: sequence
}))
}, b.d.heartbeat_interval))
} catch (a) {
console.error(a)
}
};
I would like to convert this code to PHP. Does anyone know how this would be done?