简介:
I'm pretty new at creating discord BOT and I'm currently trying to make like a ticket bot. In order to do that, I need to check if the user that add the reaction is allow to do it or not.
代码:
I already checked this (successfully) when the user is using a command thanks to this (isAllow is a custom function that works when passing member object in params) :
if (Common.isAllow(message.member)) {/* code ... */}
But I also want to check for the roles of the user that is reacting to a message, in order to create a new channel if the user is allowed to do it.
The problem is that when I get the user object, the lastMessageId is null, so I cannot get member object and check its roles.
检查 react 的代码是这个:
client.on("messageReactionAdd", async (reaction, user) => {
// When a reaction is received, check if the structure is partial
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
switch (reaction.message.channel.id) {
// Check if the message has been sent into the lawyer contact channel
case Config.CHANNELS.LAWYER_CONTACT:
case Config.CHANNELS.CONFIG:
checkForLawyerChannelReaction(reaction, user);
break;
default:
break;
}
});
如果在好的 channel (LAWYER_CONTACT)或配置 channel (CONFIG)中做出了 react ,它将调用这个函数,这是一个试图检查权限的函数:
function checkForLawyerChannelReaction(reaction, user) {
console.log('checkForLawyerChanelReaction');
console.log(user);
if (Common.isAllow( /* member object */ ) { /* code ... */ }
我对 console.log(user) 得到的响应是: Result
我已经尝试过像这样获取用户数据,但它并没有改变任何事情:
console.log(await client.users.fetch('123456789123456789'));
(当然ID换了)
请注意,BOT 昨天以管理员权限加入(再次),并且从昨天开始用户已发送消息。
是否有人对此有解决方案或我可能还没有的信息?
感谢您的宝贵时间。
最佳答案
您可以通过将 user
作为参数传递给 Guild.member(user)
来获取 GuildMember
对象方法。
const guildMember = reaction.message.guild.member(user);
关于javascript - Discordjs - 获取后 lastMessageId 始终为空事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68428952/