Invalid bitfield flag or number: undefined.

WHAT DOES THIS EVEN MEANS
24 Replies
d.js toolkit
d.js toolkit2mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Auth
AuthOP2mo ago
const { PermissionsBitField } = require('discord.js');
const path = require('path');
const fs = require('fs');
const {isWhitelisted} = require('./database')

// Load config
const configPath = path.join(__dirname, "..", "configs", "config.json");
let config;
try {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
console.log("Config loaded successfully");
} catch (error) {
console.error("Failed to load config:", error);
process.exit(1);
}

async function purge(message, args) {
// Check if the message starts with the correct prefix
if (!message.content.startsWith(config.PREFIX)) return;

// Check if the user has the necessary permissions
const isWhitelistedUser = await isWhitelisted(interaction.user.id);

if(!isWhitelisted){
return message.reply('You dont have permission to use this command')
}

// Get the amount of messages to delete
const amount = parseInt(args[0]);

// Validate the amount
if (isNaN(amount) || amount < 1 || amount > 100) {
return message.reply('Please provide a number between 1 and 100 for the amount of messages to delete.');
}

try {
// Fetch messages
const messages = await message.channel.messages.fetch({ limit: amount + 1 }); // +1 to include the command message

// Filter out messages older than 14 days (Discord API limitation)
const filteredMessages = messages.filter(msg => {
return Date.now() - msg.createdTimestamp < 14 * 24 * 60 * 60 * 1000;
});

// Delete messages
await message.channel.bulkDelete(filteredMessages, true);

actually deleted
const deletedCount = filteredMessages.size - 1;
const confirmationMsg = await message.channel.send(`Successfully deleted ${deletedCount} messages.`);

setTimeout(() => confirmationMsg.delete(), 1000);
} catch (error) {
console.error('Error purging messages:', error);
message.reply('There was an error trying to purge messages.');
}
}

module.exports = purge;
const { PermissionsBitField } = require('discord.js');
const path = require('path');
const fs = require('fs');
const {isWhitelisted} = require('./database')

// Load config
const configPath = path.join(__dirname, "..", "configs", "config.json");
let config;
try {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
console.log("Config loaded successfully");
} catch (error) {
console.error("Failed to load config:", error);
process.exit(1);
}

async function purge(message, args) {
// Check if the message starts with the correct prefix
if (!message.content.startsWith(config.PREFIX)) return;

// Check if the user has the necessary permissions
const isWhitelistedUser = await isWhitelisted(interaction.user.id);

if(!isWhitelisted){
return message.reply('You dont have permission to use this command')
}

// Get the amount of messages to delete
const amount = parseInt(args[0]);

// Validate the amount
if (isNaN(amount) || amount < 1 || amount > 100) {
return message.reply('Please provide a number between 1 and 100 for the amount of messages to delete.');
}

try {
// Fetch messages
const messages = await message.channel.messages.fetch({ limit: amount + 1 }); // +1 to include the command message

// Filter out messages older than 14 days (Discord API limitation)
const filteredMessages = messages.filter(msg => {
return Date.now() - msg.createdTimestamp < 14 * 24 * 60 * 60 * 1000;
});

// Delete messages
await message.channel.bulkDelete(filteredMessages, true);

actually deleted
const deletedCount = filteredMessages.size - 1;
const confirmationMsg = await message.channel.send(`Successfully deleted ${deletedCount} messages.`);

setTimeout(() => confirmationMsg.delete(), 1000);
} catch (error) {
console.error('Error purging messages:', error);
message.reply('There was an error trying to purge messages.');
}
}

module.exports = purge;
treble/luna
treble/luna2mo ago
show the full error because this is not the code that throws it
Auth
AuthOP2mo ago
Started refreshing application (/) commands. Successfully reloaded application (/) commands. node:events:496 throw er; // Unhandled 'error' event ^ RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined. at PermissionsBitField.resolve (/home/authtbh/mirai/node_modules/discord.js/src/util/BitField.js:174:11) at PermissionsBitField.has (/home/authtbh/mirai/node_modules/discord.js/src/util/BitField.js:60:28) at PermissionsBitField.has (/home/authtbh/mirai/node_modules/discord.js/src/util/PermissionsBitField.js:92:82) at purge (/home/authtbh/mirai/libs/purgeFunction.js:22:33) at Client.<anonymous> (/home/authtbh/mirai/index.js:82:13) at Client.emit (node:events:518:28) at MessageCreateAction.handle (/home/authtbh/mirai/node_modules/discord.js/src/client/actions/MessageCreate.js:28:14) at module.exports [as MESSAGE_CREATE] (/home/authtbh/mirai/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (/home/authtbh/mirai/node_modules/discord.js/src/client/websocket/WebSocketManager.js:355:31) at WebSocketManager.<anonymous> (/home/authtbh/mirai/node_modules/discord.js/src/client/websocket/WebSocketManager.js:239:12) Emitted 'error' event on Client instance at: at emitUnhandledRejectionOrErr (node:events:401:10) at process.processTicksAndRejections (node:internal/process/task_queues:84:21) { code: 'BitFieldInvalid' } Node.js v20.12.2 [nodemon] app crashed - waiting for file changes before starting...
treble/luna
treble/luna2mo ago
whats purgefunction.js line 22
Auth
AuthOP2mo ago
const isWhitelistedUser = await isWhitelisted(interaction.user.id);
treble/luna
treble/luna2mo ago
and what does that function do
Auth
AuthOP2mo ago
function isWhitelisted(userId) {
return new Promise((resolve, reject) => {
db.get('SELECT * FROM whitelist WHERE userId = ?', [userId], (err, row) => {
if (err) reject(err);
else resolve(!!row);
});
});
}
function isWhitelisted(userId) {
return new Promise((resolve, reject) => {
db.get('SELECT * FROM whitelist WHERE userId = ?', [userId], (err, row) => {
if (err) reject(err);
else resolve(!!row);
});
});
}
check the db if the user is whitelisted
treble/luna
treble/luna2mo ago
is your code saved. because i see you importing PermissionsBitField but never using it
Auth
AuthOP2mo ago
autosave so yes where tf am i importing it
treble/luna
treble/luna2mo ago
the top are you sure it is saved because the error is either from that or invalid intents
Auth
AuthOP2mo ago
lemme just do small changes real quick
const { PermissionsBitField } = require("discord.js");
const path = require("path");
const fs = require("fs");

