DiscordJS(14.0)TypeError:无法读取未定义的属性(正在读取'缓存')



我是JS(和DiscordJS(的新手-我希望弄清楚为什么这会破坏我的机器人。

其目的是产生5%的机会回复所有具有指定角色ID的用户。

机器人程序正确打开,但一旦在服务器中检测到或更改任何内容,它就会崩溃:TypeError:无法读取未定义(读取"缓存"(的属性

非常感谢您的任何建议!

client.on("messageCreate", (message) => {
let args = message.content.trim().split(" ");
if (message.author.roles.cache.has("641904594041045002")) {
if (message.author.bot) return;
if (message.author.id === "95784650613456896") return;
const responses = [
"Reply1",
"Reply2",
"Reply3",
"Reply4",
];
const reply = responses[Math.floor(Math.random() * responses.length)];
if (Math.random() < 0.05) {
//0.50 = 50% chance
message.reply(`${reply}`);
}
}
});

编辑:更改if (message.author.roles.cache.has("641904594041045002")) {if (message.member.roles.cache.has("641904594041045002")) {修复了这个问题,直到用户加入语音通道——这会导致机器人再次崩溃,并出现相同的TypeError。

第2版:

client.on("messageCreate", async (message) => { //now async
let args = message.content.trim().split(" ");
const member = await message.guild.members.fetch(message.author.id) //fetch author ID
if(message.member.roles.cache.some(r=>["bois"].includes(r.name)) ); {
if (message.author.bot) return;
if (message.author.id === "95784650613456896") return;
const responses = [
"Reply1",
"Reply2",
"Reply3",
"Reply4",
];
const reply = responses[Math.floor(Math.random() * responses.length)];
if (Math.random() < 0.99) {
//0.50 = 50% chance
message.reply(`${reply}`);
}
}
});

这会产生不同的崩溃(当用户加入/离开语音呼叫时(:DiscordAPIError[10013]:未知用户

应该使用message.member,而不是返回不具有roles属性的User对象的message.author

if (message.member.roles.cache.has("641904594041045002")) {

你可能需要获取这样的成员:

const member = await message.guild.members.fetch(message.author)
if (member.roles.cache.has("641904594041045002")) {

最新更新