javascript - 如果有角色 Discord.js 发送消息
问题描述
如果单击反应的用户具有角色,机器人会发送消息 "ID"
我决定试试这个,但没有成功
if(message.member.roles.cache.has(role.id)) {
console.log(`Yay, the author of the message has the role!`);
} else {
console.log(`Nope, noppers, nadda.`);
}
====这里是主要代码====
sentMessage.react("✅");
message.delete({ timeout: 100 });
const filter = (reaction, user) => {
return !user.bot && ["✅"].includes(reaction.emoji.name);
};
sentMessage
.awaitReactions(filter, {
max: 1,
time: 60000,
})
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "✅") {
const member = reaction.users.cache.find((user) => !user.bot);
message.author.send(Hello)
解决方案
您应该检查做出反应的成员(在 中找到的成员reaction.users.cache
)的角色。reaction.users.cache
返回一个用户,你需要一个公会成员来获取他们的角色。您可以为此使用message.guild.members.fetch()
或。message.guild.member()
现在您可以检查返回的成员是否具有角色:
sentMessage.awaitReactions(filter, {
max: 1,
time: 60000,
})
.then(async (collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '1️⃣') {
// find the first user who reacted and is not a bot
const userReacted = reaction.users.cache.find((user) => !user.bot);
// get the guild member
const member = await message.guild.member(userReacted);
if (!member.roles.cache.has('ROLE_ID')) return;
message.author.send({
embed: {
color: 3447003,
title: 'Вызов принят',
description: `**Сотрудник:** ${member}`,
timestamp: new Date(),
},
});
}
})
推荐阅读
- bash - 未找到 Gitlab CI/CD Gradle Clean 构建文件
- javascript - 将嵌套的对象数组转换为对象
- java - 如果@BeforeEach 方法中抛出异常,@AfterEach 是否仍会运行
- python - 如何为热图或树图聚合项目?
- node.js - 使用 Nodemailer 时的“from”属性到底是什么?
- c# - c# 明天的日期时间
- pdf - 使用 Acrobat 动作脚本自动进行拆分、保存名称和错误检查?
- java - Spring @Value 变量未按预期记录
- python - 在 Python 正则表达式中使用排列捕获重复的子模式
- c - 如何使 nftw() 更快