javascript - 带队列的Discord.js音乐机器人无法正常工作

标签 javascript audio bots discord discord.js

我正在尝试使音乐与队列不和谐机器人。目前,播放命令起作用,我可以将音乐添加到队列中(与我的播放列表命令一起显示)。问题是当第一首音乐结束时,机器人会完全停止并且不播放下一首歌(它不会断开连接,看起来像是在播放某首歌)。
我使用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}\`**`);
};

最佳答案

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

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

关于javascript - 带队列的Discord.js音乐机器人无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62190835/

相关文章:

javascript - 转发器定义将从设计器中的嵌套转发器中删除

javascript - 在 Angular Jest 测试中模拟窗口、文档和鼠标单击事件属性

jquery - 切换 SHOUTcast 音频流 HTML5 音频播放器和 JQuery

javascript - 有没有办法检测元素是否被 jQuery 的 click() 单击?

javascript - 参数的 URL? jQuery、Ajax、JSON

javascript - 如何在用户登录时显示注销

android - 如何使用Soundpool一键播放和停止声音

javascript - 更改 aud_play_pause() 中的图标

javascript - 根据使用的邀请码为新成员分配角色

node.js - Viber - 发送键盘消息时隐藏用户字段输入