0

I'm trying to use Bun to create an app that can interact with the Discord gateway. I tried the same code in node.js and it works properly, but in bun it only works sometimes. What I am doing is:

  1. Declare a paylod for identification.
  2. When the websocket connects, Discord sends the opcode 10 (hello)
  3. On the onmessage event, I properly receive the opcode 10, which is the Hello one, so I use the ping function to keep the connection alive and send the identification.
  4. The t in the payload is the gateway event name, and when the message is a payload in which the t is READY, that means that the app is working properly.

The problem is that sometimes it works and sometimes it doesn't. Sometimes printing t returns READY, sometimes it's not even printing out. I have no idea on why this happens. Here's the code:

const ws = new WebSocket("wss://gateway.discord.gg/?v=10&encoding=json");

const payload = {
  op: 2,
  d: {
    token: TOKEN,
    intents: 513,
    properties: {
      os: "linux",
      browser: "chrome",
      device: "chrome",
    },
  },
};
ws.on("open", (e) => {
  console.log("open");
});

ws.on("message", (e) => {
  const p = JSON.parse(e);
  const { t, d, op } = p;
  if (op == 10) {
    ping(d.heartbeat_interval);
    console.log("interval started");
    ws.send(JSON.stringify(payload));

  }
  console.log(op);
  console.log(t);
});

function ping(ms) {
  return setInterval(() => {
    ws.send(JSON.stringify({ op: 1, d: 251 }));
  }, ms);
}

Please keep in mind that I'm working with the bun environment! Thanks!

clapmemereview
  • 371
  • 1
  • 4
  • 14
  • 1
    The problem is still unclear. Is the problem that your client sometimes receives a Hello event and sometimes not? According to the [Discord docs](https://discord.com/developers/docs/topics/gateway) the order is: "1. App establishes a connection 2. Discord sends the app a Hello (opcode 10) event 3. Start the Heartbeat interval 4. App sends an Identify (opcode 2)" The order in your code is: "1. App establishes a connection 2. App sends an Identify (opcode 2) 3. Discord sends the app a Hello (opcode 10) event 4. Start the Heartbeat interval" – jabaa Oct 30 '22 at 13:55
  • Nono, the client always receives the hello event, but doesn't always receives the ready event – clapmemereview Oct 30 '22 at 13:59
  • Maybe the order of events is important and sending Identify before Hello was received causes it to be ignored. Without Identify no Ready is sent. – jabaa Oct 30 '22 at 14:06
  • You are right, I edited my question and my code, thank you, but sadly the problem still remains :( – clapmemereview Oct 30 '22 at 14:07
  • Have you tried this client: https://discord.js.org/#/docs/discord.js/main/general/welcome – jabaa Oct 30 '22 at 14:10
  • Yes, I tried, but I also want to try to implement my own, to know how these libraries work under the hood :) – clapmemereview Oct 30 '22 at 14:11
  • It's open source software: https://github.com/discordjs/discord.js/tree/main/packages/discord.js – jabaa Oct 30 '22 at 14:12

0 Answers0