S
S
SasukeUchiha2022-04-21 17:04:48
JavaScript
SasukeUchiha, 2022-04-21 17:04:48

How to solve DiscordAPIError: Target user is not connected to voice error?

I'm making my bot, at the moment I settled on private channels
And I ran into a problem when a participant leaves the "Create private" channel, when he has not yet been thrown into his private channel, the console gives the error "DiscordAPIError: Target user is not connected to voice ." after which the bot crashes.
How can this problem be solved?

Here is the code:

const { VoiceState } = require('discord.js');
const client = require('../index');

client.on('voiceStateUpdate', async (oldState, newState) => {
  const { member, guild } = newState;
  const oldChannel = oldState.channel;
  const newChannel = newState.channel;
  const joinToCreate = "964233496061157436"

  if(oldChannel !== newChannel && newChannel && newChannel.id === joinToCreate) {
    const voiceChannel = await guild.channels.create(member.user.tag, {
      type: "GUILD_VOICE",
      parent: newChannel.parent,
      permissionOverwrites: [
        {id: member.id, allow: ["CONNECT", "MANAGE_CHANNELS", "MOVE_MEMBERS"]},
        {id: guild.id, allow: ["CONNECT"]}
      ]
    });

    client.voiceGenerator.set(member.id, voiceChannel.id);
    await newChannel.permissionOverwrites.edit(member, {CONNECT: false});
    setTimeout(() => newChannel.permissionOverwrites.delete(member), 30 * 1000);

    return setTimeout(() => member.voice.setChannel(voiceChannel), 500);
  }

  const ownedChannel = client.voiceGenerator.get(member.id)
 
  if(ownedChannel && oldChannel.id == ownedChannel && (!newChannel || newChannel.id !== ownedChannel)) {
    client.voiceGenerator.set(member.id, null);
    oldChannel.delete().catch(() => {})
  }
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question