Home > front end >  Error [BitFieldInvalid]: Invalid bitfield flag or number: undefined. when coding discord bot
Error [BitFieldInvalid]: Invalid bitfield flag or number: undefined. when coding discord bot

Time:10-21

Got this code from another post and I'm trying to use it to create my first bot, I want it to announce using @everyone when someone enters a voice channe. No idea about the source or the error, any help?

Error: RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined.

  const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
  intents: [
    GatewayIntentBits.GUILDS,
    GatewayIntentBits.GUILD_MESSAGES,
    GatewayIntentBits.GUILD_VOICE_STATES,
  ],
});

client.on('voiceStateUpdate', async (oldState, newState) => {
  const VOICE_ID = 'xxxxxx';
  const LOG_CHANNEL_ID = 'xxxxxx';
  // if there is no newState channel, the user has just left a channel
  const USER_LEFT = !newState.channel;
  // if there is no oldState channel, the user has just joined a channel
  const USER_JOINED = !oldState.channel;
  // if there are oldState and newState channels, but the IDs are different,
  // user has just switched voice channels
  const USER_SWITCHED = newState.channel?.id !== oldState.channel?.id;

  // if a user has just left a channel, stop executing the code 
  if (USER_LEFT)
    return;

  if (
    // if a user has just joined or switched to a voice channel
    (USER_JOINED || USER_SWITCHED) &&
    // and the voice channel is the same
    newState.channel.id === VOICE_ID
  ) {
    try {
      let logChannel = await client.channels.fetch(LOG_CHANNEL_ID);

      logChannel.send(
        `${newState.member.displayName} joined the channel`,
      );
    } catch (err) {
      console.error('❌ Error finding the log channel, check the error below');
      console.error(err);
      return;
    }
  }
});

Error message

CodePudding user response:

Replace

GatewayIntentBits.GUILDS,
GatewayIntentBits.GUILD_MESSAGES,
GatewayIntentBits.GUILD_VOICE_STATES,

with

GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,

CodePudding user response:

You defined the intents like you would in v13 (SCREAMING_SNAKE_CASE) You have to change it to PascalCase:
GatewayIntentBits.GUILDS -> GatewayIntentBits.Guilds
GatewayIntentBits.GUILD_MESSAGES -> GatewayIntentBits.GuildMessages
GatewayIntentBits.GUILD_VOICE_STATES -> GatewayIntentBits.GuildVoiceStates

  • Related