Files
fluxer/commands/search.mjs
T
NoLogicAlan 57f018c815 feat: audio filters, rate limiting, stability fixes & new announcement channel
feat(filter): add %filter command with Bass Boost, Nightcore, Vaporwave,
  8D, Karaoke, Tremolo and more; filter status now shown in %player UI

feat(player): add Filter button (🎛️) to player controls; display active
  filter name/emoji in queue status line

feat(commands): add per-user command cooldown (1.5s) in CommandHandler;
  owners are exempt so %reload/%eval are never blocked

feat(settings): debounce rapid set() calls into a single remoteSave()
  80ms after the last write; prevents N parallel DB queries per command

feat(storage): add announcementChannelId field to defaults.json

fix(play): wrap statusMsg send in try/catch; fall back to fresh reply
  if the initial status message fails to send

fix(stats): normalise guild.members.fetch() result (Collection, array,
  or plain object) to avoid infinite pagination loop; use memberCount
  as primary member count

fix(search): normalise reaction event user ID across Fluxer event shapes
  (user_id / userId / user.id) so author filter works reliably

fix(forceleave): await this.players.leave() — was missing await, could
  silently swallow errors

fix(MessageHandler): guard register() against double-call on hot-reload;
  second call now returns early to prevent duplicate listeners

fix(MoonlinkManager): add getLiveSessionId() helper that walks active
  nodes to find a valid session before falling back to cached value

fix(reload): use pathToFileURL() for cache-busting import URL so Windows
  drive letters and spaces in paths are handled correctly

fix(eval): broaden sensitive-key list (mysql, nodelink, _nl, credential,
  apikey, webhook); increase scan depth to 6; handle circular refs in
  hasSensitive(); normalise keys to lowercase for matching

fix(worker): compile sanitize regexes once at module load instead of
  on every sanitizeError() call

fix(Player): cache compiled sanitize regexes keyed by host:port:password
  to avoid rebuilding identical RegExp objects on every error path

fix(index): wrap config.json read in try/catch with clear FATAL messages
  for missing file or malformed JSON; validate required keys at startup

config: add rest.timeout/retries and nodelink requestTimeout to
  config_example.json with explanatory comments
2026-04-23 17:19:49 +03:00

170 lines
5.7 KiB
JavaScript

