2
votes

I currently have a command to create a bot invite link based on the ID args provided.

Example: !permissioncalculator 123456789, where the nine-digit long number stands for the client ID of the discord bot.

This works fine and all, but I made it return the invite link with admin (one of the possible permissions) or (8) by default. I want to be able to choose a specific permission name for example MANAGE_MESSAGES and convert that to a permission number. So you could do !permissioncalculator 123456789 MANAGE_MESSAGES.

My current code:

const clientid = args.join(" ").slice(9);
const authorizationUrl = `https://discordapp.com/api/oauth2/authorize?client_id=${clientid}&scope=bot&permissions=8`;
let errorEmbed = new Discord.MessageEmbed()
  .setColor("#e31212")
  .setDescription("ERROR: No valid bot ID entered");
if (!clientid) return message.channel.send(errorEmbed);
let nanEmbed = new Discord.MessageEmbed()
  .setColor("#e31212")
  .setDescription(
    "ERROR: The sequence you have provided is not an integer"
  );
if (isNaN(args[1])) return message.channel.send(nanEmbed);
let tooshortid = new Discord.MessageEmbed()
  .setColor("#e31212")
  .setDescription(
    "ERROR: The sequence you have provided is less than 18 digits"
  );
if (clientid.length < 18) return message.channel.send(tooshortid);
let authorizationEmbed = new Discord.MessageEmbed()
  .setColor("#8f82ff")
  .setDescription(
    `Here is your invite link for your bot <@${clientid}> \n ${authorizationUrl}`
  )
message.channel.send(authorizationEmbed);

I read the Discord Permission Conversion mdn, but I did not find that helpful.

2

2 Answers

0
votes

Each permission in Discord has its own integer assigned to it. When Discord needs to calculate permissions for an invite link or client user, it adds together all the integers of the permissions that are selected or that the client user has. You can view all the integers on the Discord Developer Portal by going to

Applications > Your App > OAuth2 > OAuth2 URL Generator

and selecting bot in the first section, and then selecting the permissions you want from the second section. As you select permissions, the number in the URL above will start to change. For example, with just ADMINISTRATOR permissions the URL will look like

https://discord.com/api/oauth2/authorize?client_id=ID_HERE&permissions=8&scope=bot

and with just SEND_MESSAGES permission, the URL will look like

https://discord.com/api/oauth2/authorize?client_id=ID_HERE&permissions=2048&scope=bot

From that, we can work out that the integer for ADMINISTRATOR permissions is 8, and the integer for SEND_MESSAGES is 2048. So if the bot had both permissions, the integer would be 8 + 2048 which is 2056.

I hope that all made sense.

0
votes

Going off of what SirArchibald said, there's actually a really easy way to calculate the total amount that Discord.js guide example almost already provides.

If you simply set up the permissions you wanted in an array, and created a new Permission from that array, it'll set up a Map with a key and value (the bitfield), that'll just calculate it all for you.

Example - modify this accordingly to your needs:

const { Permissions } = require('discord.js');
const flags = [
    'MANAGE_CHANNELS',
    'EMBED_LINKS',
    'ATTACH_FILES',
    'READ_MESSAGE_HISTORY',
    'MANAGE_ROLES',
];

const permissions = new Permissions(flags);
//this is the numerical value you can plug in instead of 8 for your URL
console.log(permissions['bitfield']);