Discord.js: Permission Checks Before API Calls

Created on 15 Aug 2019  路  5Comments  路  Source: discordjs/discord.js

Is your feature request related to a problem? Please describe.
Recently, Discord implemented some API call limits for certain errors. The most worrisome being the Missing Permission error which effects quite a lot of developers. It would be nice to have D.js automatically handle these checks before sending an API call so that the bot doesn't get auto banned.

Describe the ideal solution
If a developer does message.channel.send('something'), discord.js could check all necessary permissions to make sure this is possible.

async send(content, options) {
     // Snippet of code 

    const { data, files } = await apiMessage.resolveFiles();

    if (!this.permissionsFor(this.guild.me).has(['VIEW_CHANNEL', 'SEND_MESSAGES'])) {
      // throw a custom error
    }
    return this.client.api.channels[this.id].messages.post({ data, files })
      .then(d => this.client.actions.MessageCreate.handle(d).message);
  }

Describe alternatives you've considered
N/A

Additional context
N/A

enhancement

Most helpful comment

You forgot this:

Describe alternatives you've considered
Do them myself.

All 5 comments

You forgot this:

Describe alternatives you've considered
Do them myself.

I believe having D.js handling this is much better and safer. It is the same as D.JS handling rate limits. By the same logic devs should handle rate limits themself as well.

No it's not, you're literally using a framework with a field in each command called requiredPermissions, please use it.

As much as I agree with you, I have to also disagree. Adding manual permission checking in discord.js would make it:

  1. Opinionated
  2. Require us to update the library every time Discord adds / removes a permission required on an endpoint

As @kyranet suggested above, you are currently using a framework (Klasa) that lets you do that. if you REALLY want to add these checks yourself, nothing stops you from overwriting the prototype of a function and doing these checks. In fact, here's a very basic (and don't use this for the love of God) example of that:

import { TextChannel, Permissions } from 'discord.js';

const sendFunction = TextChannel.prototype.send;

TextChannel.prototype.send = function send(this: TextChannel, ...args: any[]) {
    if (!this.permissionsFor(this.guild.me)!.has(Permissions.FLAGS.VIEW_CHANNEL)) throw new Error("Oopsie woopsie, you made a fucky wucky!");

    return sendFunction.call(this, args);
}

If you want to be extra fancy, you can use Structures to extend your classes and add these checks. Again, nothing stops you from doing it yourself 馃槃

Would this be a nice to have thing in discord.js? Absolutely
Is this getting added to discord.js? Probably not!

I understand you wanting this feature, however, as stated above, this would be opinionated and require us to update Discord.js every time Discord modifies permissions required for endpoints.

Instead, you can make these checks yourself and this also allows you to give users feedback, e.g. "I need permission to send files in this channel") rather than the generic error returned by Discord.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iCrawl picture iCrawl  路  3Comments

ghost picture ghost  路  3Comments

Blumlaut picture Blumlaut  路  3Comments

smchase picture smchase  路  3Comments

tom-barnes picture tom-barnes  路  3Comments