Discord.js "voiceStateUpdate" 调用了两次

Discord.js "voiceStateUpdate" called twice

我正在尝试创建一个使用“Slash”命令的临时频道。我快到了,但是在创建第二个频道时出现错误,因为“voiceStateUpdate”被调用了两次。

else if (commandName === "createchannel") {
   var channelName = options.getString("channelname");
   var rolesThatCanJoin = options.getRole("roles");
   var state = options.getString("state");
   var channel = interection.guild.channels.create(channelName, {
        type: "GUILD_VOICE"
        }).then((channel) => { 
        channel.setParent("670691620844470320")
        createdChannelID = channel.id;

        interection.reply({
          content: `**Channel named:** ${channelName}\n**Role that can join:** ${rolesThatCanJoin}\n**State:** ${state}`,
                ephemeral: true,
                })

        client.on('voiceStateUpdate', (oldState, newState) => {
            var newChannelID = newState.channelId;
                
            if (newChannelID === createdChannelID) {
              console.log("User has joined voice channel with id " + newChannelID);
            } 
            else {
               console.log("User has left");
               channel.delete()
            }
         });
      });
    }

控制台:

User has joined voice channel with id 936252040865284156
User has left
User has joined voice channel with id 936252068568641576
User has joined voice channel with id 936252068568641576
User has left
User has left

Error:
DiscordAPIError: Unknown Channel

似乎每次命令运行时,您都在再次调用 client.on('voiceStateUpdate'...,这会添加另一个侦听器。这导致您多次收到相同的事件。我建议您将 client.on('voiceStateUpdate'... 代码移动到另一个位置,它会被调用一次,就像您处理其他事件如 client.on('messageCreate'...client.on('interactionCreate'...

的方式一样