const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: "plays music in voice chat",
async execute(message, args, Discord){
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('You need to be in a voice channel to listen to music!');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.reply("You don't have the correct permissions to request music!");
if (!permissions.has('SPEAK')) return message.reply("You don't have the correct permissions to request music!");
if (!args.length) return message.channel.send("You Can't Request for no Music Silly >_<");
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 message.reply(`:Thumbsup: Now Playing >>>${video.title}<<<`)
} else {
message.reply('No Video Results Found :(');
}
}
}
My Error message is const connection = await voiceChannel.join();
TypeError: voiceChannel.join is not a function
If any of you have a suggestion that may work please feel free to edit it. It can also only detect if someone is in voice chat at start up of the bot, so if your in voice chat at start of bot it sends the in voice chat message while your in and out of voice chat
While transitioning from discord.js v12 - v13 we have encountered a breaking change that is switching to @discordjs/voice a seperate library for voice to play with discord.js, now as suggested from the comment here I see you're using the most latest version of discord.js where the VoiceChannel#join method no longer exists. You have to propose the following changes:
@discordjs/voice using npm install @discordjs/voicevoiceConnection, and further create an AudioPlayer instance and go ahead and call the AudioPlayer#subscribe method to this player, you would have to do some base additions for the same which are exhibited below:const {
joinVoiceChannel,
createAudioPlayer,
createAudioResource
} = require('@discordjs/voice');
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
const stream = ytdl(video.url, {
filter: "audioonly"
});
const player = createAudioPlayer();
const resource = createAudioResource(stream);
async function play() {
await player.play(resource);
connection.subscribe(player);
}
They just made it overcomplex for beginners to comprehend so don't worry! If you feel like you're over- exhausting yourselves you can always downgrade to a lower version like so:
npm uninstall discord.js
npm install discord.js@12.5.3
Welcome to stackoverflow 😄