mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
paginated to debug.mjs
This commit is contained in:
1 parent
3f5c395986
commit
c8a6469025
5 files changed
+235
-46
No files matched your search
+148
-3
@@ -2,6 +2,8 @@ import { CommandBuilder } from "../src/CommandHandler.mjs";
|
||||
import { EmbedBuilder } from "@fluxerjs/core";
|
||||
import { getGlobalColor } from "../src/MessageHandler.mjs";
|
||||
|
||||
const EMOJI_REMOVE_TIMEOUT = 60_000;
|
||||
|
||||
export const command = new CommandBuilder()
|
||||
.setName("debug")
|
||||
.setDescription("A debug command for various purposes.")
|
||||
@@ -13,7 +15,7 @@ export const command = new CommandBuilder()
|
||||
.addChoices("voice")
|
||||
.setRequired(true));
|
||||
|
||||
export function run(msg, data) {
|
||||
export async function run(msg, data) {
|
||||
switch (data.get("target").value) {
|
||||
case "voice": {
|
||||
const servers = [...this.players.playerMap.entries()].map(([cid, s]) => {
|
||||
@@ -47,12 +49,155 @@ export function run(msg, data) {
|
||||
pendingJoins: pending.length,
|
||||
pendingChannels: pending,
|
||||
};
|
||||
|
||||
const MAX_DESC = 4096;
|
||||
const CODE_WRAP = 12; // "```json\n" (8) + "\n```" (4)
|
||||
|
||||
// ── Build pages ────────────────────────────────────────────────────────
|
||||
// Page 0: summary + compact server list
|
||||
// Page 1..N: detailed JSON for each server (one per page, or grouped if small)
|
||||
const pages = [];
|
||||
|
||||
// Page 0 — Summary
|
||||
const summaryLines = [
|
||||
`📊 **Summary**`,
|
||||
`Players in map: **${summary.playerMapSize}**`,
|
||||
`Live players: **${summary.livePlayers}**`,
|
||||
`Pending joins: **${summary.pendingJoins}**${summary.pendingChannels.length ? ` (\`${summary.pendingChannels.join("`, `")}\`)` : ""}`,
|
||||
``,
|
||||
`**Players:**`,
|
||||
];
|
||||
for (let i = 0; i < servers.length; i++) {
|
||||
const s = servers[i];
|
||||
const status = s.conn === "yes" && !s.destroyed && !s.leaving ? "🟢" : "🔴";
|
||||
const line = `${status} \`${s.guildname}\` / \`#${s.name}\` — conn:${s.conn} room:${s.roomState} media:${s.mediaPlayer}`;
|
||||
// Guard against overflow even for the summary
|
||||
if (summaryLines.join("\n").length + line.length + 1 > MAX_DESC - 30) {
|
||||
summaryLines.push(`… and ${servers.length - i} more (see next pages)`);
|
||||
break;
|
||||
}
|
||||
summaryLines.push(line);
|
||||
}
|
||||
pages.push(summaryLines.join("\n"));
|
||||
|
||||
// Pages 1..N — Detailed JSON per-server (group small ones together)
|
||||
{
|
||||
let groupJson = "";
|
||||
let groupStart = 0;
|
||||
|
||||
const flushGroup = () => {
|
||||
if (!groupJson) return;
|
||||
const codeBlock = "```json\n" + groupJson + "\n```";
|
||||
pages.push(codeBlock.slice(0, MAX_DESC));
|
||||
groupJson = "";
|
||||
groupStart = i; // will be set by outer loop
|
||||
};
|
||||
|
||||
let i = 0;
|
||||
for (i = 0; i < servers.length; i++) {
|
||||
const singleJson = JSON.stringify(servers[i], null, 2);
|
||||
const candidate = groupJson
|
||||
? groupJson.slice(0, -1) + ",\n" + singleJson.slice(1) // merge objects into array-like
|
||||
: singleJson;
|
||||
|
||||
if (("```json\n" + candidate + "\n```").length > MAX_DESC && groupJson) {
|
||||
// Current group is full — flush it, start new group with this server
|
||||
pages.push(("```json\n" + groupJson + "\n```").slice(0, MAX_DESC));
|
||||
groupJson = singleJson;
|
||||
} else if (("```json\n" + candidate + "\n```").length > MAX_DESC) {
|
||||
// Even a single server is too big — truncate it
|
||||
const budget = MAX_DESC - CODE_WRAP;
|
||||
groupJson = singleJson.slice(0, budget);
|
||||
pages.push(("```json\n" + groupJson + "\n```").slice(0, MAX_DESC));
|
||||
groupJson = "";
|
||||
} else {
|
||||
groupJson = candidate;
|
||||
}
|
||||
}
|
||||
if (groupJson) {
|
||||
pages.push(("```json\n" + groupJson + "\n```").slice(0, MAX_DESC));
|
||||
}
|
||||
}
|
||||
|
||||
// ── Single page — no pagination needed ────────────────────────────────
|
||||
if (pages.length === 1) {
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(this.t(msg, "responses.debug.voiceTitle"))
|
||||
.setDescription("```json\n" + JSON.stringify({ summary, servers }, null, 2) + "\n```")
|
||||
.setDescription(pages[0].slice(0, MAX_DESC))
|
||||
;
|
||||
msg.reply({ embeds: [embed] });
|
||||
return msg.reply({ embeds: [embed] });
|
||||
}
|
||||
|
||||
// ── Multi-page with reactions ─────────────────────────────────────────
|
||||
const totalPages = pages.length;
|
||||
let currentPage = 0;
|
||||
|
||||
const buildPage = (pageIdx, expired = false) => {
|
||||
const pageLabel = pageIdx === 0
|
||||
? "Summary"
|
||||
: `Player Detail${pages[pageIdx].includes(",") ? "s" : ""}`;
|
||||
const footerText = expired
|
||||
? "Controls expired"
|
||||
: `Page ${pageIdx + 1}/${totalPages} • Use ⬅️➡️ to navigate`;
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(this.t(msg, "responses.debug.voiceTitle") + ` — ${pageLabel}`)
|
||||
.setDescription(pages[pageIdx].slice(0, MAX_DESC))
|
||||
.setFooter({ text: footerText })
|
||||
;
|
||||
return { embeds: [embed] };
|
||||
};
|
||||
|
||||
const replyMsg = await msg.reply(buildPage(0));
|
||||
if (!replyMsg?.message) return;
|
||||
|
||||
const navEmojis = ["⬅️", "➡️", "❌"];
|
||||
for (const emoji of navEmojis) {
|
||||
await replyMsg.message.react(emoji).catch(() => {});
|
||||
}
|
||||
|
||||
const clearReactions = async () => {
|
||||
try {
|
||||
await replyMsg.message.removeAllReactions();
|
||||
} catch {
|
||||
for (const emoji of navEmojis) {
|
||||
try { await replyMsg.message.removeReaction(emoji); } catch {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let emojiTimeout;
|
||||
const resetTimer = () => {
|
||||
clearTimeout(emojiTimeout);
|
||||
emojiTimeout = setTimeout(async () => {
|
||||
unobserve?.();
|
||||
await clearReactions();
|
||||
await replyMsg.edit(buildPage(currentPage, true)).catch(() => {});
|
||||
}, EMOJI_REMOVE_TIMEOUT);
|
||||
};
|
||||
|
||||
const unobserve = replyMsg.onReaction(navEmojis, async (e) => {
|
||||
if (e.emoji_id === "❌") {
|
||||
clearTimeout(emojiTimeout);
|
||||
unobserve?.();
|
||||
await replyMsg.message.delete().catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
resetTimer();
|
||||
|
||||
if (e.emoji_id === "⬅️") {
|
||||
currentPage = currentPage > 0 ? currentPage - 1 : totalPages - 1;
|
||||
} else if (e.emoji_id === "➡️") {
|
||||
currentPage = currentPage < totalPages - 1 ? currentPage + 1 : 0;
|
||||
}
|
||||
|
||||
await replyMsg.edit(buildPage(currentPage)).catch(() => {});
|
||||
});
|
||||
|
||||
resetTimer();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -152,7 +152,7 @@ export async function run(msg, data) {
|
||||
// Embed description includes the metadata and the codeblock
|
||||
const typeLabel = this.t(msg, "responses.eval.typeLabel");
|
||||
const timeLabel = this.t(msg, "responses.eval.timeLabel");
|
||||
const desc = `**${typeLabel}** \`${type}\` • **${timeLabel}** \`${elapsed}ms\`\n\`\`\`js\n${chunks[pageIdx]}\n\`\`\``;
|
||||
const desc = `**${typeLabel}** \`${type}\` • **${timeLabel}** \`${elapsed}ms\`\n\`\`\`js\n${chunks[pageIdx]}\n\`\`\``.slice(0, 4096);
|
||||
|
||||
// Customize footer based on state
|
||||
let footerText = this.t(msg, "responses.eval.pageLabel", { page: pageIdx + 1, total: totalPages });
|
||||
|
||||
+58
-33
@@ -64,45 +64,74 @@ export async function run(message) {
|
||||
const totalLines = lines.length;
|
||||
|
||||
const LINES_PER_PAGE = 25;
|
||||
const totalPages = Math.ceil(totalLines / LINES_PER_PAGE);
|
||||
const MAX_DESC = 4096;
|
||||
let totalPages = Math.ceil(totalLines / LINES_PER_PAGE);
|
||||
|
||||
// Single page — no pagination needed
|
||||
if (totalPages === 1) {
|
||||
const singleEmbed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
||||
.setDescription([
|
||||
`by *${artist}*${syncIndicator}`,
|
||||
``,
|
||||
"```",
|
||||
lines.join("\n"),
|
||||
"```"
|
||||
].join("\n"))
|
||||
.setFooter({ text: this.t(message, "responses.lyrics.nodeLinkFooter", { lines: totalLines }) });
|
||||
if (current.thumbnail) singleEmbed.setThumbnail(current.thumbnail);
|
||||
return message.reply({ embeds: [singleEmbed] });
|
||||
}
|
||||
|
||||
// Multi-page
|
||||
// Build page payloads — each is the raw lyric lines for that page
|
||||
const pages = [];
|
||||
for (let i = 0; i < totalLines; i += LINES_PER_PAGE) {
|
||||
pages.push(lines.slice(i, i + LINES_PER_PAGE).join("\n"));
|
||||
}
|
||||
|
||||
// ── Re-check: if a page's content + header + code block exceeds 4096, re-split
|
||||
// into smaller chunks so setDescription never throws RangeError. ──────────
|
||||
const CODE_WRAP = 13; // "```\n" (4) + "\n```" (5) + header-ish overhead
|
||||
{
|
||||
const reshaped = [];
|
||||
for (const pageText of pages) {
|
||||
const headerLine = `by *${artist}*${syncIndicator}`;
|
||||
const overhead = headerLine.length + 1 + 1 + CODE_WRAP; // header + blank + ``` + ```
|
||||
const budget = MAX_DESC - overhead;
|
||||
if (pageText.length <= budget) {
|
||||
reshaped.push(pageText);
|
||||
} else {
|
||||
// Split this page's text into chunks that fit
|
||||
const pageLines = pageText.split("\n");
|
||||
let chunk = "";
|
||||
for (const ln of pageLines) {
|
||||
if (chunk.length + ln.length + 1 > budget) {
|
||||
if (chunk) reshaped.push(chunk);
|
||||
chunk = ln;
|
||||
} else {
|
||||
chunk += (chunk ? "\n" : "") + ln;
|
||||
}
|
||||
}
|
||||
if (chunk) reshaped.push(chunk);
|
||||
}
|
||||
}
|
||||
pages.length = 0;
|
||||
pages.push(...reshaped);
|
||||
totalPages = pages.length;
|
||||
}
|
||||
|
||||
// ── Single page — no pagination needed ────────────────────────────────────
|
||||
if (totalPages === 1) {
|
||||
const headerLine = `by *${artist}*${syncIndicator}`;
|
||||
const descBody = pages[0];
|
||||
const singleDesc = [headerLine, "", "```", descBody, "```"].join("\n");
|
||||
|
||||
const singleEmbed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
||||
.setDescription(singleDesc.slice(0, MAX_DESC))
|
||||
.setFooter({ text: this.t(message, "responses.lyrics.nodeLinkFooter", { lines: totalLines }) });
|
||||
if (current.thumbnail) singleEmbed.setThumbnail(current.thumbnail);
|
||||
return message.reply({ embeds: [singleEmbed] });
|
||||
}
|
||||
|
||||
// ── Multi-page ────────────────────────────────────────────────────────────
|
||||
let currentPage = 0;
|
||||
let emojiRemoveTimeout;
|
||||
|
||||
const buildContent = (pageIdx, expired = false) => {
|
||||
const headerLine = `by *${artist}*${syncIndicator} • Page ${pageIdx + 1}/${totalPages}`;
|
||||
const descBody = pages[pageIdx];
|
||||
const desc = [headerLine, "", "```", descBody, "```"].join("\n");
|
||||
|
||||
const b = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
||||
.setDescription([
|
||||
`by *${artist}*${syncIndicator} • Page ${pageIdx + 1}/${totalPages}`,
|
||||
``,
|
||||
"```",
|
||||
pages[pageIdx],
|
||||
"```"
|
||||
].join("\n"))
|
||||
.setDescription(desc.slice(0, MAX_DESC))
|
||||
.setFooter({ text: expired
|
||||
? this.t(message, "responses.lyrics.controlsExpired", { lines: totalLines })
|
||||
: this.t(message, "responses.lyrics.nodeLinkFooter", { lines: totalLines }) });
|
||||
@@ -152,16 +181,12 @@ export async function run(message) {
|
||||
setTimeout(() => {
|
||||
clearTimeout(emojiRemoveTimeout);
|
||||
unobserve();
|
||||
const closedHeader = `by *${artist}*${syncIndicator} • Page ${currentPage + 1}/${totalPages}`;
|
||||
const closedDesc = [closedHeader, "", "```", pages[currentPage], "```"].join("\n");
|
||||
const closedEmbed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
||||
.setDescription([
|
||||
`by *${artist}*${syncIndicator} • Page ${currentPage + 1}/${totalPages}`,
|
||||
``,
|
||||
"```",
|
||||
pages[currentPage],
|
||||
"```"
|
||||
].join("\n"))
|
||||
.setDescription(closedDesc.slice(0, MAX_DESC))
|
||||
.setFooter({ text: this.t(message, "responses.lyrics.sessionClosed", { lines: totalLines }) });
|
||||
if (current.thumbnail) closedEmbed.setThumbnail(current.thumbnail);
|
||||
msg.edit({ embeds: [closedEmbed] }).catch(() => {});
|
||||
|
||||
+23
-7
@@ -24,16 +24,32 @@ export async function run(msg) {
|
||||
return msg.reply({ embeds: [emptyEmbed] });
|
||||
}
|
||||
|
||||
const itemsPerPage = 50;
|
||||
const totalPages = Math.ceil(guilds.length / itemsPerPage);
|
||||
const MAX_DESC = 4096;
|
||||
const pages = [];
|
||||
|
||||
// Build the content for each page
|
||||
for (let i = 0; i < guilds.length; i += itemsPerPage) {
|
||||
const chunk = guilds.slice(i, i + itemsPerPage);
|
||||
const list = chunk.map((g, index) => `${i + index + 1}. **${g.name}** (\`${g.id}\`)`).join("\n");
|
||||
pages.push(list);
|
||||
// Build pages dynamically — each page accumulates lines until it would
|
||||
// exceed the 4096-char description limit, then starts a new page.
|
||||
let currentLines = [];
|
||||
let currentLen = 0;
|
||||
|
||||
for (let i = 0; i < guilds.length; i++) {
|
||||
const g = guilds[i];
|
||||
let name = g.name || "unknown";
|
||||
if (name.length > 40) name = name.slice(0, 37) + "...";
|
||||
const line = `${i + 1}. **${name}** (\`${g.id}\`)`;
|
||||
const addedLen = line.length + (currentLines.length ? 1 : 0); // +1 for \n
|
||||
|
||||
if (currentLen + addedLen > MAX_DESC - 20 && currentLines.length > 0) {
|
||||
pages.push(currentLines.join("\n"));
|
||||
currentLines = [];
|
||||
currentLen = 0;
|
||||
}
|
||||
currentLines.push(line);
|
||||
currentLen += addedLen;
|
||||
}
|
||||
if (currentLines.length) pages.push(currentLines.join("\n"));
|
||||
|
||||
const totalPages = pages.length;
|
||||
|
||||
let currentPage = 0;
|
||||
|
||||
|
||||
+5
-2
@@ -51,13 +51,16 @@ export async function run(msg, data) {
|
||||
let desc = "";
|
||||
for (const [channelId, total] of channelCounts) {
|
||||
const name = await getChannelName(channelId);
|
||||
desc += this.t(msg, "responses.test.channelEntry", { name, count: total });
|
||||
const entry = this.t(msg, "responses.test.channelEntry", { name, count: total });
|
||||
// Guard against 4096-char embed description limit
|
||||
if (desc.length + entry.length > 4080) break;
|
||||
desc += entry;
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setTitle(this.t(msg, "responses.test.title"))
|
||||
.setDescription(desc.trim())
|
||||
.setDescription(desc.trim().slice(0, 4096))
|
||||
;
|
||||
msg.reply({ embeds: [embed] });
|
||||
}
|
||||
Reference in new issue
Block a user