music: add playlist limiting via --limit=<number>

This commit is contained in:
Cynthia Foxwell 2022-04-24 11:16:00 -06:00
parent 8c3187584d
commit 64b34c56d6
1 changed files with 20 additions and 5 deletions

View File

@ -50,7 +50,7 @@ async function getSoundcloudClientID() {
return null;
}
async function processPlaylist(url, type, shuffle = false) {
async function processPlaylist(url, type, shuffle = false, limit = -1) {
let playlist;
if (type === "yt") {
@ -108,6 +108,10 @@ async function processPlaylist(url, type, shuffle = false) {
shuffleArray(playlist);
}
if (limit > 0) {
playlist = playlist.slice(0, limit);
}
return playlist;
}
@ -361,7 +365,7 @@ const command = new Command("music");
command.addAlias("m");
command.category = "misc";
command.helpText = "Music";
command.usage = "[search term]";
command.usage = "help";
command.callback = async function (msg, line) {
if (!msg.guildID) return "This command can only be used in guilds.";
@ -380,9 +384,15 @@ command.callback = async function (msg, line) {
}
let shuffle = false;
if (argStr.startsWith("--shuffle ")) {
if (argStr.includes("--shuffle")) {
shuffle = true;
argStr = argStr.replace(/^--shuffle /, "");
argStr = argStr.replace("--shuffle", "").trim();
}
let limit = -1;
if (argStr.match(/--limit=(\d+)/)) {
limit = argStr.match(/--limit=(\d+)/)[1];
argStr = argStr.replace(/--limit=(\d+)/, "").trim();
}
let type;
@ -425,7 +435,12 @@ command.callback = async function (msg, line) {
},
],
});
const playlist = await processPlaylist(argStr, type, shuffle);
const playlist = await processPlaylist(
argStr,
type,
shuffle,
limit
);
await statusMessage.edit({
embeds: [
{