如何通过ID不和谐检查用户是否在不同的公会中扮演角色.js v12?



我想为我的机器人的捐赠者提供一些福利,我想通过检查此人是否在我的支持服务器中具有角色来检查此人是否是捐赠者。我找到了一段应该检查它的代码,但我有一个问题。我的代码如下所示:

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。机器人当然在支持公会中。我正在使用不和谐.js v12。我在那行做错了什么?也许还有其他任何地方?谢谢:)

问题是您正在创建一个新的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文档)


所以,而不是写:

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

你可以这样写:

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

它将完美地工作!

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

最新更新