0

I'm new to javascript development and I'm testing something where it can read messages on a certain discord server. The code I have now works for like 3 seconds when the website opens. I'm not sure what I'm doing wrong but I know the heartbeat interval isn't working as it should. I'm getting bnack code op = 0 and so the case nmumber isn't switching to 10. I got the code from someone else on a different question and I changed a few things so that it would work on my end. Here's the current code.

var ws = new WebSocket('wss://gateway.discord.gg/?v=6&encoding=json');
var interval = 0;
var token = 'Discord_Token_here';

payload = {
    op: 2,
    d: {
        token: token,
        intents: 512,
        properties: {
            $os: 'Windows',
            $browser: 'Opera GX',
            $device: 'Opera GX'
        }
    }
};

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) {
        case 10:
            const { heartbeat_interval } = d;
            setInterval(() => {
                ws.send(JSON.stringify({ op: 2, d: null }));
            }, heartbeat_interval);
            break;
    }
    switch (t) {
        case 'MESSAGE_CREATE':
            console.log(d.author.username + ':' + d.content);
    }
})

I'm just not sure what the payload does or what op means. But so far, the case op doesn't match and I think I messed something up in the setting up portion between the wesocket setup and the variable's maybe being incorrect on something small I'm not seeing.

I've tried a few work arounds. Setting up a manual setInterval() but it just kept getting ignored.

const pingInterval = 1500;

function sendPing() {
    if (ws.readyState === WebSocket.OPEN)
        ws.send('ping');
}

setInterval(() => {
    sendPing();
}, pingInterval);

And also setting up an event listener for when it closed to reopen the websocket

ws.addEventListener('close', function close() {
    ws = new WebSocket('wss://gateway.discord.gg/?v=6&encoding=json');
})

^This one doesn't send an error. But still closes the websocket Is there something else I'm missing or is there a workaround I can use? I'm still new to javascript so please let me know if there is another function I can use that I'm not aware of.

Edit: Found part of a solution of the problem. The ws.send in the switch(op) statement, op:2 is replaced with op:1. Though it did increase the time open, the connection doesn't stay for minutes. Further testing required.

var ws = new WebSocket('wss://gateway.discord.gg/?v=6&encoding=json');
var interval = 0;
var token = 'Njc0ODc3NDY1NTg3NDgyNjI1.G9fl4S.JpDqclJYgzha_s1yyGNG_8pCurXaLlk7uoSehU';

payload = {
    op: 2,
    d: {
        token: token,
        intents: 512,
        properties: {
            $os: 'Windows',
            $browser: 'Opera GX',
            $device: 'Opera GX'
        }
    }
};

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) {
        case 10:
            const { heartbeat_interval } = d;
            setInterval(() => {
                ws.send(JSON.stringify({ op: 1, d: null }));
            }, heartbeat_interval);
            break;
    }
    switch (t) {
        case 'MESSAGE_CREATE':
            console.log(d.author.username + ':' + d.content);
    }
})
1
  • just a quick note, it looks like you exposed your credentials in your edited post. Might want to generate a new token just to be sure. Commented Jun 18 at 11:51

0