无法获得自动断开连接漫游器以断开语音用户

时间:2020-10-18 15:58:01

标签: javascript node.js discord discord.js

我目前正在尝试运行一个不和谐的bot,该僵尸程序会在特定用户尝试输入时将其断开与服务器上afk通道的连接。

我承认我不是很擅长编码,我所研究的大部分内容都与我浏览过的其他帮助部分结合在一起。

我已经设法使漫游器达到一种状态,即当用户直接进入afk通道时,它将断开用户与用户的连接,但我无法弄清楚当用户进入afk通道时如何执行该操作用户从一个频道移动到afk频道。

我遇到问题的部分在这里:

client.on("voiceStateUpdate", (oldMember, newMember) => {
  const newUserChannel = newMember.voiceChannel;
  const oldUserChannel = oldMember.voiceChannel;

  if (
    oldUserChannel === undefined &&
    newUserChannel.name === "afk" &&
    data.find(
      guild =>
        guild.id === newMember.guild.id &&
        guild.blacklist.find(id => newMember.id === id)
    )
  ) {
    newMember.setVoiceChannel(null);
  }

我尝试将oldUserChannel === undefined &&设置为特定频道,并且!==未定义,但是===未定义以外的任何其他信息给我带来TypeError:无法读取未定义的属性“名称”。

我还尝试了用户newUserChannel.id而不是.name,但这只是将'name'更改为'id'而给了我同样的错误。

我真的很陌生,所以我在努力思考如何解决错误。

1 个答案:

答案 0 :(得分:0)

如果将用户从另一个频道移至AFK频道,则oldUserChannel将被定义为VoiceChannel

您应检查newUserChannel是否存在(如果是这种情况,我们知道用户仍与VoiceChannel连接,并检查频道的ID /名称是否等于“ AFK” /“ Channel” ID”。

注意:建议您改用Discord JS v12。


client.on("voiceStateUpdate", (oldMember, newMember) => {
    if (!newMember.voiceChannel) return false; // The GuildMember is not in a VoiceChannel.
    if (newMember.voiceChannel.guild.id !== "GUILD ID") return false; // Making sure this works in a certain Guild. I don't think you want this to happen in every Guild your bot is in which has a VoiceChannel called "AFK". 

    if (newMember.voiceChannel.name == "AFK" && newMember.id == "USER ID") { // Checking if the channel's name is AFK and making sure the user is the one you want to disconnect.
        newMember.setVoiceChannel(null);
        // I'm not sure if Discord JS v11 has a method of disconnecting the user from a VoiceChannel.
        // But setting the voice channel to null will work.
    };
});