Hrittik Chatterjee
Hrittik Chatterjee

Reputation: 91

Changing permissions discord.js

Ok so I've been working on a bot that creates channel overrides using discord.js. I'm kind of new to discord.js but I checked the documentary and it seems that the correct method to use when changing overrides is what I used in the code below, but I get an error message and I have absolutely no clue as to how I can fix it.

if (command == 'lockdown') {
 if (commandArgs == '') {
  let channel = msg.channel;
  let roles = msg.guild.roles;
  console.log(roles);
  let testRole = 708384707015868486;
  channel
   .overwritePermissions(testRole, { SEND_MESSAGES: false }, 'closing up shop')
   .then(console.log)
   .catch(console.log);
 }
}

This is the code for that specific command, but I get the following error message.

TypeError [INVALID_TYPE]: Supplied overwrites is not an Array or Collection of Permission Overwrites.
    at TextChannel.overwritePermissions (/home/runner/Ecliptical-Productions-V3/node_modules/discord.js/src/structures/GuildChannel.js:208:9)
    at Client.<anonymous> (/home/runner/Ecliptical-Productions-V3/index.js:25:17)
    at Client.emit (events.js:315:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/Ecliptical-Productions-V3/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/Ecliptical-Productions-V3/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/Ecliptical-Productions-V3/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/Ecliptical-Productions-V3/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/Ecliptical-Productions-V3/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/Ecliptical-Productions-V3/node_modules/ws/lib/event-target.js:125:16) {
      [Symbol(code)]: 'INVALID_TYPE'
    }

How can I fix this error? I checked the channel overrides after the command and the changes weren't made, I've been trying to fix this for ages but I can't find the error.

Upvotes: 0

Views: 2249

Answers (3)

RareHyperIon
RareHyperIon

Reputation: 11

If you have "message" defined you can use "message.channel" to get the channel you typed the message in.

message.channel.createOverwrite(message.guild.id, { SEND_MESSAGES: false })

You can see that above that would change send messages permissions for all roles except moderation roles and if you wanted to send a message after you can use the ".then()" function, If you use V12 everything i told you will work fine, I suggest you research before asking there's loads of information and tutorials online you can find.

Upvotes: 0

Jadev
Jadev

Reputation: 303

overwritePermissions expects an array and you supplied it with 2 objects.

channel.overwritePermissions(
 [  //<--- you are missing these
  {
   id: message.author.id,
   deny: ['SEND_MESSAGES'],
  },
 ], //<--- you are missing these
 'Needed to change permissions'
);

https://discord.js.org/#/docs/main/stable/class/GuildChannel?scrollTo=overwritePermissions

Upvotes: 0

T. Dirks
T. Dirks

Reputation: 3676

The overwritePermissions method wants an Array or a Collection of OverwriteResolvables as the first argument. Instead, your first argument is the id of the role you want to change the permissions for.

Change the channel.overwritePermissions to the example code below and give it a try.

channel.overwritePermissions(
 [
  {
   id: testRole,
   deny: ['SEND_MESSAGES'],
  },
 ],
 'Closing up shop'
);

In this example, the supplied OverwriteResolvable is of type OverwriteData which can be defined with a simple Object structure.

Upvotes: 3

Related Questions