【问题标题】:Discord.js music bot with queue doesn't work带有队列的 Discord.js 音乐机器人不起作用
【发布时间】:2020-09-23 05:07:46
【问题描述】:

我正在尝试制作带有队列的音乐 Discord Bot。目前,播放命令有效,我可以将音乐添加到队列中(使用我的播放列表命令显示)。问题是当第一首音乐结束时,机器人完全停止并且不播放下一首歌曲(它不会断开连接并且看起来它正在播放一些东西)。
我使用 Discord.js v12、ffmpeg-static 和 Youtube API。

if (command === "play" || command === "p") {

    const args = message.content.split(" ");
    const searchString = args.slice(1).join(" ");
    if (!args[1]) {
      return message.channel.send('Il faut spécifier une URL !')
        .catch(console.error);
    }
    const url = args[1] ? args[1].replace(/<(.+)>/g, "$1") : "";
    const serverQueue = queue.get(message.guild.id);
    var voiceChannel = message.member.voice.channel;
    if (!voiceChannel) return message.channel.send("Tu dois être dans un salon vocal pour utiliser cette commande !");
    const permissions = voiceChannel.permissionsFor(message.client.user);
    if (!permissions.has("CONNECT")) {
      return message.channel.send("J'ai besoin de la permission **`CONNECT`** pour cette commande");
    }
    if (!permissions.has("SPEAK")) {
      return message.channel.send("J'ai besoin de la permission **`SPEAK`** pour parler");
    }

    if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
      const playlist = await youtube.getPlaylist(url);
      const videos = await playlist.getVideos();
      for (const video of Object.values(videos)) {
        const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
        await handleVideo(video2, message, voiceChannel, true); // eslint-disable-line no-await-in-loop
      }
      return message.channel.send(`:white_check_mark:  **|**  Playlist: **\`${playlist.title}\`** a été ajouté à la playlist !`);
    } else {
      try {
        var video = await youtube.getVideo(url);
      } catch (error) {
        try {
          var videos = await youtube.searchVideos(searchString, 10);
          let index = 0;
          // eslint-disable-next-line max-depth
          try {

          } catch (err) {
            console.error(err);
            return message.channel.send("Annulation de la commande...");
          }
          const videoIndex = parseInt(1);
          var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
        } catch (err) {
          console.error(err);
          return message.channel.send("????  **|**  Je n'obtiens aucun résultat :pensive:");
        }
      }
      return handleVideo(video, message, voiceChannel);
    }
}


async function handleVideo(video, message, voiceChannel, playlist = false) {
  const serverQueue = queue.get(message.guild.id);
  const song = {
    id: video.id,
    title: Util.escapeMarkdown(video.title),
    url: `https://www.youtube.com/watch?v=${video.id}`
  };
  console.log(song.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();
      queueConstruct.connection = connection;
      play(message.guild, queueConstruct.songs[0]);
    } catch (error) {
      console.error(`Je ne peux pas rejoindre le salon vocal : ${error}`);
      queue.delete(message.guild.id);
      return message.channel.send(`Je ne peux pas rejoindre le salon vocal : **\`${error}\`**`);
    }
  } else {
    serverQueue.songs.push(song);
    if (playlist) return undefined;
    else return message.channel.send(`:white_check_mark:  **|** **\`${song.title}\`** a été ajouté à la playlist !`);
  }
  return undefined;
}


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("end", reason => {
      if (reason === "Stream is not generating quickly enough.") console.log("Musique terminée.");
      else console.log(reason);
      serverQueue.songs.shift();
      play(guild, serverQueue.songs[0]);
    })
    .on("error", error => console.error(error));
  dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);

  serverQueue.textChannel.send(`????  **|**  En cours de lecture : **\`${song.title}\`**`);
};

【问题讨论】:

  • 我建议您在 play 命令的第一行声明 serverQueue 变量后记录它,以查看您的队列是否为空或是否有其他问题。您还应该尝试记录歌曲变量,以防万一
  • 是的,变量似乎 serverQueue 为空,但我不明白为什么
  • 我将自己进行一些测试,如果有什么发现我会回来找你
  • 很抱歉,我无法让您的代码在我的计算机上运行。我想我可能缺少正确的模块...从现在开始,我建议您尝试其他模块,例如ytsr 查找歌曲,ytpl 用于播放列表,使用ytdl 获取信息(id、标题等),它可能会帮助您解决问题。您还可以执行我喜欢称之为“大规模日志记录”的操作,其中包括记录代码的每一步以查看错误来自何处。我希望你能解决这个问题,如果你这样做了,请告诉我:)

标签: javascript audio bots discord discord.js


【解决方案1】:

尝试将"end" 替换为"finish"

const dispatcher = serverQueue.connection.play(ytdl(song.url)).on("end", reason => {

const dispatcher = serverQueue.connection.play(ytdl(song.url)).on("finish", reason => {

【讨论】:

  • 哇,成功了,谢谢!我不认为我会在 6 个月后找到答案!
猜你喜欢
  • 2021-02-07
  • 2021-09-18
  • 2018-07-18
  • 2020-08-04
  • 2021-04-27
  • 2022-07-09
  • 2020-09-22
  • 2021-05-24
  • 2020-06-27
相关资源
最近更新 更多