Files
fluxer/commands/join.mjs
T
NoLogicAlan 6060e6d318 feat: add i18n locale system and fix pagination/help bugs
- Add Locale class (src/constants/Locale.mjs) with per-guild translation
  support and settings-manager binding
- Wire locale into CommandHandler, MessageHandler, Player, PlayerManager,
  and index.mjs via setLocale() / t() helpers
- Replace all hardcoded English strings (help pages, errors, now-playing,
  permission messages, session-closed footers) with locale key lookups
- Fix: %help triggering "Unknown command" — replyHandler intercept now
  uses case-insensitive check for "unknown command"
- Fix: TypeError crash in closeSession when embed has no footer — guard
  with `if (!footer) footer = { text: "" }` before appending
- Fix: media player teardown now calls destroy() and nulls the instance
  instead of relying on _stopMediaPlayer(), preventing stale state
- Refactor: reply() and send() auto-wrap plain strings in embeds,
  matching old replyEmbed/sendEmbed behaviour
- Refactor: permission errors and invalid-flag errors use t() lookups
- Add top-level t(message, key, data) helper on the bot instance for
  command files to resolve guild locale without manual guildId plumbing
2026-05-06 01:07:03 +03:00

176 lines
6.7 KiB
JavaScript

import { CommandBuilder } from "../src/CommandHandler.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { getGlobalColor } from "../src/MessageHandler.mjs";
import Player from "../src/Player.mjs";
function getGuildId(message) {
return message?.channel?.guildId
?? message?.channel?.guild?.id
?? message?.message?.guildId
?? message?.message?.guild?.id
?? message?.channel?.server_id
?? message?.channel?.serverId
?? null;
}
function cleanId(value) {
return String(value ?? "").replace(/\D/g, "");
}
export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) {
if (!this.client.channels.has(cid)) {
ecb();
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.channelNotFound", { channel: cid }));
return message.reply({ embeds: [embed] });
}
const cleanChannelId = cleanId(cid);
const existing = this.players.playerMap.get(cleanChannelId)
?? [...this.players.playerMap.values()].find((player) => cleanId(player?._channelId) === cleanChannelId);
if (existing) {
cb(existing);
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.alreadyJoined", { channel: cid }));
return message.reply({ embeds: [embed] });
}
// Guard: moonlink must be ready before we can play anything
if (!this.moonlink) {
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.audioNodeConnecting"));
ecb();
return message.reply({ embeds: [embed] });
}
const p = new Player(this.config.token, {
client: this.client,
config: this.config,
nodelink: this.config.nodelink,
moonlink: this.moonlink ?? null,
messageChannel: message.channel,
settingsMgr: this.settingsMgr ?? this.settings ?? null,
observedVoiceUsers: this.observedVoiceUsers ?? null,
});
p.on("autoleave", () => {
const activeChannelId = String(p._channelId ?? cid).replace(/\D/g, "") || cid;
const homeChannelId = String(p._home247Channel ?? activeChannelId).replace(/\D/g, "") || activeChannelId;
const guildId = getGuildId(message);
const is247 = (() => {
try {
const raw = this.settingsMgr?.getServer?.(guildId)?.get?.("stay_247");
return raw && raw !== "none";
} catch (_) { return false; }
})();
const prefix = (() => {
try {
return this._commands?.getPrefix?.(guildId) ?? "%";
} catch (_) { return "%"; }
})();
const desc = is247
? this.t(message, "responses.join.autoLeaveInactive", { channel: activeChannelId })
: this.t(message, "responses.join.autoLeaveInactive247", { channel: activeChannelId, prefix });
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(desc);
message.channel.send({ embeds: [embed] });
this.players.playerMap.delete(activeChannelId);
if (activeChannelId !== cid) this.players.playerMap.delete(cid);
if (homeChannelId !== activeChannelId) this.players.playerMap.delete(homeChannelId);
p.destroy();
});
p.on("message", m => {
const guildId = getGuildId(message);
const raw = this.settingsMgr?.getServer?.(guildId)?.get("songAnnouncements");
const disabled = raw === false || raw === 0 ||
["false","0","no","off","disable"].includes(String(raw).toLowerCase().trim());
if (disabled) return;
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(m);
message.channel.send({ embeds: [embed] });
});
this.players.playerMap.set(cleanChannelId, p);
const joiningEmbed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.join.joining"));
const statusMsg = await message.reply({ embeds: [joiningEmbed] });
try {
await p.join(cleanChannelId);
const okEmbed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.join.joined", { channel: cid }));
await statusMsg.edit({ embeds: [okEmbed] });
cb(p);
} catch (e) {
this.players.playerMap.delete(cleanChannelId);
p.destroy();
const errEmbed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.join.joinFailed", { error: e.message }));
await statusMsg.edit({ embeds: [errEmbed] });
ecb(e);
}
}
export const command = new CommandBuilder()
.setName("join")
.setDescription("Make the bot join your voice channel, or specify one.", "commands.join")
.setId("join")
.setCategory("music")
.addTextOption(option =>
option.setName("channel")
.setDescription("A voice channel mention, ID, or name to join. Defaults to your current channel.")
.setRequired(false)
);
export function run(message, data) {
// Check if a channel argument was provided (mention like <#123456>, bare ID, or name)
const rawArg = data?.get?.("channel")?.value?.trim?.() ?? null;
if (rawArg) {
// Parse channel mention <#ID>, bare numeric ID, or channel name
const mentionMatch = rawArg.match(/^<#(\d+)>$/);
const idMatch = rawArg.match(/^(\d{15,})$/);
let resolvedId = null;
if (mentionMatch) {
resolvedId = mentionMatch[1];
} else if (idMatch) {
resolvedId = idMatch[1];
} else {
// Try to look up by name
const guildId = cleanId(getGuildId(message));
const allChannels = [
...(this.client?.channels?.values?.() ?? [])
];
const match = allChannels.find(c => {
const cServerId = cleanId(c.guildId ?? c.guild?.id ?? c.server_id ?? c.serverId);
// Fluxer uses numeric channel types: 0=text, 2=voice, 4=category, 5=link
const isVoice = c.type === 2;
return isVoice && cServerId === guildId &&
(c.name?.toLowerCase() === rawArg.toLowerCase());
});
if (match) resolvedId = match.id;
}
if (!resolvedId) {
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.voiceChannelNotFound"));
return message.reply({ embeds: [embed] });
}
return this.players.initPlayer(message, resolvedId);
}
// No argument — auto-detect the user's current voice channel
const cid = this.players.checkVoiceChannels(message);
if (!cid) {
const prefix = this._commands?.getPrefix?.(getGuildId(message)) ?? "%";
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.noVoiceChannel", { prefix }));
return message.reply({ embeds: [embed] });
}
this.players.initPlayer(message, cid);
}
export const exportDef = {
name: "joinChannel",
object: joinChannel
};