skip to Main Content

I’m developing a discord bot in Discord.js V14 and I’m working on a command called "claimticket". The problem I’m having is that I’m not being able to restrict the command to anyone who has one of the two roles that I indicate in the command.

If possible, I would like help to restrict the claimticket command to only be used by members who have at least 1 of two roles! Below I send the command code in full.

const Discord = require("discord.js");
const config = require("../../config.json")
const StaffRolesPermitted = [`${config["ID-role"].staff}`, `${config["ID-role"]["trial-helper"]}`];

module.exports = {
    name: "claimticket",
    description: "claim a ticket",

    run: async (client, interaction) => {
        
        const member = interaction.user;
        const StaffRolesPermitted = member.roles.cache.some(role => StaffRolesPermitted.includes(role.id));

        if (!StaffRolesPermitted) {
            return interaction.reply({ content: "You do not have permission to use this command!", ephemeral: true });
        } else {

            let embed = new Discord.EmbedBuilder()
            .setColor("Green")
            .setFooter({ text: `${config.servidor.footer}`, iconURL: `${config.servidor.logo}`})
            .setDescription(`**This Ticket will be serviced by ${interaction.user}!**`)

            await interaction.reply({ embeds:  });

        }
    }
}

2

Answers


  1. Not sure, but i would try this maybe :

    // specify which roles are allowed
    const requiredRoles = ['admin', 'moderator'];
    
    // get user who typed the command
    const member = message.member
    
    
    // check if the user has one role inside the array of requiredRoles
    const hasRequiredRole = requiredRoles.some((roleName) =>
        member.roles.cache.some((role) => role.name === roleName)
    )
    
    
    // your business code :D
    if(hasRequiredRole){
        // do your command function
    } else {
        // no permission
    }
    
    Login or Signup to reply.
  2. Check Docs https://discord.js.org/docs/packages/collection/main/Collection:Class#hasAny

    // Role IDs
    const roles = ["123", "456", "789"]
    
    if (<GuildMember>.roles.cache.hasAny(...roles)) {
       // ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search