'Error in discord.js "guildMemberUpdate" event

client.on("guildMemberUpdate", async (oldMember, newMember) => {
  if (oldMember.partial) await oldMember.fetch();
  if (newMember.partial) await newMember.fetch();
  let rolesold = oldMember.roles.cache;
  let rolesnew = newMember.roles.cache;
  if (rolesold.size != rolesnew.size) {
    let rolesguild = newMember.guild.roles.cache;
    rolesguild.sort((a, b) => (a.rawPosition < b.rawPosition ? 1 : -1));
    rolesguild.delete(rolesguild.lastKey());
    let maproles = new Map();
    rolesguild.forEach((role) => {
      if (role.name.includes("categorie")) {
        maproles.set(role.id, []);
      }
    });
    let i = -1;
    rolesguild.forEach((role) => {
      if (maproles.has(role.id)) {
        i++;
      } else {
        let temp = maproles.get(Array.from(maproles.keys())[i]);
        temp.push(role.id);
        maproles.set(Array.from(maproles.keys())[i], temp);
      }
    });

    maproles.forEach((catroles, catrole) => {
      needscat = false;
      catroles.forEach((role) => {
        if (rolesnew.some((r) => r.id === role)) needscat = true;
      });

      if (needscat) {
        if (!rolesnew.some((r) => r.id === catrole))
          newMember.roles.add(catrole, "Categorie missed!");
      } else {
        if (rolesnew.some((r) => r.id === catrole))
          newMember.roles.remove(catrole, "Categorie needed!");
      }

      console.log(catrole + needscat);
    });
  }
});

That's my code. I want my bot to add a categorie role if the user has a role underneeth the categorie. The bot also has to remove a categorie role if the user has no role underneeth the categorie. This program works the first time I update a role on a member but the second time there is an error which I dont find on the internet.

C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:36
    return this.guild.roles.cache.filter(role => this.member._roles.includes(role.id)).set(everyone.id, everyone);
                                                                                                    ^

TypeError: Cannot read properties of undefined (reading 'id')
    at GuildMemberRoleManager.get cache [as cache] (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:36:101)
    at Client.<anonymous> (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\main.js:506:34)
    at Client.emit (node:events:520:28)
    at GuildMemberUpdateAction.handle (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\client\actions\GuildMemberUpdate.js:29:74)
    at Object.module.exports [as GUILD_MEMBER_UPDATE] (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\client\websocket\handlers\GUILD_MEMBER_UPDATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\paulb\VisualStudioCode-workspace\rp-discord-bot\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:520:28)

I think it is also an error in the discordjs code and not in my code. Does anyone know a solution?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source