Discord.js 如何向discord js music bot添加循环和队列命令

Discord.js 如何向discord js music bot添加循环和队列命令,discord.js,Discord.js,如何向这个discord js音乐机器人添加循环和队列命令? 我按照指南做了一个带有Discord js的简单音乐机器人。它可以工作,但我无法找到如何向这个机器人添加循环和队列命令。此外,每当我将歌曲添加到队列时,它都会延迟一秒钟。如果有人知道这方面的任何事情,那么提供一些信息就好了 const Discord = require("discord.js"); const { prefix, token } = require("./config.json"

如何向这个discord js音乐机器人添加循环和队列命令? 我按照指南做了一个带有Discord js的简单音乐机器人。它可以工作,但我无法找到如何向这个机器人添加循环和队列命令。此外,每当我将歌曲添加到队列时,它都会延迟一秒钟。如果有人知道这方面的任何事情,那么提供一些信息就好了

const Discord = require("discord.js");
const { prefix, token } = require("./config.json");
const ytdl = require("ytdl-core");
const yts = require("yt-search");
const client = new Discord.Client();

const queue = new Map();


client.on("message", async message => {
  
  const serverQueue = queue.get(message.guild.id);

  if (message.content.startsWith(`${prefix}play`)) {
    execute(message, serverQueue);
    return;
  } 
  
  if (message.content.startsWith(`${prefix}skip`)) {
    skip(message, serverQueue);
    return;
  } 
  if (message.content.startsWith(`${prefix}stop`)) {
    stop(message, serverQueue);
    return;
  }
  
});

async function execute(message, serverQueue) {
  const args = message.content.split(" ");

  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!"
    );
  }

 let song;
 if (ytdl.validateURL(args[1])) {
  const songInfo = await ytdl.getInfo(args[1]);
  song = {
    title: songInfo.title,
    url: songInfo.video_url
  };
 } else {
  const {videos} = await yts(args.slice(1).join(" "));
  if (!videos.length) return message.channel.send("No songs were found!");
  song = {
    title: videos[0].title,
    url: videos[0].url
  };
 }


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

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

    queueContruct.songs.push(song);

    try {
      var connection = await 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 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}**`);
}


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();
}





client.login(token);


可以使用映射而不是数组。保存所有循环音乐信息(如Youtube URL、id等)。然后编写一个函数,在当前歌曲的结尾播放已保存的音乐。

您要求我们为您执行bot项目。这里有一些潜在的解决方案,可以帮助您解决这个问题,但是您仅仅是填鸭式地输入代码,实际上要求的堆栈溢出太多了。A) 对对象使用数组,其中每个对象都有一个YouTube ID和一个循环布尔值[{ID:“abc123”,Loop:true}]。B) 设置一个全局循环变量,如果循环为true,则返回到队列末尾ID数组的开头