如何通过ID Discord.js v12检查用户是否在其他行会中扮演角色?

时间:2020-09-02 18:05:51

标签: javascript discord.js

我想为机器人的捐赠者带来一些好处,并且我想通过检查捐赠者在我的支持服务器中是否有角色来确认捐赠者是否是捐赠者。我找到了一段应该检查的代码,但是我有一个问题。我的代码如下:

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);
    }
}

,但没有获得支持服务器行会。如您所见,我尝试记录supportGuild的值,但记录了Support guild: undefined。该机器人当然在支持行会中。我正在使用discord.js v12。我在那行做错了什么?也许还有其他地方?谢谢:)

2 个答案:

答案 0 :(得分:0)

问题是您正在创建一个新的Discord.Client()实例,该实例与原始实例不共享相同的频道,成员,角色等。代替创建新的Discord.Client(),您应该将原始参数作为参数传递给execute()函数。

例如,您可以将async execute(message, args){更改为async execute(message, args, client){。然后,在命令处理程序中,将command.execute(message, args)更改为command.execute(message, args, client)


但是,甚至有一种更简单的方式。 client实际上是message对象的有效属性,指的是:

实例化消息的客户端

Message#client docs


因此,与其写作:

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

您可以写:

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

它将完美运行!

答案 1 :(得分:-1)

supportGuildundefined,因为您的client未连接到Discord。您不应该为每个命令定义一个新的客户端,而是将原始客户端(来自主进程)作为参数传递给execute函数。

// 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);
    }
}