【问题标题】:error: "UnhandledPromiseRejectionWarning: Error: No video id found"错误:“UnhandledPromiseRejectionWarning:错误:未找到视频 ID”
【发布时间】:2021-08-02 00:48:20
【问题描述】:

我目前正在创建我的第一个 discordjs 机器人并遇到了一些问题。我现在想添加播放音乐的功能,但是当我在服务器中为我的机器人使用播放命令时,我总是收到错误消息:“UnhandledPromiseRejectionWarning:错误:未找到视频 ID”。 我已经用谷歌搜索了它,但没有任何答案可以帮助我...... 代码如下:

async function execute(message, serverQueue, args) {

  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") || !permissions.has("SPEAK")) {
    return message.channel.send(
      "I need the permissions to join and speak in your voice channel!"
    );
  }

  const songInfo = ytdl.getInfo(args);
  const song = {
        title: songInfo.videoDetails.title,
        url: songInfo.videoDetails.video_url,
   };

  if (!serverQueue) {
    const queueContruct = {
      textChannel: message.channel,
      voiceChannel: voiceChannel,
      connection: null,
      songs: [],
      volume: 5,
      playing: true
    };

    queue.set(message.guild.id, queueContruct);

    queueContruct.songs.push(song);

    try {
      var connection = voiceChannel.join();
      queueContruct.connection = connection;
      play(message.guild, queueContruct.songs[0]);
    } catch (err) {
      console.log(err);
      queue.delete(message.guild.id);
      return message.channel.send(err);
    }
  } else {
    serverQueue.songs.push(song);
    return message.channel.send(`${song.title} has been added to the queue!`);
  }
}

function skip(message, serverQueue) {
  if (!message.member.voice.channel)
    return message.channel.send(
      "You have to be in a voice channel to stop the music!"
    );
  if (!serverQueue)
    return message.channel.send("There is no song that I could skip!");
  serverQueue.connection.dispatcher.end();
}

function stop(message, serverQueue) {
  if (!message.member.voice.channel)
    return message.channel.send(
      "You have to be in a voice channel to stop the music!"
    );
    
  if (!serverQueue)
    return message.channel.send("There is no song that I could stop!");
    
  serverQueue.songs = [];
  serverQueue.connection.dispatcher.end();
}

function play(guild, song) {
  const serverQueue = queue.get(guild.id);
  if (!song) {
    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.error(error));
  dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}

【问题讨论】:

  • ytdl.getInfo() 需要一个字符串 - 你传递了一个数组
  • 对于未来与 ytdl 相关的问题,请添加 ytdl 标签

标签: node.js discord.js ytdl


【解决方案1】:

该错误意味着您的代码中某处 Promise 被拒绝,但您尚未指定对其进行错误处理。我不确定你的哪个函数调用返回了 Promise,但是当你需要处理错误时,你需要在 Promise 中添加 .catch() 调用。

【讨论】:

    猜你喜欢
    • 2019-06-09
    • 2021-12-10
    • 1970-01-01
    • 2019-11-02
    • 2021-10-03
    • 2019-11-11
    • 1970-01-01
    • 2019-11-02
    • 2021-04-17
    相关资源
    最近更新 更多