简体   繁体   中英

how to fix the Invalid Bitfield discord.js bot crash Problem

the code says invalid bitfield and has no idea how to fix it, the intents are fine I think and I think everything should be normal. i have tried to combine code from a bot from a snippet someone sent, maybe thats why this doesnt work

code:

const { Client, Intents, Message} = require('discord.js');
const util = require('minecraft-server-util');
const {EmbedBuilder} = require('discord.js');
const options = {
    timeout: 1000 * 5, 
    enableSRV: true 
};
const prefix = "!mcstatus"; 
const client = new Client({
    intents: [
      "Guilds",
      "GuildMessages",
      "MessageContent"
    ]
});
client.on('ready', () => {
    console.log('bot started');
    
    client.user.setPresence({ activities: [{ name: `${server_ip}`, type: 'WATCHING' }], status: 'active' });
});
const server_ip = "mc.hypixel.net"; 
const server_port = 25565; 
client.on('messageCreate', (message) => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    if(message.content.startsWith(prefix)){
          util.status(server_ip, server_port, options)
    .then((result) => {
        const embed = new EmbedBuilder()
        let args = message.content.split(" "); // Splitting with a blank space.

        args = args.slice(1); // Remove the first argument which would be the command itself.

        const server_port = 25565; // Leave the default port for MC servers.
        
        if (args.length < 1) return message.reply("Didn't provide arguments.")
// Run a forEach loop for every argument.
        args.forEach((arg) => {
          util
            .status(arg, server_port, options)
            .then((result) => {
              const embed = new EmbedBuilder()
                .setColor("#FF0000")
                .setTitle("Results")
                .setDescription(
                  `This will show the status and info about the minecraft server \n **Server ip:** ${arg} \n **Server port:** ${server_port}`
                )
                .addFields(
                  { name: "Server Version", value: `${result.version.name}` },
                  {
                    name: "Server Protocol Version",
                    value: `${result.version.protocol}`,
                  },
                  { name: "Players Online", value: `${result.players.online}` },
                  { name: "Max Players", value: `${result.players.max}` },
                  {
                    name: "MOTD (May Not Display Accurately)",
                    value: `${result.motd.clean}`,
                  },
                  { name: "Latency", value: `${result.roundTripLatency}` }
                )
                .setTimestamp();

              message.channel.send({ embeds: [embed] });
            })
            .catch((error) => {
              const embed = new EmbedBuilder()
                .setColor("#808080")
                .setTitle("Error")
                .setDescription(
                  `${arg} was unable to be pinged or you mis-typed the info`
                )
                .setTimestamp();
              message.channel.send({ embeds: [embed] });
            });
        });
    message.channel.send({embeds: [embed]})
    
    })}});

log i have tried to google things but i couldnt find anything that helps my thing


RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number: Guilds.
    at Intents.resolve (C:\Users\jason\Desktop\discord minecraft bot test 2\node_modules\discord.js\src\util\BitField.js:152:11)
    at C:\Users\jason\Desktop\discord minecraft bot test 2\node_modules\discord.js\src\util\BitField.js:147:54
    at Array.map (<anonymous>)
    at Intents.resolve (C:\Users\jason\Desktop\discord minecraft bot test 2\node_modules\discord.js\src\util\BitField.js:147:40)
    at Client._validateOptions (C:\Users\jason\Desktop\discord minecraft bot test 2\node_modules\discord.js\src\client\Client.js:550:33)
    at new Client (C:\Users\jason\Desktop\discord minecraft bot test 2\node_modules\discord.js\src\client\Client.js:76:10)
    at Object.<anonymous> (C:\Users\jason\Desktop\discord minecraft bot test 2\index.js:9:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32) {
  [Symbol(code)]: 'BITFIELD_INVALID'
}

Node.js v18.12.1
PS C:\Users\jason\Desktop\discord minecraft bot test 2>

First of all, please see the guide on how to use intents . (It's really useful!)

This doesn't look like DJSv14 code either. v14 snippets can be found in the guide as well.

To fix your issue you can change the way you define your intents when instantiating your new client like so;

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

// ...

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ],
});

OT: Newer D.JS versions have application commands , I highly recommend you use those instead of prefix commands!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM