'receive discord messages in pure, browser-based javascript?
(NOTE: this first section kinda just gives context, not too relevant to the message-receive thing, but again, it sets up some context that might help)
So, I know i need to use a WebSocket to connect to it somehow. After a little bit of reverse engineering, I noticed that I need to start a WebSocket connection with wss://gateway.discord.gg
.
Now, I made a crappy bot login function (though I'm not entirely sure how WebSockets work) and it looks like this:
function login_bot() {
let gateway_url = "https://discordapp.com/api/v9/gateway/bot";
let tok = prompt('bot token: ');
let request = new XMLHttpRequest();
request.withCredentials = true;
request.open("GET", gateway_url);
request.setRequestHeader("authorization", tok);
request.setRequestHeader("accept", "/");
request.setRequestHeader("authority", "discordapp.com");
request.setRequestHeader("content-type", "application/json");
let socket = new WebSocket("wss://gateway.discord.gg");
socket.onopen = function(e) {
alert("[open] Connection established");
alert("Sending to server");
socket.send("I stole this request code xd");
};
}
With this, I got nothing; no 403 not permitted response, no 404 not found response, etc. (which probably doesnt make sense in this context? again, im clueless when it comes to websockets).
After this, someone in the Discord-API server let me know that if the connection comes from the web, you cannot log in (the api denies the websocket connection). So, what if i don't want to log in?
I can't figure out how to even just simply log messages while already logged into the discord domain.
I tried reverse engineering the discord.py wrapper, although that's just more WebSockets.
So, I so far know that I:
- need to use websockets
- get message content somehow & then console.log 'em
So, does anyone know how i would get the message content with a websocket connection (the one that discord.com/login gives u) already established to the discord server?
Again, apologies if this question isn't allowed here.
Thanks, 1305.
Solution 1:[1]
Try this. Replace YOUR_TOKEN
with your Discord token.
var ws = new WebSocket("wss://gateway.discord.gg/?v=6&encoding=json");
var interval = 0;
var token = "YOUR_DISCORD_TOKEN"
payload = {
op: 2,
d: {
token: token,
intents: 512,
properties: {
$os: "linux",
$browser: "chrome",
$device: "chrome",
},
},
};
ws.addEventListener("open", function open(x) {
ws.send(JSON.stringify(payload));
});
ws.addEventListener("message", function incoming(data) {
var x = data.data;
var payload = JSON.parse(x);
const { t, event, op, d } = payload;
switch (op) {
// OPCODE 10 GIVES the HEARTBEAT INTERVAL, SO YOU CAN KEEP THE CONNECTION ALIVE
case 10:
const { heartbeat_interval } = d;
setInterval(() => {
ws.send(JSON.stringify({ op: 2, d: null }));
}, heartbeat_interval);
break;
}
switch (t) {
// IF MESSAGE IS CREATED, IT WILL LOG IN THE CONSOLE
case "MESSAGE_CREATE":
console.log(d.author.username + ": " + d.content);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | marc_s |