From 6ba8359c8cb39573557f2693b21cd477e50797b5 Mon Sep 17 00:00:00 2001 From: Blocks <51055767+Blocksnmore@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:43:08 -0500 Subject: [PATCH] Improve music --- commands/misc/help.ts | 8 +- commands/music/play.tsx | 157 ++++++++++++++-------------- imports/queue.tsx | 86 +++++++-------- imports/tools.ts | 11 -- interactions/config/betas/index.tsx | 1 - 5 files changed, 123 insertions(+), 140 deletions(-) diff --git a/commands/misc/help.ts b/commands/misc/help.ts index caba392..b7960f0 100644 --- a/commands/misc/help.ts +++ b/commands/misc/help.ts @@ -206,10 +206,16 @@ export default class Help extends Command { const categorydata = choice.toLowerCase() === "uncategorized" ? uncategorizedCmds : ctx.client.commands.category(choice).array(); - const description = categorydata + let description = categorydata .sort() .map((cmd) => `${format(cmd.name)}`) .join("\n - "); + + if (choice.toLowerCase() == "music") { + description += `\n - Shuffleplay` + } + + description = description.split("\n").sort().join("\n"); await message.edit({ components: [], diff --git a/commands/music/play.tsx b/commands/music/play.tsx index b089937..326e600 100644 --- a/commands/music/play.tsx +++ b/commands/music/play.tsx @@ -11,15 +11,17 @@ import { import { doPermCheck, lavaCluster, queues, ServerQueue, Song } from "queue"; import { Track } from "lavadeno"; import { getEmojiByName } from "emoji"; -import { removeDiscordFormatting } from "tools"; +import { shuffleArray } from "tools"; import { getEmote } from "i18n"; +const shuffleCommands = ["shuffleplay", "sp"]; + export default class Play extends Command { name = "play"; - aliases = ["p", "enqueue", "add"]; + aliases = ["p", "enqueue", "add", ...shuffleCommands]; category = "music"; description = "Play a song"; - usage = "play "; + usage = ["play ", "shuffleplay "]; async execute(ctx: CommandContext) { if (ctx.guild == undefined) return; @@ -37,9 +39,7 @@ export default class Play extends Command { ], }); } else { - const botState = await ctx.guild!.voiceStates.get( - ctx.client.user!.id, - ); + const botState = await ctx.guild!.voiceStates.get(ctx.client.user!.id); if ( queues.has(ctx.guild!.id) && (botState == undefined || botState.channel == undefined) @@ -57,8 +57,7 @@ export default class Play extends Command { icon_url: ctx.client.user!.avatarURL(), }, title: "Unable to play", - description: - "Please join a voice channel before playing!", + description: "Please join a voice channel before playing!", }).setColor("red"), ], }); @@ -77,14 +76,13 @@ export default class Play extends Command { }); const isLink = - /(https?:\/\/)?(www\.)?([a-zA-Z0-9][a-zA-Z0-9\-]{1,}[a-zA-Z0-9]\.?){1,}(\.[a-zA-Z]{2})?\.[a-zA-Z]{2,63}/i - .test( - ctx.argString, - ); + /(https?:\/\/)?(www\.)?([a-zA-Z0-9][a-zA-Z0-9\-]{1,}[a-zA-Z0-9]\.?){1,}(\.[a-zA-Z]{2})?\.[a-zA-Z]{2,63}/i.test( + ctx.argString + ); const { data, loadType } = await lavaCluster.api.loadTracks( isLink || /(yt|sc)search\:/i.test(ctx.argString) ? ctx.argString - : `ytsearch:${ctx.argString}`, + : `ytsearch:${ctx.argString}` ); if (loadType == "error" || loadType == "empty") { @@ -96,8 +94,7 @@ export default class Play extends Command { icon_url: ctx.client.user!.avatarURL(), }, title: "Unable to find songs!", - description: - "No songs were found for that result!", + description: "No songs were found for that result!", }).setColor("red"), ], }); @@ -112,14 +109,9 @@ export default class Play extends Command { let thumbnail: undefined | string = undefined; - if ( - uri.toLowerCase().startsWith( - "https://www.youtube.com/", - ) - ) { + if (uri.toLowerCase().startsWith("https://www.youtube.com/")) { const videoID = uri.substring(uri.indexOf("=") + 1); - thumbnail = - `https://img.youtube.com/vi/${videoID}/hqdefault.jpg`; + thumbnail = `https://img.youtube.com/vi/${videoID}/hqdefault.jpg`; } songsToAdd.push({ @@ -137,21 +129,55 @@ export default class Play extends Command { if (isLink) { switch (loadType) { case "playlist": { - for (const track of data.tracks) { - addTrackData({ - info: { - author: track.info.author, - identifier: track.info.identifier, - isSeekable: track.info.isSeekable, - isStream: track.info.isStream, - length: track.info.length, - position: track.info.position, - sourceName: track.info.sourceName, - title: track.info.title, - uri: track.info.uri!, - }, - track: track.encoded, - }); + if ( + shuffleCommands.includes( + ctx.message.content + .substring(ctx.prefix.length) + .trim() + .split(" ")[0] + ) + ) { + const tracks: Track[] = []; + + for (const track of data.tracks) { + tracks.push({ + info: { + author: track.info.author, + identifier: track.info.identifier, + isSeekable: track.info.isSeekable, + isStream: track.info.isStream, + length: track.info.length, + position: track.info.position, + sourceName: track.info.sourceName, + title: track.info.title, + uri: track.info.uri!, + }, + track: track.encoded, + }); + } + + const shuffledTracks = shuffleArray(tracks); + + for (const track of shuffledTracks) { + addTrackData(track); + } + } else { + for (const track of data.tracks) { + addTrackData({ + info: { + author: track.info.author, + identifier: track.info.identifier, + isSeekable: track.info.isSeekable, + isStream: track.info.isStream, + length: track.info.length, + position: track.info.position, + sourceName: track.info.sourceName, + title: track.info.title, + uri: track.info.uri!, + }, + track: track.encoded, + }); + } } break; } @@ -201,20 +227,13 @@ export default class Play extends Command { .slice(0, 5) .map( (track, i) => - `${ - emojiMap[ - i as 0 | 1 | 2 | 3 | 4 - ] - } - [${ - removeDiscordFormatting( - track.info.title, - ) - }](${track.info.uri})`, + `${emojiMap[i as 0 | 1 | 2 | 3 | 4]} - [${ + track.info.title + }](${track.info.uri})` ) .join("\n"), footer: { - text: - "This message will time out in 30 seconds!", + text: "This message will time out in 30 seconds!", }, }).setColor("red"), ], @@ -225,14 +244,7 @@ export default class Play extends Command {