简体   繁体   中英

Discord.js 'RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number.'

I am trying to create a purge command but when I execute it, it deletes the messages, but it stops the bot with the error RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number.

Here is my index.js

const Discord = require('discord.js')
const client = new Discord.Client()
const prefix = "%"
const cooldowns = new Discord.Collection();
client.commands = new Discord.Collection();
client.app = app
const fs = require('fs');
const db = require('quick.db')

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);
  client.commands.set(command.name, command);
}


client.on('message', message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const commandName = args.shift().toLowerCase();

  const command = client.commands.get(commandName)
    || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

  if (!command) return;

  if (command.guildOnly && message.channel.type === 'dm') {
    return message.reply('I can\'t execute that command inside DMs!');
  }

  if (command.permissions) {
    const authorPerms = message.channel.permissionsFor(message.author);
    if (!authorPerms || !authorPerms.has(command.permissions)) {
      return message.reply('You can not do this!');
    }
  }

  if (command.args && !args.length) {
    let reply = `You didn't provide any arguments, ${message.author}!`;

    if (command.usage) {
      reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
    }

    return message.channel.send(reply);
  }
  if (command.permissions) {
    const authorPerms = message.channel.permissionsFor(message.client.user);
    if (!authorPerms || !authorPerms.has(command.permissions)) {
      return message.channel.reply('You can not do this!');
    }
  }

  if (!cooldowns.has(command.name)) {
    cooldowns.set(command.name, new Discord.Collection());
  }

  const now = Date.now();
  const timestamps = cooldowns.get(command.name);
  const cooldownAmount = (command.cooldown || 3) * 1000;

  if (timestamps.has(message.author.id)) {
    const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

    if (now < expirationTime) {
      const timeLeft = (expirationTime - now) / 1000;
      return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
    }
  }

  timestamps.set(message.author.id, now);
  setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

  try {
    command.execute(message, args);
  } catch (error) {
    console.error(error);
    message.reply('there was an error trying to execute that command!');
  }
});

and my purge.js


module.exports = {
  name: "purge",
  description: "Deletes an amount of messages",
  guildOnly: true,
  permissions: "MANAGE_MESSAGE",
  execute(message, args) {
    if(args[0]){
      if(isNaN(args[0])) return message.reply("That is not a number")
      message.channel.bulkDelete(args[0])
      message.reply(`I. have deleted ${args[0]} messages`)
    } else {
      message.reply("You did not provide a number")
    }
  }
}

When I execute %purge it deletes the messages, but it does not reply when it finished, it also exits the program. So I have to restart the bot every time I use the command.

MANAGE_MESSAGE in your permissions value is not valid permission, you have to use MANAGE_MESSAGES instead! All perms list can be found here !

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