import { CommandBuilder } from "../src/CommandHandler.mjs";
import { Message, getGlobalColor } from "../src/MessageHandler.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { PROVIDER_CHOICES, PROVIDER_NAMES } from "../src/constants/providers.mjs";
const NUMBER_EMOJIS = ["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣","8️⃣","9️⃣"];
const CANCEL_EMOJI = "❌";
// SESSION_MS is now read from config.timers.searchSessionTimeout (fallback: 30s)
function parseInlineProvider(raw) {
const match = raw.match(/^([a-z]+):\s*(.+)$/i);
if (!match) return { provider: null, query: raw.trim() };
const maybeProvider = match[1].toLowerCase();
if (PROVIDER_CHOICES.includes(maybeProvider))
return { provider: maybeProvider, query: match[2].trim() };
return { provider: null, query: raw.trim() };
}
export const command = new CommandBuilder()
.setName("search")
.setDescription(
"Display search results for a query and pick one to play.\n" +
"Supports all providers — use inline prefix e.g. `sp: song` or `-p dz`.",
"commands.search"
)
.setCategory("music")
.addExamples(
"$prefixsearch never gonna give you up",
"$prefixsearch sp: blinding lights",
"$prefixsearch -p dz get lucky"
)
.addChoiceOption(o =>
o.setName("provider")
.setDescription("The search provider. Default: YouTube Music.", "options.search.provider")
.addChoices(...PROVIDER_CHOICES)
.setDefault("ytm")
.addFlagAliases("p", "u", "use")
, true)
.addTextOption(o =>
o.setName("query")
.setDescription("The query to search for, or a provider-prefixed query like `sp: song name`.", "options.search.query")
.setRequired(true)
);
export async function run(msg, data) {
const p = await this.getPlayer(msg, true, true, true);
if (!p) return;
const rawQuery = data.get("query").value;
const flagProvider = data.get("provider")?.value;
const { provider: inlineProvider, query } = parseInlineProvider(rawQuery);
const provider = inlineProvider ?? flagProvider ?? "ytm";
const name = PROVIDER_NAMES[provider] ?? "YouTube Music";
// Send loading embed
const channel = msg.channel?.channel ?? msg.message?.channel;
if (!channel?.send) return;
/**
* Build a search-result embed using EmbedBuilder.
* @param {string} description
* @param {string} [footerText]
* @param {string} [authorLabel] - defaults to `name` (provider name)
*/
const makeEmbed = (description, footerText, authorLabel = name) => {
const b = new EmbedBuilder()
.setColor(getGlobalColor())
.setAuthor({ name: authorLabel })
.setDescription(description);
if (footerText) b.setFooter({ text: footerText });
return b.toJSON();
};
const nativeMsg = msg.message ?? msg;
const rawMsg = await nativeMsg.reply(
{ embeds: [makeEmbed("⏳ Loading results...", null, `Searching ${name}...`)] },
{ ping: false }
).catch(() => null);
if (!rawMsg) return;
// Fetch results
const res = await p.fetchResults(query, msg.authorId, provider);
if (!res?.count) {
rawMsg.edit({ embeds: [makeEmbed("❌ No results found.")] }).catch(() => {});
return;
}
// Build results embed
const results = p.searches.get(msg.authorId) ?? [];
const reactions = NUMBER_EMOJIS.slice(0, res.count);
let desc = "";
results.forEach((v, i) => {
const dur = v.duration ? p.getDuration(v.duration) : "?:??";
const title = v.title || "Unknown";
const url = v.url || "";
desc += `${NUMBER_EMOJIS[i]} [${title}](${url}) — \`${dur}\`\n`;
});
desc += `\n${CANCEL_EMOJI} Cancel`;
await rawMsg.edit({
embeds: [makeEmbed(
desc,
"React with a number to add to queue • ❌ to cancel • 30s timeout",
`${name} — Search Results`
)]
}).catch(() => {});
// Add reactions
for (const emoji of [...reactions, CANCEL_EMOJI]) {
await rawMsg.react(emoji).catch(() => {});
}
const allReactions = [...reactions, CANCEL_EMOJI];
const client = this.client;
const channelId = rawMsg.channelId ?? rawMsg.channel_id ?? rawMsg.channel?.id;
const msgId = rawMsg.id;
const clearReactions = async () => {
try {
await rawMsg.removeAllReactions();
} catch (_) {}
};
const wrapped = new Message(rawMsg, this.messages);
const authorId = msg.message?.author?.id ?? msg.authorId;
const unobserve = wrapped.onReaction(allReactions, async (e, reactionMsg) => {
// Only react to the command author — normalise across Fluxer reaction event shapes
const reactorId = e.user_id ?? e.userId ?? e.user?.id ?? null;
if (reactorId && reactorId !== authorId) return;
clearTimeout(timer);
unobserve();
await clearReactions();
if (e.emoji_id === CANCEL_EMOJI) {
rawMsg.edit({
embeds: [makeEmbed("❌ Cancelled.", null, `${name} — Search Results`)]
}).catch(() => {});
return;
}
const idx = reactions.indexOf(e.emoji_id);
if (idx === -1) return;
const v = p.playResult(msg.authorId, idx);
if (!v) return;
rawMsg.edit({
embeds: [makeEmbed(
`✅ Added [${v.title}](${v.url}) to the queue!`,
null,
`${name} — Search Results`
)]
}).catch(() => {});
});
// Read session timeout from config (fallback 30 s)
const SESSION_MS = this.config?.timers?.searchSessionTimeout ?? 30_000;
const timer = setTimeout(() => {
unobserve();
clearReactions().catch(() => {});
rawMsg.edit({
embeds: [makeEmbed("⏰ Search timed out.", "Session closed", `${name} — Search Results`)]
}).catch(() => {});
}, SESSION_MS); // configurable via config.timers.searchSessionTimeout
}