Discord.js v12 角色查询

时间:2021-06-12 05:42:33

标签: javascript node.js arrays discord discord.js

我正在尝试开发一个不和谐的机器人,但我遇到了一些问题。

我的机器人的目的是当我的服务器上的员工加入名为“XXXXXX”的语音频道时,向文本频道“YYYYYY”发送消息

我的代码块:


client.on('voiceStateUpdate', (oldMember, newMember) => {
    let newUserChannel = newMember.channelID;
    let oldUserChannel = oldMember.channelID;
    if(newUserChannel === "838819904798457928" && newMember.roles.cache.has === "839532611948511272")
    { 
        console.log(" example entered "+newUserChannel);
    }
    else{
        console.log("Left the channel");
    }
});

我的错误代码:

if(newUserChannel === "838819904798457928" && newMember.roles.cache.has === "839532611948511272")
                                                                      ^

TypeError: Cannot read property 'cache' of undefined
Press any key to continue . . .

1 个答案:

答案 0 :(得分:0)

voiceStateUpdate 事件不返回 GuildMember,而是返回 VoiceState

因此,为了获得成员角色,您必须首先从 VoiceState 中获得成员

client.on('voiceStateUpdate', (oldState, newState) => {
    console.log(newState.member.roles.cache);
});