Hey everyone! I’m working on a Discord bot using Node.js and discord.js library. I want to create a command that moves a specific user to the AFK voice channel when someone types a command in chat.
Basically, when anyone types !move in text chat, the bot should find user “John” and move him to the AFK voice channel (but only if he’s currently in a voice channel).
Here’s what I have so far but it’s not working:
const Discord = require("discord.js");
const bot = new Discord.Client();
const commandPrefix = "!"
bot.on('ready', () => {
console.log(`Bot is online. ${bot.user.tag} has started!`);
bot.user.setStatus("online");
bot.user.setActivity('Moderating Server', { type: 'WATCHING' });
});
bot.on('message', message => {
if (message.content === commandPrefix + 'move') {
message.reply('Moving John to AFK channel!');
var targetUser = message.guild.members.find("123456789012345678", USER_ID);
await targetUser.setVoiceChannel("987654321098765432");
}
})
bot.login('my_bot_token');
I’m getting errors and the user isn’t being moved. What am I doing wrong here? Any help would be appreciated!
You’re using await without making your function async. Change it to bot.on('message', async message => {. Your members.find() syntax is wrong too - use message.guild.members.cache.get('USER_ID') instead. Double-check that your user ID and channel ID are correct strings. Add error handling to make sure the user exists and is actually in a voice channel before trying to move them. I ran into the same problems when I started with discord.js - these fixes sorted out the movement issues.
Hey! You’re using outdated methods. Switch to message.guild.members.cache.get('USER_ID'). Also, drop the await since your function isn’t async. Double-check your user and channel IDs too. Good luck!
Had the same problem with my first Discord bot. You’re mixing old and new discord.js syntax - members.find() with that format doesn’t work anymore. Use message.guild.members.cache.get('USER_ID') instead. Your function needs to be async if you’re using await. Here’s what everyone else missed: check your bot’s permissions. It needs “Move Members” permission and a role higher than the target user’s highest role. I wasted hours on this exact bug before figuring out it was permissions. Also, wrap your code in try-catch because setVoiceChannel() throws an error if the user isn’t in voice. Check that targetUser.voice.channel exists before trying to move them.