I'm making a Discord music Bot and I'm having trouble with an error saying
TypeError: Cannot read property 'url' of undefined
I tried console logging it and it showed me the url
, so I don't understand what is the problem with my code.
Here's my code:
//musicBOT
const Discord = require('discord.js');
const client = new Discord.Client();
const ytdl = require('ytdl-core');
const mcPrefix = '.';
const queue = new Map();
client.on('ready', () => console.log('Music bot ready!'));
client.on('message', async message => {
if(message.author.bot) return;
if(!message.content.startsWith(mcPrefix)) return;
const args = message.content.substring(mcPrefix.length).split(" ");
const serverQueue = queue.get(message.guild.id);
if(message.content.startsWith(`${mcPrefix}play`)) {
const voiceChannel = message.member.voice.channel;
if(!voiceChannel) return message.channel.send("Hang-szobában kell lenned zenelejátszáshoz.");
const permissions = voiceChannel.permissionsFor(message.client.user);
if(!permissions.has('CONNECT')) return message.channel.send("Nincs jogosultságom csatlakozni a hangszobához.");
if(!permissions.has('SPEAK')) return message.channel.send("Nincs jogosultságom megszólalni ebben a hangszobában.");
const songInfo = await ytdl.getInfo(args[1])
const song = {
title: songInfo.title,
url: songInfo.videoDetails.video_url
}
if(!serverQueue) {
const queueConstruct = {
textChannel: message.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
playing: true
}
queue.set(message.guild.id, queueConstruct)
queueConstruct.songs.push(song)
try{
var connection = await voiceChannel.join();
message.channel.send(`${song.title} lejátszása.`)
queueConstruct.connection = connection
play(message.guild, queueConstruct.songs[0])
}catch(e){
console.log(`Hiba csatlakozás közben itt: ${e}`);
queue.delete(message.guild.id)
return message.channel.send(`Hiba volt a csatlakozás közben itt: ${e}`)
}
} else{
serverQueue.songs.push(song)
return message.channel.send(`**${song.title}** hozzáadva a lejátszási listához.`)
}
return undefined
}else if (message.content.startsWith(`${mcPrefix}stop`)) {
if(!message.member.voice.channel) return message.channel.send("Hang-szobában kell lenned ahhoz, hogy leállítsd a zenét.")
if(!serverQueue) return message.channel.send("There is nothing playing")
serverQueue.songs= []
serverQueue.connection.dispatcher.end()
message.channel.send("Sikeresen megálltottad a zenét.")
return undefined
}else if(message.content.startsWith(`${mcPrefix}skip`)){
if(!message.member.voice.channel) return message.channel.send("Hang-szobában kell lenned a skip parancshoz.")
if(!serverQueue) return message.channel.send("There is nothing playing")
serverQueue.connection.dispatcher.end()
message.channel.send("Zene továbbléptetve.")
message.channel.send(`${song.title} játszása.`)
return undefined
}
function play(guild, song) {
const serverQueue = queue.get(guild.id)
if(!serverQueue.songs){
serverQueue.voiceChannel.leave()
queue.delete(guild.id)
return
}
const dispatcher = serverQueue.connection.play(ytdl(song.url))
.on('finish', () => {
serverQueue.songs.shift()
play(guild, serverQueue.songs[0])
})
.on('error', error => {
console.log(error)
})
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5)
}
})
//musicBOT
and here is the full error:
const dispatcher = serverQueue.connection.play(ytdl(songInfo.url))
^
TypeError: Cannot read property 'url' of undefined
at play (C:\Users\Levi\Desktop\Discord BOT Javascript\bot.js:97:70)
at StreamDispatcher.<anonymous> (C:\Users\Levi\Desktop\Discord BOT Javascript\bot.js:100:17)
at StreamDispatcher.emit (node:events:388:22)
at finish (node:internal/streams/writable:734:10)
at processTicksAndRejections (node:internal/process/task_queues:80:21)
I started searching on the internet but found nothing about it, I guess my basic javascript knowledge is just not enough.
The empty songs
array checking in play
function is incorrect:
// This "if" clause will never be run, voice channel won't leave when there's no song
if(!serverQueue.songs){
serverQueue.voiceChannel.leave()
...
}
Fix:
if(serverQueue.songs.length === 0){
...
}