Discord.js: roles.add inside voiceStateUpdate event returns undefined

Created on 2 Sep 2020  Â·  3Comments  Â·  Source: discordjs/discord.js

Code:

bot.on("voiceStateUpdate", (oldState, newState) => {

  let role = newState.guild.roles.cache.find(role => role.id === "730938567337181266");

  if (newState.channelID === null) {
    newState.roles.remove(role);
  } else {
    newState.roles.add(role);
  }

});

Output:

 newState.roles.add(role);
                 ^

TypeError: Cannot read property 'add' of undefined
    at Client.<anonymous> (/root/podel/podel.js:36:18)
    at Client.emit (events.js:327:22)
    at VoiceStateUpdate.handle (/root/podel/node_modules/discord.js/src/client/actions/VoiceStateUpdate.js:40:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (/root/podel/node_modules/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (/root/podel/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/root/podel/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/root/podel/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/root/podel/node_modules/ws/lib/event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (/root/podel/node_modules/ws/lib/websocket.js:797:20)

It appears that fetching the role for this does work, but for some reason I can't add or remove any role inside the event, I can do so in other events though, such as on message and on guildMemberAdd. I can include the code for the two other events if that's needed.

I also tried newState.guild.member(newState) but I was very doubtful about it, rightly so, because it gave me a TypeError there.

Further Info:

  • discord.js version: 12.3.1
  • Node.js version: 12.18.3
  • Operating system: Debian 10

Relevant client options:

  • partials: none
  • gateway intents: none
  • other: none
question (please use Discord instead)

All 3 comments

A VoiceState instance does not have any roles. GuildMember does.

The issue tracker is only for bug reports and enhancement suggestions. If you have a question, please ask it in the Discord server instead of opening an issue – you will get redirected there anyway.

A VoiceState instance does not have any roles. GuildMember does.

The issue tracker is only for bug reports and enhancement suggestions. If you have a question, please ask it in the Discord server instead of opening an issue – you will get redirected there anyway.

So how did it work in v11.6.4?

So how did it work in v11.6.4?

Client#voiceStateUpdate emitted GuildMember instances in v11. In v12, it emits VoiceStates.

I will again direct you to the Discord server, please don't continue seeking support here.

Was this page helpful?
0 / 5 - 0 ratings