TFAGaming / DiscordJS-V14-Bot-Template

A Discord bot with commands, components and events handler based on the latest discord.js v14 and fully written in JavaScript.
GNU General Public License v3.0
354 stars 154 forks source link

Where can I set the permission for the user that they need to have #38

Closed PassiveFr closed 1 year ago

TFAGaming commented 1 year ago

Sorry, but I didn't understand the title of this issue. Please provide more information!

bogdanwebdeveloper commented 1 year ago

In the module.exports you have a line where you can set the permisions needed.

TFAGaming commented 1 year ago

My bad, for slash commands, use the method .setDefaultMemberPermissions from the class SlashCommandBuilder. Here is an example:

const { PermissionFlagBits } = require('discord.js');

new SlashCommandBuilder()
   .setName(...)
   .setDescription(...)
   .setDefaultMemberPermissions(PermissionFlagBits.Administrator)

More info: https://discordjs.guide/slash-commands/permissions.html#member-permissions

Now for prefix commands, you need to use if keyword to check whenever the user has the required permissions or not.

const { PermissionFlagBits } = require('discord.js');

if (!message.member.permissions.has(PermissionFlagBits.Administrator) {
   // Your code here
};
PassiveFr commented 1 year ago

I know how that works but if the user doesn't have perms it will not display the slash cmd but I want it like that it sends an error embed instead

TFAGaming commented 1 year ago

It is similar to prefix command, but instead of message, it's interaction. Here is an example.

const { ChatInputCommandInteraction, SlashCommandBuilder } = require('discord.js');
const ExtendedClient = require('../../../class/ExtendedClient');

module.exports = {
    structure: new SlashCommandBuilder()
        .setName('')
        .setDescription(''),
    /**
     * @param {ExtendedClient} client 
     * @param {ChatInputCommandInteraction<"cached">} interaction 
     */
    run: async (client, interaction) => {

        if (!interaction.member.permissions.has(PermissionFlagsBits.Administrator)) {
            await interaction.reply({
                embeds: [] // <= Your embed
            });

            return;
        };

    }
};
TFAGaming commented 1 year ago

No response, closing...