mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
feat(playlists): add personal saved playlists and track bookmarks (#15)
- Implement PlaylistManager with MySQL persistence (user_favorites & user_playlists) - Add %fav command with toggle bookmark, paginated list, play, remove, and clear - Add %playlist command to snapshot queues, queue saved playlists, and manage tracks - Document %fav and %playlist in README command reference
This commit is contained in:
1 parent
b2af315240
commit
c90f591ec1
13 files changed
+1767
-8
No files matched your search
@@ -0,0 +1,342 @@
|
||||
/**
|
||||
* @module commands/fav
|
||||
* @description Bookmark personal favorite tracks and queue them across any server.
|
||||
*/
|
||||
|
||||
import { CommandBuilder } from "../src/commands/index.mjs";
|
||||
import { EmbedBuilder } from "@fluxerjs/core";
|
||||
import { Utils, cleanId } from "../src/utils/Utils.mjs";
|
||||
import { getGlobalColor, getMessageGuildId } from "../src/ui/index.mjs";
|
||||
|
||||
const PAGE_SIZE = 10;
|
||||
const COLOR_PRIMARY = 0xe9196c;
|
||||
const COLOR_SUCCESS = 0x2ecc71;
|
||||
const COLOR_ERROR = 0xe74c3c;
|
||||
|
||||
/** @type {CommandBuilder} */
|
||||
export const command = new CommandBuilder()
|
||||
.setName("fav")
|
||||
.setDescription("Bookmark your favorite tracks and load them into voice channels.", "commands.fav")
|
||||
.addAliases("favorite", "favorites", "bookmark", "bookmarks")
|
||||
.setCategory("music")
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("toggle")
|
||||
.setId("fav_toggle")
|
||||
.setDescription("Save or remove the currently playing song from your favorites.")
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("list")
|
||||
.setId("fav_list")
|
||||
.setDescription("View your list of saved favorite tracks.")
|
||||
.addNumberOption((o) =>
|
||||
o.setName("page").setDescription("Page number to view.").setRequired(false)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("play")
|
||||
.setId("fav_play")
|
||||
.setDescription("Play all your favorites or a specific track number.")
|
||||
.addNumberOption((o) =>
|
||||
o.setName("track").setDescription("Track number to play.").setRequired(false)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("remove")
|
||||
.setId("fav_remove")
|
||||
.setDescription("Remove a track from your favorites by number or title.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("target").setDescription("Track number or title to remove.").setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("clear")
|
||||
.setId("fav_clear")
|
||||
.setDescription("Clear all tracks from your personal favorites.")
|
||||
);
|
||||
|
||||
/**
|
||||
* Run handler for the fav command.
|
||||
* @param {object} msg
|
||||
* @param {object} [data]
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function run(msg, data) {
|
||||
const pm = this.playlists;
|
||||
if (!pm) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.fav.notAvailable"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const userId = cleanId(msg.author?.id ?? msg.message?.author?.id ?? msg.member?.user?.id ?? msg.user?.id);
|
||||
const authorName = msg.author?.username ?? msg.message?.author?.username ?? "User";
|
||||
const guildId = cleanId(getMessageGuildId(msg));
|
||||
const prefix = this.handler.getPrefix(guildId) || "%";
|
||||
const embedColor = getGlobalColor() || COLOR_PRIMARY;
|
||||
|
||||
/* Parse raw arguments for text-command dispatch */
|
||||
const rawContent = (msg.message?.content || msg.content || "").trim();
|
||||
const rawParts = rawContent.split(/\s+/).slice(1);
|
||||
const firstToken = (rawParts[0] || "").toLowerCase();
|
||||
|
||||
let sub = data?.command?.name || data?.commandId || "";
|
||||
if (!sub || sub === "fav") {
|
||||
if (["list", "view", "all"].includes(firstToken)) sub = "list";
|
||||
else if (["play", "p", "queue"].includes(firstToken)) sub = "play";
|
||||
else if (["remove", "delete", "rm", "del"].includes(firstToken)) sub = "remove";
|
||||
else if (["clear", "reset"].includes(firstToken)) sub = "clear";
|
||||
else if (["add", "save"].includes(firstToken)) sub = "add";
|
||||
else sub = "toggle";
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: PLAY
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "play") {
|
||||
const p = await this.getPlayer(msg);
|
||||
if (!p) return;
|
||||
|
||||
const favs = await pm.getFavorites(userId, 250, 0);
|
||||
if (favs.length === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.fav.empty", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* Check if a specific track number was requested */
|
||||
let trackNum = data?.get?.("track")?.value;
|
||||
if (!trackNum && rawParts.length > 1) {
|
||||
const parsedNum = parseInt(rawParts[1], 10);
|
||||
if (!isNaN(parsedNum)) trackNum = parsedNum;
|
||||
}
|
||||
|
||||
if (trackNum != null) {
|
||||
const idx = parseInt(trackNum, 10) - 1;
|
||||
if (isNaN(idx) || idx < 0 || idx >= favs.length) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.fav.invalidIndex", { max: favs.length }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const target = favs[idx];
|
||||
const video = {
|
||||
videoId: "",
|
||||
encoded: "",
|
||||
sourceName: target.source_name || "unknown",
|
||||
isLive: false,
|
||||
isStream: false,
|
||||
title: target.title,
|
||||
url: target.uri,
|
||||
thumbnail: target.thumbnail || null,
|
||||
spotifyUrl: null,
|
||||
_durationMs: Number(target.duration_ms) || 0,
|
||||
duration: {
|
||||
timestamp: Utils.prettifyMS(Number(target.duration_ms) || 0),
|
||||
seconds: Math.floor((Number(target.duration_ms) || 0) / 1000),
|
||||
},
|
||||
author: {
|
||||
name: target.author || "Unknown",
|
||||
url: target.uri || null,
|
||||
},
|
||||
artists: null,
|
||||
requester: userId,
|
||||
requesterName: authorName,
|
||||
};
|
||||
|
||||
p.addToQueue(video, false);
|
||||
if (!p.queue.getCurrent()) {
|
||||
p.playNext().catch((e) => logger.error("[fav] playNext error:", e?.message));
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.fav.queuedOne", {
|
||||
title: target.title,
|
||||
url: target.uri,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* Queue all favorite tracks */
|
||||
const videos = favs.map((f) => ({
|
||||
videoId: "",
|
||||
encoded: "",
|
||||
sourceName: f.source_name || "unknown",
|
||||
isLive: false,
|
||||
isStream: false,
|
||||
title: f.title,
|
||||
url: f.uri,
|
||||
thumbnail: f.thumbnail || null,
|
||||
spotifyUrl: null,
|
||||
_durationMs: Number(f.duration_ms) || 0,
|
||||
duration: {
|
||||
timestamp: Utils.prettifyMS(Number(f.duration_ms) || 0),
|
||||
seconds: Math.floor((Number(f.duration_ms) || 0) / 1000),
|
||||
},
|
||||
author: {
|
||||
name: f.author || "Unknown",
|
||||
url: f.uri || null,
|
||||
},
|
||||
artists: null,
|
||||
requester: userId,
|
||||
requesterName: authorName,
|
||||
}));
|
||||
|
||||
p.addManyToQueue(videos, false);
|
||||
if (!p.queue.getCurrent()) {
|
||||
p.playNext().catch((e) => logger.error("[fav] playNext error:", e?.message));
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.fav.queuedAll", { count: videos.length }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: REMOVE
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "remove") {
|
||||
let target = data?.get?.("target")?.value;
|
||||
if (!target && rawParts.length > 1) {
|
||||
target = rawParts.slice(1).join(" ");
|
||||
}
|
||||
|
||||
if (!target) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.fav.removeUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const res = await pm.removeFavorite(userId, target);
|
||||
if (!res.removed || !res.track) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.fav.notFound"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.fav.removed", {
|
||||
title: res.track.title,
|
||||
url: res.track.uri,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: CLEAR
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "clear") {
|
||||
const count = await pm.clearFavorites(userId);
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.fav.cleared", { count }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: LIST
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "list") {
|
||||
const totalCount = await pm.getFavoriteCount(userId);
|
||||
if (totalCount === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setAuthor({ name: this.t(msg, "responses.fav.listTitle", { user: authorName }) })
|
||||
.setDescription(this.t(msg, "responses.fav.empty", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(totalCount / PAGE_SIZE));
|
||||
let page = data?.get?.("page")?.value;
|
||||
if (!page && rawParts.length > 1) {
|
||||
const parsed = parseInt(rawParts[1], 10);
|
||||
if (!isNaN(parsed)) page = parsed;
|
||||
}
|
||||
const safePage = Math.max(1, Math.min(parseInt(page, 10) || 1, totalPages));
|
||||
const offset = (safePage - 1) * PAGE_SIZE;
|
||||
|
||||
const favs = await pm.getFavorites(userId, PAGE_SIZE, offset);
|
||||
|
||||
let desc = "";
|
||||
favs.forEach((f, i) => {
|
||||
const num = offset + i + 1;
|
||||
const dur = Utils.prettifyMS(Number(f.duration_ms) || 0);
|
||||
const authorText = f.author ? ` • ${f.author}` : "";
|
||||
desc += `\`${num}.\` **[${f.title}](${f.uri})**${authorText} \`[${dur}]\`\n`;
|
||||
});
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setAuthor({ name: this.t(msg, "responses.fav.listTitle", { user: authorName }) })
|
||||
.setDescription(desc || this.t(msg, "responses.fav.empty", { prefix }))
|
||||
.setFooter({
|
||||
text: this.t(msg, "responses.fav.listFooter", {
|
||||
page: safePage,
|
||||
totalPages,
|
||||
total: totalCount,
|
||||
prefix,
|
||||
}),
|
||||
});
|
||||
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: TOGGLE / DEFAULT
|
||||
------------------------------------------------------------------------- */
|
||||
const p = await this.getPlayer(msg, false);
|
||||
const current = p?.queue?.getCurrent();
|
||||
|
||||
/* If no track is currently playing and user typed plain %fav, show the list */
|
||||
if (!current) {
|
||||
const totalCount = await pm.getFavoriteCount(userId);
|
||||
if (totalCount === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setDescription(this.t(msg, "responses.fav.empty", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
return run.call(this, msg, { command: { name: "list" } });
|
||||
}
|
||||
|
||||
/* Toggle the currently playing track in favorites */
|
||||
const toggleRes = await pm.toggleFavorite(userId, current);
|
||||
|
||||
if (toggleRes.action === "limit") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.fav.limitReached", { max: 250 }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
if (toggleRes.action === "added") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.fav.added", {
|
||||
title: current.title,
|
||||
url: current.url || current.uri,
|
||||
count: toggleRes.count,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_PRIMARY)
|
||||
.setDescription(this.t(msg, "responses.fav.removed", {
|
||||
title: current.title,
|
||||
url: current.url || current.uri,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
@@ -0,0 +1,508 @@
|
||||
/**
|
||||
* @module commands/playlist
|
||||
* @description Save the active queue or manage personal custom playlists across servers.
|
||||
*/
|
||||
|
||||
import { CommandBuilder } from "../src/commands/index.mjs";
|
||||
import { EmbedBuilder } from "@fluxerjs/core";
|
||||
import { Utils, cleanId } from "../src/utils/Utils.mjs";
|
||||
import { getGlobalColor, getMessageGuildId } from "../src/ui/index.mjs";
|
||||
import { MAX_PLAYLISTS_PER_USER, MAX_TRACKS_PER_PLAYLIST } from "../src/services/PlaylistManager.mjs";
|
||||
|
||||
const PAGE_SIZE = 10;
|
||||
const COLOR_PRIMARY = 0xe9196c;
|
||||
const COLOR_SUCCESS = 0x2ecc71;
|
||||
const COLOR_ERROR = 0xe74c3c;
|
||||
|
||||
/** @type {CommandBuilder} */
|
||||
export const command = new CommandBuilder()
|
||||
.setName("playlist")
|
||||
.setDescription("Save the current queue or manage your personal custom playlists.", "commands.playlist")
|
||||
.addAliases("pl", "playlists")
|
||||
.setCategory("music")
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("save")
|
||||
.setId("playlist_save")
|
||||
.setDescription("Save the current queue as a named personal playlist.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("name").setDescription("Name for your playlist.").setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("play")
|
||||
.setId("playlist_play")
|
||||
.setDescription("Queue a previously saved personal playlist.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("name").setDescription("Name of the playlist to play.").setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("list")
|
||||
.setId("playlist_list")
|
||||
.setDescription("List all your saved personal playlists.")
|
||||
.addNumberOption((o) =>
|
||||
o.setName("page").setDescription("Page number to view.").setRequired(false)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("show")
|
||||
.setId("playlist_show")
|
||||
.setDescription("View the tracks inside a specific playlist.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("name").setDescription("Name of the playlist to view.").setRequired(true)
|
||||
)
|
||||
.addNumberOption((o) =>
|
||||
o.setName("page").setDescription("Page number of tracks.").setRequired(false)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("add")
|
||||
.setId("playlist_add")
|
||||
.setDescription("Add the currently playing track to an existing playlist.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("name").setDescription("Name of the target playlist.").setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("remove")
|
||||
.setId("playlist_remove")
|
||||
.setDescription("Remove a track from a playlist by its track number.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("name").setDescription("Name of the playlist.").setRequired(true)
|
||||
)
|
||||
.addNumberOption((o) =>
|
||||
o.setName("index").setDescription("Track number (1-based) to remove.").setRequired(true)
|
||||
)
|
||||
)
|
||||
.addSubcommand((s) =>
|
||||
s
|
||||
.setName("delete")
|
||||
.setId("playlist_delete")
|
||||
.setDescription("Delete a saved personal playlist.")
|
||||
.addTextOption((o) =>
|
||||
o.setName("name").setDescription("Name of the playlist to delete.").setRequired(true)
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Run handler for the playlist command.
|
||||
* @param {object} msg
|
||||
* @param {object} [data]
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function run(msg, data) {
|
||||
const pm = this.playlists;
|
||||
if (!pm) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.notAvailable"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const userId = cleanId(msg.author?.id ?? msg.message?.author?.id ?? msg.member?.user?.id ?? msg.user?.id);
|
||||
const authorName = msg.author?.username ?? msg.message?.author?.username ?? "User";
|
||||
const guildId = cleanId(getMessageGuildId(msg));
|
||||
const prefix = this.handler.getPrefix(guildId) || "%";
|
||||
const embedColor = getGlobalColor() || COLOR_PRIMARY;
|
||||
|
||||
/* Parse raw text tokens for fallback CLI syntax */
|
||||
const rawContent = (msg.message?.content || msg.content || "").trim();
|
||||
const rawParts = rawContent.split(/\s+/).slice(1);
|
||||
const firstToken = (rawParts[0] || "").toLowerCase();
|
||||
|
||||
let sub = data?.command?.name || data?.commandId || "";
|
||||
if (!sub || sub === "playlist") {
|
||||
if (["save", "create", "new"].includes(firstToken)) sub = "save";
|
||||
else if (["play", "p", "load"].includes(firstToken)) sub = "play";
|
||||
else if (["list", "all"].includes(firstToken)) sub = "list";
|
||||
else if (["show", "view", "info"].includes(firstToken)) sub = "show";
|
||||
else if (["add"].includes(firstToken)) sub = "add";
|
||||
else if (["remove", "rm"].includes(firstToken)) sub = "remove";
|
||||
else if (["delete", "del", "destroy"].includes(firstToken)) sub = "delete";
|
||||
else if (rawParts.length > 0) sub = "play";
|
||||
else sub = "list";
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: SAVE
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "save") {
|
||||
let name = data?.get?.("name")?.value;
|
||||
if (!name && rawParts.length > 1) {
|
||||
name = rawParts[1];
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.saveUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const p = await this.getPlayer(msg, false);
|
||||
const current = p?.queue?.getCurrent();
|
||||
const queued = p?.queue?.data || [];
|
||||
|
||||
const allTracks = [];
|
||||
if (current) allTracks.push(current);
|
||||
if (queued.length > 0) allTracks.push(...queued);
|
||||
|
||||
if (allTracks.length === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.emptyQueue", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const res = await pm.savePlaylist(userId, name, allTracks);
|
||||
if (!res.success) {
|
||||
if (res.error === "LIMIT_REACHED") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.limitReached", { max: MAX_PLAYLISTS_PER_USER }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
if (res.error === "INVALID_NAME") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.invalidName"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.saveFailed"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.playlist.saved", {
|
||||
name: res.name,
|
||||
count: res.count,
|
||||
prefix,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: PLAY
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "play") {
|
||||
let name = data?.get?.("name")?.value;
|
||||
if (!name && rawParts.length > 1) {
|
||||
name = rawParts[1];
|
||||
} else if (!name && rawParts.length === 1 && !["play", "list"].includes(firstToken)) {
|
||||
name = rawParts[0];
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.playUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const playlist = await pm.getPlaylist(userId, name);
|
||||
if (!playlist) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.notFound", { name, prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
if (!playlist.tracks || playlist.tracks.length === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.playlistEmpty", { name }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const p = await this.getPlayer(msg);
|
||||
if (!p) return;
|
||||
|
||||
const videos = playlist.tracks.map((t) => ({
|
||||
videoId: "",
|
||||
encoded: "",
|
||||
sourceName: t.sourceName || "unknown",
|
||||
isLive: false,
|
||||
isStream: false,
|
||||
title: t.title,
|
||||
url: t.uri,
|
||||
thumbnail: t.thumbnail || null,
|
||||
spotifyUrl: null,
|
||||
_durationMs: Number(t.durationMs) || 0,
|
||||
duration: {
|
||||
timestamp: Utils.prettifyMS(Number(t.durationMs) || 0),
|
||||
seconds: Math.floor((Number(t.durationMs) || 0) / 1000),
|
||||
},
|
||||
author: {
|
||||
name: t.author || "Unknown",
|
||||
url: t.uri || null,
|
||||
},
|
||||
artists: null,
|
||||
requester: userId,
|
||||
requesterName: authorName,
|
||||
}));
|
||||
|
||||
p.addManyToQueue(videos, false);
|
||||
if (!p.queue.getCurrent()) {
|
||||
p.playNext().catch((e) => logger.error("[playlist] playNext error:", e?.message));
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.playlist.queued", {
|
||||
name: playlist.name,
|
||||
count: videos.length,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: LIST
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "list") {
|
||||
const playlists = await pm.listPlaylists(userId);
|
||||
if (playlists.length === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setAuthor({ name: this.t(msg, "responses.playlist.listTitle", { user: authorName }) })
|
||||
.setDescription(this.t(msg, "responses.playlist.noPlaylists", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(playlists.length / PAGE_SIZE));
|
||||
let page = data?.get?.("page")?.value;
|
||||
if (!page && rawParts.length > 1) {
|
||||
const parsed = parseInt(rawParts[1], 10);
|
||||
if (!isNaN(parsed)) page = parsed;
|
||||
}
|
||||
const safePage = Math.max(1, Math.min(parseInt(page, 10) || 1, totalPages));
|
||||
const offset = (safePage - 1) * PAGE_SIZE;
|
||||
|
||||
const slice = playlists.slice(offset, offset + PAGE_SIZE);
|
||||
|
||||
let desc = "";
|
||||
slice.forEach((pl, i) => {
|
||||
const num = offset + i + 1;
|
||||
desc += `\`${num}.\` **${pl.name}** — \`${pl.trackCount} tracks\`\n`;
|
||||
});
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setAuthor({ name: this.t(msg, "responses.playlist.listTitle", { user: authorName }) })
|
||||
.setDescription(desc)
|
||||
.setFooter({
|
||||
text: this.t(msg, "responses.playlist.listFooter", {
|
||||
page: safePage,
|
||||
totalPages,
|
||||
total: playlists.length,
|
||||
max: MAX_PLAYLISTS_PER_USER,
|
||||
prefix,
|
||||
}),
|
||||
});
|
||||
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: SHOW / VIEW
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "show") {
|
||||
let name = data?.get?.("name")?.value;
|
||||
if (!name && rawParts.length > 1) {
|
||||
name = rawParts[1];
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.showUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const playlist = await pm.getPlaylist(userId, name);
|
||||
if (!playlist) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.notFound", { name, prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const tracks = playlist.tracks || [];
|
||||
if (tracks.length === 0) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setDescription(this.t(msg, "responses.playlist.playlistEmpty", { name: playlist.name }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(tracks.length / PAGE_SIZE));
|
||||
let page = data?.get?.("page")?.value;
|
||||
if (!page && rawParts.length > 2) {
|
||||
const parsed = parseInt(rawParts[2], 10);
|
||||
if (!isNaN(parsed)) page = parsed;
|
||||
}
|
||||
const safePage = Math.max(1, Math.min(parseInt(page, 10) || 1, totalPages));
|
||||
const offset = (safePage - 1) * PAGE_SIZE;
|
||||
|
||||
const slice = tracks.slice(offset, offset + PAGE_SIZE);
|
||||
|
||||
let desc = "";
|
||||
slice.forEach((t, i) => {
|
||||
const num = offset + i + 1;
|
||||
const dur = Utils.prettifyMS(Number(t.durationMs) || 0);
|
||||
const authorText = t.author ? ` • ${t.author}` : "";
|
||||
desc += `\`${num}.\` **[${t.title}](${t.uri})**${authorText} \`[${dur}]\`\n`;
|
||||
});
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(embedColor)
|
||||
.setAuthor({ name: this.t(msg, "responses.playlist.showTitle", { name: playlist.name }) })
|
||||
.setDescription(desc)
|
||||
.setFooter({
|
||||
text: this.t(msg, "responses.playlist.showFooter", {
|
||||
page: safePage,
|
||||
totalPages,
|
||||
total: tracks.length,
|
||||
name: playlist.name,
|
||||
prefix,
|
||||
}),
|
||||
});
|
||||
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: ADD
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "add") {
|
||||
let name = data?.get?.("name")?.value;
|
||||
if (!name && rawParts.length > 1) {
|
||||
name = rawParts[1];
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.addUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const p = await this.getPlayer(msg, false);
|
||||
const current = p?.queue?.getCurrent();
|
||||
|
||||
if (!current) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.nothingPlaying"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const res = await pm.addToPlaylist(userId, name, current);
|
||||
if (!res.success) {
|
||||
if (res.error === "NOT_FOUND") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.notFound", { name, prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
if (res.error === "PLAYLIST_FULL") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.playlistFull", { max: MAX_TRACKS_PER_PLAYLIST }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.addFailed"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.playlist.trackAdded", {
|
||||
title: current.title,
|
||||
name: res.name,
|
||||
count: res.count,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: REMOVE
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "remove") {
|
||||
let name = data?.get?.("name")?.value;
|
||||
let index = data?.get?.("index")?.value;
|
||||
|
||||
if (!name && rawParts.length > 1) name = rawParts[1];
|
||||
if (index == null && rawParts.length > 2) index = parseInt(rawParts[2], 10);
|
||||
|
||||
if (!name || index == null || isNaN(index)) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.removeUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const res = await pm.removeFromPlaylist(userId, name, index);
|
||||
if (!res.success) {
|
||||
if (res.error === "NOT_FOUND") {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.notFound", { name, prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.invalidIndex"));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.playlist.trackRemoved", {
|
||||
title: res.removedTrack?.title || "Track",
|
||||
name: res.name,
|
||||
count: res.count,
|
||||
}));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
SUBCOMMAND: DELETE
|
||||
------------------------------------------------------------------------- */
|
||||
if (sub === "delete") {
|
||||
let name = data?.get?.("name")?.value;
|
||||
if (!name && rawParts.length > 1) {
|
||||
name = rawParts[1];
|
||||
}
|
||||
|
||||
if (!name) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.deleteUsage", { prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const deleted = await pm.deletePlaylist(userId, name);
|
||||
if (!deleted) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_ERROR)
|
||||
.setDescription(this.t(msg, "responses.playlist.notFound", { name, prefix }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(COLOR_SUCCESS)
|
||||
.setDescription(this.t(msg, "responses.playlist.deleted", { name }));
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user