简体   繁体   中英

How to check if a user has a role in a different guild by it's ID discord.js v12?

I want to make some perks for donators for my bot and I want to check if the person is a donator by checking if the person has a role in my support server. I found a piece of code that should check for it, but I have a problem with it. My code looks like this:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
    name: 'donate',
    description: "The bot will list the sponsors and tell you the way how to become one",
    execute(message, args){
        if(message.member.roles.cache.has('750723943869972641')){
            message.reply(`you are a donator`)
        };
        const supportGuild = client.guilds.cache.get('746082575889596456')
        console.log(`Support guild: ${supportGuild}`)
        const member = supportGuild.members.cache.get(message.author.id)
        const isDonator = member ? member.roles.cache.some(role => role.id === '750723943869972641') : false
        const donatorRole = member.roles.cache.some(role => role.id === '750723943869972641')
        if (isDonator = 'true') {
            message.reply('donator')
        }
        const embed = new Discord.MessageEmbed()
        .setTitle('Donate')
        .setColor('#DAF7A6')
        .addFields(
            {name: 'Donator perks',
            value: `By donating, you will be listed here along with your link chosen by you. You will also get a special role on the support server (<@&${donatorRole}>) and with that a lower command cooldowns and more profit in economy.`}
        )
        .setFooter('Bot made by mkpanda')
        message.channel.send(embed);
    }
}

but it doesn't get the support server guild. As you can see, I've tried logging the value of supportGuild , but it logged Support guild: undefined . The bot is of course in the support guild. I'm using discord.js v12. What did I do wrong in that line? And maybe anywhere else? Thanks :)

The problem is that you are creating a new Discord.Client() instance, which does not share the same channels, members, roles, etc. as the original. Instead of creating a new Discord.Client() , you should pass the original one as an argument to your execute() function.

For example, you could change async execute(message, args){ to async execute(message, args, client){ . Then, in your command handler, change command.execute(message, args) to command.execute(message, args, client)


However , there is an even easier way. client is actually a valid property of the message object, referring to:

The client that instantiated the message

( Message#client docs )


So, instead of writing:

const supportGuild = client.guilds.cache.get('746082575889596456')

You could write:

const supportGuild = message.client.guilds.cache.get('746082575889596456')

And it will work perfectly!

supportGuild is undefined because your client is not connected to Discord. You shouldn't define a new client for each command but pass the original client (from your main process) as a parameter to your execute function.

// This is how you should run the execute function.
execute(client, message, args)
const Discord = require('discord.js');

module.exports = {
    name: 'donate',
    description: "The bot will list the sponsors and tell you the way how to become one",
    execute(client, message, args){
        if(message.member.roles.cache.has('750723943869972641')){
            message.reply(`you are a donator`)
        };
        const supportGuild = client.guilds.cache.get('746082575889596456')
        console.log(`Support guild: ${supportGuild}`)
        const member = supportGuild.members.cache.get(message.author.id)
        const isDonator = member ? member.roles.cache.some(role => role.id === '750723943869972641') : false
        const donatorRole = member.roles.cache.some(role => role.id === '750723943869972641')
        if (isDonator = 'true') {
            message.reply('donator')
        }
        const embed = new Discord.MessageEmbed()
        .setTitle('Donate')
        .setColor('#DAF7A6')
        .addFields(
            {name: 'Donator perks',
            value: `By donating, you will be listed here along with your link chosen by you. You will also get a special role on the support server (<@&${donatorRole}>) and with that a lower command cooldowns and more profit in economy.`}
        )
        .setFooter('Bot made by mkpanda')
        message.channel.send(embed);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM