无法读取未定义的属性“成员”(discord.js)
Posted
技术标签:
【中文标题】无法读取未定义的属性“成员”(discord.js)【英文标题】:Cannot read property 'members' of undefined (discord.js) 【发布时间】:2021-04-16 14:18:03 【问题描述】:我正在尝试创建一个 DM 验证系统,如果成员将 DM 发送到机器人,他就会被接受。
....
const respectrumServer = client.guilds.cache.get('782347082362912768');
client.on('message', message =>
if (message.author.equals(client.user)) return;
var authorid = message.author.id;
if (message.toString().toLowerCase() === "lonely guy is the best")
if (message.channel.type == "dm")
if(respectrumServer.members.cache.has(authorid))
//error is here ^
var verifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '782348009362161714');
var notverifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '797218539563122688');
member.roles.add(verifiedrole);
member.roles.remove(notverifiedrole);
authorid.send('You have been verified');
else
authorid.send('You are not a member of Respectrum');
else
message.channel.send('I know, right?');
else
return;
);
我改变了很多东西,比如 message.author
到 message.author.id
和 members.cache.find(authorid)
到 members.cache.has(authorid)
我不知道是什么问题。
【问题讨论】:
【参考方案1】:这是因为您试图在机器人准备好之前获取respectrumServer
服务器。
当您收到一条新消息时,请改用以下代码来定义它:
client.on('message', async message =>
if (message.author.equals(client.user)) return;
const respectrumServer = client.guilds.cache.get('782347082362912768');
var authorid = message.author.id;
if (message.toString().toLowerCase() === "lonely guy is the best")
if (message.channel.type == "dm")
const member = await respectrumServer.members.fetch(authorid).catch(() => );
if(member)
//error is here ^
var verifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '782348009362161714');
var notverifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '797218539563122688');
member.roles.add(verifiedrole);
member.roles.remove(notverifiedrole);
authorid.send('You have been verified');
else
authorid.send('You are not a member of Respectrum');
else
message.channel.send('I know, right?');
else
return;
);
【讨论】:
Tysm 但是,现在当我发送消息时,它说'你不是尊重的成员',即使我是成员(我也修复了author.id
问题)
没问题,请将我的答案标记为正确,以便关闭问题以上是关于无法读取未定义的属性“成员”(discord.js)的主要内容,如果未能解决你的问题,请参考以下文章
Discord.js 的欢迎消息 - “无法读取未定义的属性 'get'”