discordjs / builders

A collection of builders that you can use when creating your bot.
Apache License 2.0
97 stars 37 forks source link

Can't add permissions #27

Closed Faabvk closed 2 years ago

Faabvk commented 3 years ago

This is likely already in the pipeline to be added, but there's no function to add permissions to commands at this time.

mahyarmirrashed commented 3 years ago

Could you clarify what you mean by permissions? Are they the permissions located here? Or are you referring to a potentially new feature where you can specify what permissions the user needs to invoke the command?

Faabvk commented 3 years ago

Yes, those are the permissions I'm referring to. It feels redundant having to specify permissions after the command has been created, rather than being able to specify them during the creation of the command.

vladfrangu commented 3 years ago

This is not something we can help out with as there is no API support for this at this time... You should leave feedback on their GitHub repository about this!

mahyarmirrashed commented 3 years ago

I am linking this discussion on the Discord API GitHub as there are a few others with the same request. I guess the team can implement it whenever the new API features are released. https://github.com/discord/discord-api-docs/discussions/3581

I think we can close this issue for now though and reopen when necessary.

rfwn commented 2 years ago

As I know, defaultPermission: false in slash command data makes it unusable for non-admin members in a guild, So I'd like to see something like setDefaultPermissions(true) in slash command builders

megatank58 commented 2 years ago

The setDefaultPermission(boolean) is available on SlashCommandBuilder

nemoralis commented 2 years ago

As I know, defaultPermission: false in slash command data makes it unusable for non-admin members in a guild, So I'd like to see something like setDefaultPermissions(true) in slash command builders

19

fivekWBassMachine commented 2 years ago

The setDefaultPermission(boolean) is available on SlashCommandBuilder

And I guess it's not working. I've used the example at https://discordjs.guide/creating-your-bot/command-handling.html#individual-command-files and created an extra file for each command.

The command file (/CommandStatus.js):

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('status')
        .setDescription('Returns information about the bots health status.')
        .setDefaultPermission(false), // should disable the command for everyone
    permission: 1, // irrelevant, it's for my permission system
    async execute(interaction) {
        // command stuff, irrelevant here
    }
};

async function wait(ms) {
    await new Promise(resolve => setTimeout(resolve, ms));
}

Le exception:

/CommandStatus.js:7
        .setDefaultPermission(false),
         ^

TypeError: (intermediate value).setName(...).setDescription(...).setDefaultPermission is not a function
    at Object.<anonymous> (/CommandStatus.js:7:10)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at *my main class*

In my main class, i registered the commands as described here: https://discordjs.guide/creating-your-bot/command-handling.html#reading-command-files the error is thrown in the line where i included /CommandStatus.js

Setting SlashCommand.defaultPermission directly did not work either, so my solution was to add default_permission after calling SlashCommand.toJSON before pushing the json object to the commands array:

const command = require('/CommandStatus.js'); // simplified
const data = command.data.toJSON();
data.default_permission = false;
commands.push(data); // commands is the array which is PUT to the api via the rest call.

Well, I've got, what I wanted, the command is disabled. But I can't enable it for specific groups. https://discordjs.guide/interactions/slash-command-permissions.html#user-permissions describes how it should work - but I don't have the command id.

solved using this after bot is ready

MainClass {
    async #setCommandPermissions(client) {
        function getRolesWithPermission() {
            const roles = [];
            for (/* iterate through roles, permission levels, .. */) {
                if (/* role is permitted */) roles.push({ id: /* role id */, type: 'ROLE', permission: true });
            }
            return roles;
        }
        const commands = await client.guilds.cache.get(client.config.discord.guild)?.commands.fetch();
        commands.each(async command => {
            await command.permissions.add({ permissions: getRolesWithPermission() });
        });
    }
}
Khasms commented 2 years ago

@fivekWBassMachine You need to update your builders version to v0.6.

fivekWBassMachine commented 2 years ago

thanks. no clue why, but in my package.json had no @discordjs/builders dependency. ive installed it manually and than SlashCommand.defaultPermission worked as expected. the other problem to enable permissions again for some roles is solved either (althought the guide helped me with nothing)

vladfrangu commented 2 years ago

Since it seems like the issue at hand is solved, I'm going to close this. If that's not the case, please follow up and I'll reopen this for you :D