YTDL-Core Error: input stream: No video id found: l

1.8k Views Asked by At

I am making a discord bot and i use the following code while having all the correct npm stuff installed and ffmpeg working. This bot was working earlier today and i messed it up so i reverted to the old code and now it isnt working.

const Discord = require('discord.js');
const api = require("imageapi.js");
const client = new Discord.Client();
const YouTube = require('simple-youtube-api')
const ytdl = require('ytdl-core')
const prefix = '!';


client.once('ready', () => {
    console.log("Online!");
    client.user.setActivity('!help');
});

client.on('message', async message => {
  if(message.author.bot) return
  if(!message.content.startsWith(prefix)) return

  const args = message.content.substring(prefix.length).split("")

  if(message.content.startsWith(`${prefix}play`)) {
    const voiceChannel = message.member.voice.channel
    if(!voiceChannel) return message.channel.send("You need to be in a voice channel to play music")
    const permissions = voiceChannel.permissionsFor(message.client.user)
    if(!permissions.has('CONNECT')) return message.channel.send(`I don\'t have permission to connect`)
    if(!permissions.has('SPEAK')) return message.channel.send(`I don\'t have permission to speak`)
     
    try {
        var connection = await voiceChannel.join()
    } catch(error){
        console.log("error")
        return message.channel.send(`There was a error when connection to the voice channel`)
    }

    const dispatcher = connection.play(ytdl(args[1]))
    .on('finish', () => {
      voiceChannel.leave()
    })
    .on('error', error => {
      console.log(error)
    })
    dispatcher.setVolumeLogarithmic(5 / 5)
  } else if (message.content.startsWith(`${prefix}stop`)) {
    if(!message.member.voice.channel) return message.channel.send("You need to be in a voice channel to stop the music")
    message.member.voice.channel.leave()
    return undefined
  }
})```
1

There are 1 best solutions below

0
On

This means that args[1] is not a valid youtube URL or ID. When you split the message:

const args = message.content.substring(prefix.length).split("")

You split by '' instead of ' '. This is the difference of splitting by every character vs every space.

const str = 'Hello World';

console.log(str.split(''));
console.log(str.split(' '));

So, you probably called ytdl('w') as in www.youtube.com/.... Even though you fixed this problem, you should add error handling to make sure:

  1. args[1] exists
  2. args[1] is a valid ID
if (!args[1]) return message.channel.send('...');

try {
  const audio = ytdl(args[1]);
} catch (err) {
  return message.channel.send('...');
}