我正在尝试创建一个报告命令,该命令将向所有员工发送一条消息,因为其中一些员工将服务器静音。该机器人仅适用于我的服务器,以便尽量不违反 Discord 的 API。每当我执行命令时,控制台中都会弹出:
(node:17644) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined at Object.module.exports.run (C:\Users\myname\Desktop\LDU_Bot\cmds\report.js:5:35)
这是代码:
module.exports.run = async (Discord, fs, xp, bot, message, args) =>{
var staffID = "503409681876779032";
//console.log(`Staff: ${staff}`);
for (let i = 0; i < message.guild.members.size; i++) {
if (message.guild.members[i].roles.has(staff)) { //Error on this line
message.guild.members[i].user.send(`This is a test for our report command. DM the bot dev if you are not staff.`);
}
}
}
module.exports.help = {
name: "report"
}
错误表明问题出在第 5 行,但我仍然没有看到问题,因为从我所看到的来看,这遵循文档。
最佳答案
var staffID = "503409681876779032";
message.guild.members.forEach( (member) => {
if(member.roles.has(staffID)) {
member.send(`This is a test for our report command. DM the bot dev if you are not staff.`);
}
})
关于javascript - 如何通过 DM 向具有特定角色的所有成员发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54609231/