// Load config
const configPath = path.join(__dirname, "..", "configs", "config.json");
let config;
try {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
console.log("Config loaded successfully");
} catch (error) {
console.error("Failed to load config:", error);
process.exit(1);
}

async function purge(message, args) {

if (!message.content.startsWith(config.PREFIX)) return;


if (
!message.member.permissions.has(PermissionsBitField.Flags.MANAGE_MESSAGES)
) {
return message.reply("You do not have permission to use this command.");
}


const amount = parseInt(args[0]);


if (isNaN(amount) || amount < 1 || amount > 100) {
return message.reply(
"Please provide a number between 1 and 100 for the amount of messages to delete.",
);
}

try {
// Fetch messages
const messages = await message.channel.messages.fetch({
limit: amount + 1,
});
const filteredMessages = messages.filter((msg) => {
return Date.now() - msg.createdTimestamp < 14 * 24 * 60 * 60 * 1000;
});

// Delete messages
await message.channel.bulkDelete(filteredMessages, true);


const deletedCount = filteredMessages.size - 1;

// Send confirmation message
const confirmationMsg = await message.channel.send(
`Successfully deleted ${deletedCount} messages.`,
);

// Delete the confirmation message after 5 seconds
setTimeout(() => confirmationMsg.delete(), 1000);
} catch (error) {
console.error("Error purging messages:", error);
message.reply("There was an error trying to purge messages.");
}
}

module.exports = purge;
const { PermissionsBitField } = require("discord.js");
const path = require("path");
const fs = require("fs");

// Load config
const configPath = path.join(__dirname, "..", "configs", "config.json");
let config;
try {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
console.log("Config loaded successfully");
} catch (error) {
console.error("Failed to load config:", error);
process.exit(1);
}

async function purge(message, args) {

if (!message.content.startsWith(config.PREFIX)) return;


if (
!message.member.permissions.has(PermissionsBitField.Flags.MANAGE_MESSAGES)
) {
return message.reply("You do not have permission to use this command.");
}


const amount = parseInt(args[0]);


if (isNaN(amount) || amount < 1 || amount > 100) {
return message.reply(
"Please provide a number between 1 and 100 for the amount of messages to delete.",
);
}

try {
// Fetch messages
const messages = await message.channel.messages.fetch({
limit: amount + 1,
});
const filteredMessages = messages.filter((msg) => {
return Date.now() - msg.createdTimestamp < 14 * 24 * 60 * 60 * 1000;
});

// Delete messages
await message.channel.bulkDelete(filteredMessages, true);


const deletedCount = filteredMessages.size - 1;

// Send confirmation message
const confirmationMsg = await message.channel.send(
`Successfully deleted ${deletedCount} messages.`,
);

// Delete the confirmation message after 5 seconds
setTimeout(() => confirmationMsg.delete(), 1000);
} catch (error) {
console.error("Error purging messages:", error);
message.reply("There was an error trying to purge messages.");
}
}

module.exports = purge;
treble/luna
treble/luna2mo ago
yeah those arent valid permissions did you ask ai to write that
Auth
AuthOP2mo ago
hell yeah
treble/luna
treble/luna2mo ago
this is exactly why you shouldnt do that write your own code and use the docs
Auth
AuthOP2mo ago
alright wait lemme try it myself
Auth
AuthOP2mo ago
!message.member.permissions.has(PermissionsBitField.Flags.MessageManager)
!message.member.permissions.has(PermissionsBitField.Flags.MessageManager)
No description
treble/luna
treble/luna2mo ago
..no where did you get that from
d.js docs
d.js docs2mo ago
:class: PermissionsBitField @14.16.1 Data structure that makes it easy to interact with a permission bitfield. All GuildMembers have a set of permissions in their guild, and each channel in the guild may also have PermissionOverwrites for the member that override their default permissions.
Auth
AuthOP2mo ago
😭
No description
Auth
AuthOP2mo ago
if ( !message.member.permissions.has(PermissionsBitField.Flags.MANAGE_MESSAGES) ) shouldnt it work
treble/luna
treble/luna2mo ago
those are discord docs i linked you the docs read them
d.js docs
d.js docs2mo ago
:dtypes: v10: PermissionFlagsBits read more
Auth
AuthOP2mo ago
holy shit ManageMessages worked ily
Want results from more Discord servers?
Add your server