discordjs / RPC

A simple RPC client for Discord
MIT License
474 stars 192 forks source link

Events fire twice #154

Closed Hacksore closed 2 years ago

Hacksore commented 2 years ago

When subscribing to any event I see them firing twice.

Steps to produce:

Code for reference/reproduction

const RPC = require("discord-rpc");
const client = new RPC.Client({ transport: "ipc" });
const clientId = "redacted";
const accessToken = "redacted";

client.on("ready", () => {
  console.log("Authed for user", client.user.username);
  client.subscribe("VOICE_CHANNEL_SELECT");
});

client.on("VOICE_CHANNEL_SELECT", event => {
  console.log(event.channel_id);
  client.subscribe("SPEAKING_START", { channel_id: event.channel_id });
});

client.on("SPEAKING_START", event => {
  console.log("SPEAKING_START", event);
});

// Log in to RPC with access token
client.login({
  clientId,
  scopes: ["rpc"],
  prompt: "none",
  accessToken
});

What I see in the console: image

Hacksore commented 2 years ago

After looking at this again I think it's a rather large PEBCAK. The VOICE_CHANNEL_SELECT must have fired twice, thus creating two listeners.

Mystery solved 😅