Search code examples
javascriptnode.jsdiscord.js

discord.js v14 - Move member to voice channel


via the slashcommand /crociera I'm trying to move the mentioned user from the voice channel he is on to a list of voice channels every 2 sec. in debug I get this error:

CANALE: <#729013398368419891>
Errore durante lo spostamento dell'UTENTE:<@836003700588937268> nel CANALE: <#729013398368419891> con errore: TypeError: Cannot read properties of undefined (reading 'setChannel')
CANALE: <#729016216894832641>
Errore durante lo spostamento dell'UTENTE:<@836003700588937268> nel CANALE: <#729016216894832641> con errore: TypeError: Cannot read properties of undefined (reading 'setChannel')
CANALE: <#892746358061146152>
Errore durante lo spostamento dell'UTENTE:<@836003700588937268> nel CANALE: <#892746358061146152> con errore: TypeError: Cannot read properties of undefined (reading 'setChannel')
CANALE: <#729016744605384784>
Errore durante lo spostamento dell'UTENTE:<@836003700588937268> nel CANALE: <#729016744605384784> con errore: TypeError: Cannot read properties of undefined (reading 'setChannel')

di seguito il mio codice:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { Permissions, PermissionsBitField} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('crociera')
        .setDescription('Sposta un utente nei canali vocali specificati')
        .addUserOption(option => 
            option.setName('utente')
                .setDescription('L\'utente da spostare')
                .setRequired(true)
        ),
    
    async execute(interaction) {
        const userToMove = interaction.options.getUser('utente');
        await interaction.reply(`Ciao, ${userToMove}!`);
        //const userToMove = interaction.options.getUser('userToMove');
        console.log(`LOG DI USER ${userToMove}`)
        
        // Controllo delle autorizzazioni dell'utente
        if (!interaction.member.permissions.has(PermissionsBitField.Flags.MoveMembers)) {
            return interaction.editReply('Non hai il permesso di spostare membri.');
        }
         
        if (!userToMove) {
            await interaction.editReply('Error: Didn\'t specify member.');          
            return;
        }
        //if (!userToMove.voice.channel) {
        //    await interaction.editReply('Error: Mentioned member is not in a Voice Channel.');
        //    return;
        //}
        
        //if (!(interaction.member.voice.channel)) {
        //    return await interaction.reply({ content: "You need to join a Voice-Channel first.", ephemeral: true });
        //}
        //if (!(userToMove.voice.channelId)) {
        //    return await interaction.reply({ content: "The Member is currently not in a Voice-Channel.", ephemeral: true });
        //}
        

    
        // Ottiene i canali vocali di destinazione
        const channelIds = ['729013398368419891', '729016216894832641', '892746358061146152', '729016744605384784'];
        const channels = channelIds.map(id => interaction.guild.channels.cache.get(id)).filter(Boolean);
        console.log(`CANALI: ${channels}`)
        if (channels.length === 0) {
            return interaction.editReply('Non sono stati trovati i canali vocali di destinazione.');
        }

        // Sposta l'utente nei canali vocali
        for (const channel of channels) {
            try {
                console.log(`CANALE: ${channel}`)
                await userToMove.voice.setChannel(channel);
                await new Promise(resolve => setTimeout(resolve, 2000)); // Attendere 2 secondi prima di spostarsi di nuovo
            } catch (error) {
                console.error(`Errore durante lo spostamento dell'UTENTE:${userToMove} nel CANALE: ${channel} con errore: ${error}`);
            }
        }

        await interaction.editReply(`L'utente ${userToMove} è stato spostato nei canali vocali specificati.`);
    },
};


I tried every way known to me but I can't fix it, can you help me? I'm not very familiar with discord.js. i see other 3d but not resolve my problem. Thank you


Solution

  • Users don't have voice states, only GuildMembers have. interaction.options.getUser('utente') returns a User. You will need to change it to interaction.options.getMember('utente') to receive a GuildMember.