【问题标题】:YTDL-Core Error: input stream: No video id found: lYTDL-Core 错误:输入流:未找到视频 ID:l
【发布时间】:2021-08-03 07:19:14
【问题描述】:

我正在制作一个不和谐的机器人,我使用以下代码,同时安装了所有正确的 npm 东西并且 ffmpeg 工作。这个机器人今天早些时候工作,我把它搞砸了,所以我恢复到旧代码,现在它不工作了。

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
  }
})```

【问题讨论】:

    标签: javascript discord.js ytdl


    【解决方案1】:

    这意味着args[1] 不是有效的 youtube 网址或 ID。拆分消息时:

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

    您按'' 而不是' ' 拆分。这就是每个字符和每个空格的区别。

    const str = 'Hello World';
    
    console.log(str.split(''));
    console.log(str.split(' '));

    所以,您可能调用了ytdl('w'),就像在www.youtube.com/... 中一样。即使您解决了这个问题,您也应该添加错误处理以确保:

    1. args[1] 存在
    2. args[1] 是一个有效的 ID
    if (!args[1]) return message.channel.send('...');
    
    try {
      const audio = ytdl(args[1]);
    } catch (err) {
      return message.channel.send('...');
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 2021-03-18
      • 2020-11-21
      • 2016-12-13
      • 2019-09-23
      • 2021-08-02
      • 2019-06-09
      • 2021-09-07
      • 1970-01-01
      相关资源
      最近更新 更多