javascript - Discord.js - 向音乐系统添加队列功能
问题描述
是的,我知道这里有很多关于这个话题的话题。然而; 我发现在大多数情况下,它有点取决于你的音乐代码的样子。因此,我决定不要乱七八糟地搞乱我的代码,并把我不需要的东西弄得乱七八糟。
所以我来了。
下面是我的 play.js 文件中的代码。其中大部分是我通过我找到的指南来完成的,然后我对其进行了一些调整以更适合我的使用。
const discord = require('discord.js');
//Setting up constants
const ytdl = require("ytdl-core");
const ytSearch = require("yt-search");
const voiceChannel = msg.member.voice.channel;
// Check if user is in voiceChannel
if (!voiceChannel) return msg.channel.send(errorVoiceEmbed);
// Check if we have the correct permissions
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has("CONNECT")) return msg.channel.send(permsEmbed);
if (!permissions.has("SPEAK")) return msg.channel.send(permsEmbed);
// Check if a second argument has been passed
if (!args.length) return msg.channel.send(playEmbed);
// Validating the passed URL
const validURL = (str) => {
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
if (!regex.test(str)){
return false;
} else {
return true;
}
}
// If we have a valid URL, load it and play the audio
if(validURL (args[0])){
const connection = await voiceChannel.join();
const stream = ytdl(args[0], {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 1})
// Leave when done
.on('finish', () => {
voiceChannel.leave();
msg.channel.send(completedEmbed);
});
await msg.reply(playingEmbed);
return
}
const connection = await voiceChannel.join();
// If a user enters a search query instead of a link, Search YouTube and play a result.
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const video = await videoFinder(args.join(' '));
if (video) {
const stream = ytdl(video.url, {filter: 'audioonly'});
connection.play(stream, {seek: 0, volume: 1})
.on('finish', () => {
voiceChannel.leave();
msg.channel.send(completedEmbed);
});
await msg.reply (playingEmbed);
} else {
msg.channel.send(noResultsEmbed);
}
那么-我将如何为此添加适当的队列?我正在寻找一个与播放命令完美结合的单独队列命令。因此 - 我将使用两个不同的文件,它们需要通过某种歌曲列表进行通信。这将如何完成,我不确定。我研究过只使用数组来解决这个问题,但没有设法让它发挥作用。
在你问之前 - 这些 msg.channel.send 语句中使用的那些嵌入是文件中前面配置的嵌入。我没有在这里包含这些,但它们就在那里。
请注意: 我不是在寻找完整的解决方案。我只是想要一些提示和提示,以了解如何以简单有效的方式解决这个问题,而不必弄乱我已经拥有的代码。
话虽如此 - 就其本身而言,播放命令的代码可以完美运行。它播放来自提供的链接的请求歌曲或来自搜索查询的歌曲。但是当请求一首新歌时,旧歌会停止播放,而新歌会播放。你们都知道这应该怎么走。如果没有歌曲正在播放,请播放它。如果播放了一首歌曲,则将请求的歌曲添加到队列中,并在上一首歌曲完成后播放,等等。
解决方案
推荐阅读
- c++ - 参数包的反向累积乘积
- javascript - 在数组上增加对象属性,如果在 javascript 中找到则删除
- javascript - javascripts默认替换功能在nextjs中不起作用
- r - 计算数据框单元格中存在的向量的平均值
- firebase - 多个客户端使用 Firebase 发送通知
- ruby - 如何通过内存从叉子传回数据,而不必在 Ruby 中写入文件?
- android - 如何从 Fragment 进行 Intent 活动?(活动名称错误)
- https - “no-referrer-when-downgrade 错误”Firebase 上的 Angular 应用程序连接到 NGINX Jelastic
- javascript - Eloquent JavaScript 第 15 章气球练习
- azure - 如何延长 Azure 自定义脚本扩展默认超时?