Sam Jackson
Sam Jackson

Reputation: 5

Creating channel permission overwrites

I am creating a bot command so that when you type !setup it sets up the whole server (creating channels and roles, etc). I have already set it to create the roles and channels but there are some channels that only specific roles can type in, whereas, other roles can only read the channel. I don't know how to set up permission overwrites for certain roles.

I have already looked on the Discord.js documentation and it doesn't help much. I receive an error that supplied parameter was neither a user nor a role, but I don't know how to gain the role ID.

message.guild.createRole({
  name: 'Admin',
  color: '#2494ad',
  permissions: ['ADMINISTRATOR', 'VIEW_AUDIT_LOG', 'MANAGE_GUILD', 'MANAGE_CHANNELS', 'SEND_TTS_MESSAGES', 'CREATE_INSTANT_INVITE', 'KICK_MEMBERS', 'BAN_MEMBERS', 'ADD_REACTIONS', 'PRIORITY_SPEAKER', 'READ_MESSAGES', 'SEND_MESSAGES', 'MANAGE_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES', 'READ_MESSAGE_HISTORY', 'MENTION_EVERYONE', 'USE_EXTERNAL_EMOJIS', 'CONNECT', 'SPEAK', 'MUTE_MEMBERS', 'DEAFEN_MEMBERS', 'MOVE_MEMBERS', 'USE_VAD', 'CHANGE_NICKNAME', 'MANAGE_NICKNAMES', 'MANAGE_ROLES', 'MANAGE_WEBHOOKS', 'MANAGE_EMOJIS']
});

message.guild.createRole({
  name: 'Requesting Role',
  color: '#1bb738',
  permissions: ['READ_MESSAGES', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY',]
});

//Categories and Channels
message.guild.createChannel('clan-communications', {
  type: 'category',
  permissionOverwrites: [{
    id:'25311096',
    deny: ['SEND_MESSAGES']
  }]
});

//Permissions
message.channel.overwritePermissions('25311096', {
  SEND_MESSAGES: false
});

break;

I would like roles to have base permissions for the whole server. But certain roles have overwrites for different channels. Instead it says the supplied parameter was neither a user nor a role.

Upvotes: 0

Views: 10820

Answers (1)

slothiful
slothiful

Reputation: 5623

First and foremost, welcome to Stack Overflow. Hopefully we can be of help to you.

Let's walk through a solution step by step to achieve your desired result.

When you create your roles, you should declare a variable to store them. That way, you can use what the client just created later on. Since Guild.createRole() returns a promise, we can save the fulfilled result into a variable.

Note that the keyword await must be placed in async context (within an async function).

const vip = await message.guild.createRole('VIP', { ... });

Then, when you need to use that role, you can refer to the variable.

await message.guild.createChannel('VIPs Only', {
  type: 'category',
  permissionOverwrites: [
    {
      id: vip.id,
      allow: ['READ_MESSAGES']
    }, {
      id: message.guild.defaultRole.id, // @everyone role
      deny: ['READ_MESSAGES']
    }
  ]
});

If the role already exists, you can retrieve it from the Collection of roles returned by Guild.roles.

const testRole = message.guild.roles.get('IDhere');
const adminRole = message.guild.roles.find(r => r.name === 'Admin');

Other simple improvements.

  • In your code snippet, I don't see any error handling. The methods you're using return promises. If an error occurs in any, the promise state will become rejected. Any rejected promises that aren't caught will throw errors. To fix this, attach catch() methods to each promise (better for individual handling) or wrap async code in a try...catch statement.
  • When creating your Admin role, know that the ADMINISTRATOR permission automatically grants all other permissions (and allows users to bypass channel-specific permissions). Therefore, there's no need to list any other permission, or change the role's permission overwrites in any channels.
  • Your IDs in your code don't appear to be valid. Discord IDs are snowflakes, represented by strings of 18 digits. You can access them through Developer Mode in Discord. However, you shouldn't have to hard-code them with this solution, unless they already exist.
  • In the permissions array for your Requesting Role role, you have an extra comma.

Upvotes: 1

Related Questions