RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number

Viewed 18488

Hello I am working on my mute command and I came up with this error:

RangeError [BITFIELD_INVALID]: Invalid bitfield flag or number.
at Function.resolve (/app/node_modules/discord.js/src/util/BitField.js:150:19)
     at /app/node_modules/discord.js/src/util/BitField.js:148:54
   at Array.map (<anonymous>)
     at Function.resolve (/app/node_modules/discord.js/src/util/BitField.js:148:40)
     at RoleManager.create (/app/node_modules/discord.js/src/managers/RoleManager.js:112:58)
    at Client.<anonymous> (/app/index.js:586:41)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
   at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)

I want to make a mute role for my mute command here is my command:

 message.guild.roles.create({
                        data: {
                          name: 'muted',
                          color: '#ff0000',
                          permissions: [
                              "SEND_MESSAGES" === false,
                              "ADD_REACTIONS" === false

                          ]
                        },
                        reason: 'to mute people',
                      })
                        .catch(console.error);
                } catch (e) {
                    console.log(e.stack);
                }
            } return message.channel.send('Cant')

I don't quite know how to fix it please help me

3 Answers

I stumbled onto this old question today, and though I'm quite a bit late, the current primary answer posted by Androz2091 is incorrect.

You can't use an array for the permissions value (it must be an object):

This is completely false. In no version of discord.js from v11 to v13 was the permissions value for creating a role an object literal in the form presented by that answer. The permissions value must be a PermissionResolvable. A PermissionResolvable can be a string such as "SEND_MESSAGES", or it can be an array of such strings. Review the docs.

As for what the OP actually got wrong, notice how they specified their permission strings:

permissions: [
    "SEND_MESSAGES" === false,
    "ADD_REACTIONS" === false
]

That is incorrect. That's the same thing as doing permissions: [ false, false ]. Obviously false is not a permission, which is why you are getting the invalid bitfield flag error.

That is not how permissions work when creating roles. If you look at your guild's role permissions, you'll see that each permission has two states: enabled or disabled. The way the permissions value works in guild.roles.create() is, it enables any permissions that are specified and disables ALL other permissions. That means, if you want SEND_MESSAGES and ADD_REACTIONS disabled for your Muted role, you simply need to not include them in the permissions array.

Instead, include whatever permissions the muted users are supposed to have. For example, if they should be able to view channels, give them VIEW_CHANNEL. Here is an example solution:

permissions: [
    "VIEW_CHANNEL",
    "READ_MESSAGE_HISTORY"
]

That should work pretty well for a Muted role. Note that how permissions work in this way is still the same for both discord.js v12 and v13, but the way in which you create roles itself has changed a little bit between the versions. However, this solution should still be applicable to both versions.

You can't use an array for the permissions value (it must be an object):

message.guild.roles.create({
                        data: {
                          name: 'muted',
                          color: '#ff0000',
                          permissions: {
                              SEND_MESSAGES: false,
                              ADD_REACTIONS: false
                          }
                        },
                        reason: 'to mute people',
                      })
                        .catch(console.error);
                } catch (e) {
                    console.log(e.stack);
                }
            } return message.channel.send('Cant')

Reinstalling discord.js worked for me.

npm uninstall discord.js

npm install discord.js
Related