'Discord bot won't join the music channel a user is in
I was trying a tutorial yesterday to make a personal music bot but the code to make the bot join a channel that the user is in upon using the "play" command isn't working. Since yesterday, it only went up to the message to be printed when the user isn't in a voice channel (even if they are). Today, it wasn't recognising that part of the code at all, only going to stating the missing permissions or prompting the user to enter a song (and would do nothing if they do so). Then, it started giving an error stating that joinChannel() is not a function, and now it's all the way back to square one (not joining the VC) despite the overall code having barely any changes. The code I have right now is as follows:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'Joins a Voice channel and plays media from YouTube',
async execute(messageCreate, args) {
voiceChannel = messageCreate.member.voice.channel;
if(!voiceChannel) return messageCreate.channel.send('You need to get in a Voice channel first');
const permissions = voiceChannel.permissionsFor(messageCreate.client.user);
if(!permissions.has('CONNECT')) return messageCreate.channel.send('You are missing the required permissions');
if(!permissions.has('SPEAK')) return messageCreate.channel.send('You are missing the required permissions');
if(!args.length) return messageCreate.channel.send('Please enter the second argument');
const connection = await voiceChannel.join();
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await videoFinder(args.join(' '));
if(video) {
const stream = ytdl(video.url, {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 1})
.on('finish', () => {
voiceChannel.leave();
});
await messageCreate.reply(`:thumbsup: Got it! Now Playing ***${video.title}***`)
} else {
messageCreate.channel.send('No results found');
}
}
}
I'm new to JavaScript and Discord.js so any help is appreciated.
EDIT: The error I'm getting is:
const connection = await voiceChannel.join(); ^
TypeError: voiceChannel.join is not a function
(Full Error log):
C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\commands\play.js:16 const connection = await voiceChannel.join(); ^
TypeError: voiceChannel.join is not a function at Object.execute (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\commands\play.js:16:47) at Client. (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\main.js:37:37) at Client.emit (node:events:394:28) at MessageCreateAction.handle (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:23:14) at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:345:31) at WebSocketShard.onPacket (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22) at WebSocketShard.onMessage (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10) at WebSocket.onMessage (C:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\node_modules\ws\lib\event-target.js:132:16) at WebSocket.emit (node:events:394:28)
When I'm not in a voice channel, it outputs "You need to get in a Voice channel first" even if I join it afterwards. However, when I'm already in the channel and run node .
again, it will output "Please enter the second argument" instead when running the play command and then give the above error. If I enter a second argument with the command (e.g: !play EDM), it doesn't give any response until I run node .
again.
The if statement to call the play command in the main.js file is:
if(command === 'play' || 'p'){
client.commands.get('play').execute(messageCreate, args);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|