0

Is there any way to have a discord js bot that can move users to different voice channels? Everything I've seen online seems outdated as I keep getting errors. Right now I'm just hard coding it to a certain channel to test out if it will work, but it doesn't.

client.on('message', message => {
    console.log('' + message.author + ': ' + message.content);

    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(" ");
    const c = args;
    for(var i = 0; i < c.length; i++) {
    c[i] = c[i].toLowerCase();
    }

    if (c[0] === 'movevoice'){
        if(!message.mentions.members.first()){
            message.author.voice.setChannel(762760164364648479)
        }else{
            const member = message.mentions.members.first();
            member.voice.setChannel(762760164364648479);
        }
    }
  • 1
    The channel ID has to be a string (`'762760164364648479'`), not a number (`762760164364648479`) – Lioness100 Oct 06 '20 at 21:38
  • It gives me the error "TypeError: cannot read property 'setChannel' of undefined" – Goldenboi Oct 07 '20 at 00:11
  • 2
    Change `message.author.voice.setChannel()` to `message.member.voice.setChannel()`. [More information](https://stackoverflow.com/questions/63979076/what-is-the-difference-between-a-user-and-a-guildmember-in-discord-js/63979077#63979077) – Lioness100 Oct 07 '20 at 00:16
  • I managed to get it working. Thank you so much! – Goldenboi Oct 07 '20 at 00:25

1 Answers1

1

I have re-developed a code to make it work correctly with some additions that you might find useful, the code below :

client.on('message', message => {
let args = message.content.trim().split(/ +/g)
if(args[0].toLowerCase() === prefix + 'movevoice') {

if(!message.guild)return;
if(message.author.bot)return;

let u = message.author.id
if(args[1]) {
if(!message.guild.members.cache.get(`${args[1]}`.replace(/<@/g, '').replace(/!/g, '').replace(/>/g, ''))return;
u = `${args[1]}`.replace(/<@/g, '').replace(/!/g, '').replace(/>/g, '')
}

message.guild.members.cache.get(u).voice.setChannel('762760164364648479').catch(error=>{return;})

}
})
Z'aroDev
  • 116
  • 6