Search code examples
node.jsdiscord.js

How to use role ID's and Member ID's discord js


I have also used else if statements for more roles so I would like to be able to use either the role ID or the user ID for the const.

const { DiscordAPIError } = require("discord.js")
const Discord = require('discord.js');
const client = new Discord.Client();
const { Client, Attachment, Message, MessageEmbed } = require("discord.js");
const { newInMsgEvtInterceptor } = require("fix/handlers/inMsgEvtInterceptor");

module.exports = {
    name: `say`,
    aliases: ['send'],
    cooldown: 10,
    description: "sends a message through the bot",
    execute (client, message, args){
        let { content, member, channel } = message
        let timeout;
        
        const Sradmin = message.guild.roles.cache.find(r => r.name === "SR.Admin");
        
//----------------------------------------------------------
        if (message.member.roles.cache.has(Sradmin.id)) {
        
            message.delete({timeout: 100})
            message.channel.send(`${args.join(" ")}** **`);
//----------------------------------------------------------   

        } else {   
            message.channel.send(`You cant use this command`);
    
        }
    }
}

Edit: I would like to replace the name of the role in this case SR.Admin with the ID of the role: 1044399351742611516 As well as being able to add custom users to be able to use this command by using there ID: 927305865428611172


Solution

  • For role ID'S I can use this

    const role1 = message.member.roles.cache.find(r => r.id === "1044399351742611516")
    

    For Users I can use

    const admins = ['927305865428611172'] // You can add more user ID'S in this box
    

    and

    if(admins.includes(message.author.id)) 
    

    The final code would look something like this

    const { DiscordAPIError } = require("discord.js")
    const Discord = require('discord.js');
    const client = new Discord.Client();
    const { Client, Attachment, Message, MessageEmbed } = require("discord.js");
    const { newInMsgEvtInterceptor } = require("fix/handlers/inMsgEvtInterceptor");
    
    module.exports = {
        name: `say`,
        aliases: ['send'],
        cooldown: 10,
        description: "sends a message through the bot",
        execute (client, message, args){
            let { content, member, channel } = message
            let timeout;
            
            const admins = ['927305865428611172']
    
            const role1 = message.member.roles.cache.find(r => r.id === "1044399351742611516")
    
            
    //----------------------------------------------------------
            if(admins.includes(message.author.id)) {
            
                message.delete({timeout: 100})
                message.channel.send(`${args.join(" ")}** **`);
    //----------------------------------------------------------
            else if (message.member.roles.cache.has(role1.id)) {
            
                message.delete({timeout: 100})
                message.channel.send(`${args.join(" ")}** **`);
    //----------------------------------------------------------   
    
            } else {   
                message.channel.send(`You cant use this command`);
        
            }
        }
    }