尝试在不使用用户的情况下获取语音通道的 id

时间:2021-02-03 14:14:59

标签: javascript discord.js

即使用户不在 VC 中,我也希望我的机器人离开 vc。这需要获取 id,但这就是我遇到问题的地方。每次我尝试获取 id 时都会收到以下错误!

const voiceChannel = message.member.voice.channel;
const voiceid = voiceChannel.id;
let channel = message.guild.channels.cache.get(voiceid);

错误:Cannot read property 'id' of null

1 个答案:

答案 0 :(得分:0)

voiceChannel 未定义,这就是没有 id 属性的原因。 在代码中添加如果消息成员语音通道未定义则返回消息到通道

const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send("No Voice Channel!")
const voiceid = voiceChannel.id;
let channel = message.guild.channels.cache.get(voiceid);