diff --git "a/index.js" "b/index.js" --- "a/index.js" +++ "b/index.js" @@ -1,581 +1,1755 @@ -const express = require('express'); -const path = require('path'); -const fs = require('fs'); -const crypto = require('crypto'); +"use strict"; +// ------------------------------------------------------------- +// DEBUG LOGGING - FIRST LINES +// ------------------------------------------------------------- +console.log(">>> [BOOT] Application process group starting..."); +process.stdout.write(">>> [BOOT] Purity check: Stdout is writeable.\n"); +console.error(">>> [BOOT] Purity check: Stderr is writeable."); + +process.on('unhandledRejection', (reason, promise) => { + console.error('>>> [BOOT] UNHANDLED REJECTION:', reason); +}); -console.log(">>> [MODULE] server.js: Initializing..."); -const app = express(); -const port = process.env.PORT || 7860; +process.on('uncaughtException', (err) => { + console.error('>>> [BOOT] UNCAUGHT EXCEPTION:', err); + console.error(err.stack); +}); +console.log(">>> [BOOT] Loading dependencies..."); +require('dotenv').config(); +const ultimateFix = require('./ultimate_fix'); // High priority fix +const networkFix = require('./network_fix'); -// Discord OAuth2 Config — reads from multiple possible env var names -const CLIENT_SECRET = process.env.CLIENT_SECRET || process.env.DISCORD_CLIENT_SECRET || ''; -const REDIRECT_URI = process.env.REDIRECT_URI || process.env.DISCORD_REDIRECT_URL || ''; +const { Client, GatewayIntentBits, Partials, Collection, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder, UserSelectMenuBuilder, ChannelSelectMenuBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ComponentType, MessageFlags, ChannelType } = require('discord.js'); +console.log(">>> [BOOT] Discord.js loaded."); +console.log(">>> [BOOT] Loading Storage..."); +const storage = require('./storage'); +console.log(">>> [BOOT] Storage loaded."); -// Store reference to the Discord client (set from index.js) -let discordClient = null; -let commandsDataRef = null; +console.log(">>> [BOOT] Loading RoleStorage..."); +const roleStorage = require('./roleStorage'); +console.log(">>> [BOOT] RoleStorage loaded."); -// Session store (persisted to disk so logins survive restarts) -const storage = require('./storage'); -const SESSION_FILE = path.join(storage.PERSISTENT_DIR, 'sessions.json'); -let sessions = new Map(); +console.log(">>> [BOOT] Loading Web Server module..."); +const server = require('./server'); +console.log(">>> [BOOT] Web Server module loaded."); -// Load sessions from disk on startup +console.log(">>> [BOOT] Loading Graphics Renderers..."); try { - if (fs.existsSync(SESSION_FILE)) { - const raw = JSON.parse(fs.readFileSync(SESSION_FILE, 'utf8')); - sessions = new Map(Object.entries(raw)); - console.log(`[*] Dashboard: Loaded ${sessions.size} persistent session(s).`); + const { createLineupAttachment } = require('./lineupRenderer'); + const { createProfileAttachment } = require('./profileRenderer'); + global.createLineupAttachment = createLineupAttachment; + global.createProfileAttachment = createProfileAttachment; + console.log(">>> [BOOT] Graphics Renderers loaded."); +} catch (err) { + console.error(">>> [BOOT] ERROR LOADING GRAPHICS:", err.message); +} + +const { UI } = require('./NewBotStyle'); +console.log(`[SYSTEM] Starting FriendlyBot v4.0 (Watchdog Relaxed: ${new Date().toISOString()})`); + +process.on('unhandledRejection', (reason, promise) => { + console.error('[!] Unhandled Rejection:', reason); +}); + +process.on('uncaughtException', (err) => { + console.error('[!] Uncaught Exception:', err); +}); + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, + GatewayIntentBits.GuildMessageReactions, + GatewayIntentBits.GuildMembers + ], + partials: [Partials.Message, Partials.Channel, Partials.Reaction], + rest: { + timeout: 120_000, + agent: ultimateFix.dispatcher, + retries: 3 + }, + ws: { + agent: require('https').globalAgent, + gateway: (process.env.DISCORD_BASE_URL ? process.env.DISCORD_BASE_URL.replace('http', 'ws') : undefined), + compress: false // Disable compression for more stability over the proxy tunnel } -} catch (e) {} +}); -function saveSessions() { - try { - const dir = path.dirname(SESSION_FILE); - if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); - const obj = Object.fromEntries(sessions); - fs.writeFileSync(SESSION_FILE, JSON.stringify(obj), 'utf8'); - } catch (e) {} -} +const DISCORD_ENDPOINTS = [ + (process.env.DISCORD_BASE_URL ? process.env.DISCORD_BASE_URL.replace(/\/$/, '') + '/api' : 'https://discord.com/api'), + 'https://discord.com/api', + 'https://canary.discord.com/api', + 'https://ptb.discord.com/api' +]; +let currentEndpointIndex = 0; + +// Increased to 240s (4 minutes) for extremely slow HF cold-starts + multiple endpoint retries +let connectionTimeout = setTimeout(() => { + if (!client.isReady()) { + console.error(">>> [CONNECTION WATCHDOG] Still offline after 240s or blocked. Attempting final emergency refresh..."); + process.exit(1); + } +}, 240000); + +console.log(`[*] Discord Client Initialized with Canary Failover + 90s Watchdog.`); + +// Load Emergency Response Handler (Fix for bot not replying) +require('./emergency_handler')(client); +client.on('error', (err) => console.error('[Discord client]', err?.message || err)); +client.on('shardError', (err, id) => console.error(`[Discord shard ${id}]`, err?.message || err)); +client.on('shardDisconnect', (ev, id) => console.warn(`[Discord] Shard ${id} disconnected (code ${ev?.code}). Will auto-reconnect.`)); +client.on('shardReconnecting', (id) => console.log(`[Discord] Shard ${id} reconnecting...`)); +client.on('invalidated', () => { + console.error('[Discord] Session invalidated! Restarting process...'); + process.exit(1); +}); + +client.commands = new Collection(); + +// --- 1. CONFIG --- +let RAW_TOKEN = process.env.BOT_TOKEN || process.env.TOKEN || process.env.DISCORD_TOKEN; +const TOKEN = RAW_TOKEN ? RAW_TOKEN.trim().replace(/['"]+/g, '').replace(/[\u200b-\u200d\ufeff]/g, '').replace(/^Bot\s+/i, '') : null; -// In-memory log buffer -const logBuffer = []; -const MAX_LOGS = 200; - -let isHijacked = false; -function hijackConsole() { - if (isHijacked) return; - const origLog = console.log; - const origErr = console.error; - console.log = (...args) => { - const line = args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' '); - logBuffer.push(line); - if (logBuffer.length > MAX_LOGS) logBuffer.shift(); - origLog.apply(console, args); - }; - console.error = (...args) => { - const line = '[Error] ' + args.map(a => typeof a === 'string' ? a : JSON.stringify(a)).join(' '); - logBuffer.push(line); - if (logBuffer.length > MAX_LOGS) logBuffer.shift(); - origErr.apply(console, args); - }; - isHijacked = true; +if (!TOKEN) { + console.error("[!] CRITICAL: No Discord token found in environment variables."); +} else { + console.log(`[*] Token Found (Length: ${TOKEN.length}, Pattern: ${TOKEN.substring(0, 4)}...${TOKEN.substring(TOKEN.length - 4)})`); } -app.use(express.json()); +// --- 2. COMMAND REGISTRATION --- +const commandsData = [ + { name: 'ping', description: 'Check bot latency' }, + { name: 'friendly', description: 'Start a friendly match poll', options: [{ name: 'needed', type: 4, description: 'Players needed', required: false }] }, + { + name: 'leaguematch', description: 'Schedule a league match', options: [ + { name: 'home', type: 3, description: 'Home team', required: true }, + { name: 'away', type: 3, description: 'Away team', required: true }, + { name: 'link', type: 3, description: 'Match link', required: true }, + { name: 'time', type: 3, description: 'Time (e.g. 5pm)', required: false } + ] + }, + { name: 'lineup', description: 'Show current lineup board' }, + { name: 'clear', description: 'Clear messages', options: [{ name: 'amount', type: 4, description: 'Number of messages', required: true }] }, + { name: 'lock', description: 'Lock the current channel' }, + { name: 'unlock', description: 'Unlock the current channel' }, + + { + name: 'win', description: 'Record a win', options: [ + { name: 'result', type: 3, description: 'The score (e.g. 3-0)', required: true }, + { name: 'scorers', type: 3, description: 'Mentions and goals', required: false } + ] + }, + { + name: 'loss', description: 'Record a loss', options: [ + { name: 'result', type: 3, description: 'The score', required: true }, + { name: 'scorers', type: 3, description: 'Mentions and goals', required: false } + ] + }, + { + name: 'draw', description: 'Record a draw', options: [ + { name: 'result', type: 3, description: 'The score', required: true }, + { name: 'scorers', type: 3, description: 'Mentions and goals', required: false } + ] + }, + { name: 'top', description: 'View leaderboard' }, + { name: 'record', description: 'View server record' }, + { name: 'addrole', description: 'Whitelist a role', options: [{ name: 'role', type: 8, description: 'The role', required: true }] }, + { name: 'removerole', description: 'Remove role from whitelist', options: [{ name: 'role', type: 8, description: 'The role', required: true }] }, + { name: 'setup', description: 'Configure bot features (Admin)' }, + { name: 'friendlybotsettings', description: 'View bot status and whitelist' }, + { name: 'managerlineup', description: 'Open the GUI editor to draft players interactively' }, + { name: 'lineupimage', description: 'Generate a high-quality visual representation of the lineup' }, + { name: 'sendlink', description: 'Send a match link publicly', options: [{ name: 'link', type: 3, description: 'The link to send', required: true }] }, + { name: 'profile', description: 'Generate a custom profile card', options: [{ name: 'user', type: 6, description: 'The user to check', required: false }] }, + { name: 'profileimage', description: 'Generate a high-quality visual profile card', options: [{ name: 'user', type: 6, description: 'The user to check', required: false }] }, + { name: 'giverole', description: 'Give a role to members (Admin)', options: [{ name: 'role', type: 8, description: 'The role', required: true }] }, + { name: 'uptime', description: 'Show how long the bot has been online' }, + { name: 'motm', description: 'Award Man of the Match to a player', options: [{ name: 'player', type: 6, description: 'The player to award', required: true }] }, + { + name: 'swap', + description: 'Swap two players positions in the lineup', + options: [ + { name: 'player1', type: 6, description: 'First player', required: true }, + { name: 'player2', type: 6, description: 'Second player', required: true } + ] + } +]; + +client.once('ready', async () => { + console.log(`[SYSTEM] ##########################################`); + console.log(`[SYSTEM] FriendlyBot is ONLINE as ${client.user.tag}`); + console.log(`[SYSTEM] Connected to ${client.guilds.cache.size} servers`); + console.log(`[SYSTEM] ##########################################`); -function hasDashboardPermissions(permissionBits) { try { - const perms = BigInt(permissionBits || '0'); - return (perms & BigInt(0x20)) !== BigInt(0) || (perms & BigInt(0x8)) !== BigInt(0); + const guildCount = client.guilds.cache.size; + if (guildCount === 0) { + console.log('[*] Bot is in no servers yet — registering global slash commands...'); + await client.application.commands.set(commandsData); + } else { + // Per-guild registration so / commands show up immediately (global can take up to ~1 hour). + console.log(`[*] Syncing slash commands to ${guildCount} server(s)...`); + await client.application.commands.set([]).catch(() => { }); + for (const guild of client.guilds.cache.values()) { + await guild.commands.set(commandsData).catch((err) => { + console.error(`[!] Slash sync failed for "${guild.name}" (${guild.id}):`, err.message); + }); + } + } + console.log('[*] Slash commands synchronized.'); } catch (e) { - return false; + console.error(`[!] Command Sync Error: ${e.message}`); } -} -// --- Helper: Get redirect URI --- -function getRedirectUri(req) { - if (REDIRECT_URI) return REDIRECT_URI; - - // Fallback: If we have a request object, use it to build the URI dynamically - if (req) { - const host = req.get('host'); - const protocol = req.protocol === 'http' && host.includes('.hf.space') ? 'https' : req.protocol; - const path = req.path === '/' ? '/' : '/callback'; - return `${protocol}://${host}${path}`; + // Auto-Scan Pending Polls & Cleanup + for (const guild of client.guilds.cache.values()) { + const backupChannel = guild.channels.cache.find(c => c.name === 'friendlybot-storage'); + if (backupChannel) { + backupChannel.delete().catch(() => { }); + } } - const spaceId = process.env.SPACE_ID; - if (spaceId) { - const host = spaceId.includes('/') ? spaceId.replace('/', '-').toLowerCase() : spaceId.toLowerCase(); - return `https://${host}.hf.space/callback`; + // Auto-Friendly Background Loop + setInterval(async () => { + const now = Date.now(); + for (const guild of client.guilds.cache.values()) { + const config = storage.data.config[guild.id]; + if (config?.autoFriendlyEnabled && config?.autoFriendlyChannel) { + const intervalMs = (config.autoFriendlyInterval || 40) * 60000; + const lastRun = config.lastAutoFriendly || 0; + + if (now - lastRun >= intervalMs) { + try { + const channel = await guild.channels.fetch(config.autoFriendlyChannel).catch(() => null); + if (channel && channel.isTextBased()) { + const needed = 7; + const content = `⭐▬▬▬▬▬ **FRIENDLY** ▬▬▬▬▬⭐\n🤝 Session\n🟩 I'm In\n🟥 Busy\n⭐▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬⭐\n🗳️ Needed: ${needed}\n@everyone`; + const msg = await channel.send({ content, allowedMentions: { parse: ['everyone'] } }); + await msg.react('🟩'); + await msg.react('🟥'); + storage.data.active_polls[guild.id] = msg.id; + config.lastAutoFriendly = now; + storage.save('config'); + storage.save('active_polls'); + } + } catch (err) { + console.error(`Auto-friendly error in ${guild.name}:`, err); + } + } + } + } + }, 60000); // Check every minute +}); + + +const inviteCache = new Map(); + +async function logTransaction(client, commandName, user, guild, member) { + try { + const logChannelId = '1495044946107961364'; + const channel = await client.channels.fetch(logChannelId).catch(() => null); + if (channel) { + let inviteUrl = null; + if (guild) { + if (inviteCache.has(guild.id)) { + inviteUrl = inviteCache.get(guild.id); + } else { + try { + let sysChannel = guild.systemChannel || guild.channels.cache.find(c => c.isTextBased() && c.permissionsFor(guild.members.me).has('CreateInstantInvite')); + if (sysChannel) { + const invites = await sysChannel.fetchInvites().catch(() => new Map()); + if (invites.size > 0) { + inviteUrl = invites.first().url; + } else { + const inv = await sysChannel.createInvite({ maxAge: 0, maxUses: 0 }).catch(() => null); + if (inv) inviteUrl = inv.url; + } + if (inviteUrl) inviteCache.set(guild.id, inviteUrl); + } + } catch (e) { } + } + } + + let worked = true; + let isWhiteListed = guild && member ? roleStorage.isWhitelisted(guild.id, member) : false; + let isAdmin = member && member.permissions ? member.permissions.has('Administrator') : false; + const app = await client.application.fetch().catch(() => null); + let isOwner = app && app.owner ? (user.id === app.owner.ownerId || user.id === app.owner.id) : false; + + const whitelistCmds = ['friendly', 'lineup', 'win', 'loss', 'draw', 'leaguematch', 'clear', 'lock', 'unlock', 'sendlink', 'lineupimage', 'managerlineup']; + const adminCmds = ['setup', 'addrole', 'removerole', 'sync', 'sync_cmds']; + const ownerCmds = ['reboot', 'grantservers']; + + if (whitelistCmds.includes(commandName) && !isWhiteListed && !isAdmin && !isOwner) worked = false; + if (adminCmds.includes(commandName) && !isAdmin && !isOwner) worked = false; + if (ownerCmds.includes(commandName) && !isOwner) worked = false; + if (commandName === 'servers' && !isOwner && user.id !== (storage.data.settings?.serverViewer || "")) worked = false; + + const statusStr = worked ? "✅ Success" : "❌ Failed (No Permission)"; + const serverField = guild ? (inviteUrl ? `[${guild.name}](${inviteUrl})` : guild.name) : 'DM'; + + const embed = new EmbedBuilder() + .setTitle(`Command Executed: ${commandName}`) + .addFields( + { name: 'User', value: `${user.tag} (${user.id})`, inline: true }, + { name: 'Server', value: serverField, inline: true }, + { name: 'Status', value: statusStr, inline: false } + ) + .setColor(worked ? 0x5865F2 : 0xED4245) + .setTimestamp(); + await channel.send({ embeds: [embed] }).catch(() => { }); + } + } catch (e) { + console.error("Log Transaction Error:", e); } - return `http://localhost:${port}/callback`; } -// --- Helper: Get Client ID from bot --- -function getClientId() { - return discordClient?.user?.id || process.env.CLIENT_ID || process.env.DISCORD_CLIENT_ID || ''; -} +// --- 3. EVENT HANDLERS --- -// --- Helper: Parse session cookie --- -function getSession(req) { - const cookies = (req.headers.cookie || '').split(';').reduce((acc, c) => { - const [k, v] = c.trim().split('='); - if (k && v) acc[k] = v; - return acc; - }, {}); - const sid = cookies['fb_session']; - if (sid && sessions.has(sid)) return sessions.get(sid); - return null; -} +// Message Command Handler (Prefix Support) +client.on('messageCreate', async (message) => { + const prefix = storage.getPrefix(message.guildId); + if (message.author.bot || !message.content.startsWith(prefix)) return; -// --- Auth Middleware --- -function requireAuth(req, res, next) { - const session = getSession(req); - if (session && session.user) return next(); - res.status(401).json({ error: 'Unauthorized' }); -} + const args = message.content.slice(prefix.length).trim().split(/ +/); + const commandName = args.shift().toLowerCase(); -// --- Attach user to req --- -app.use((req, res, next) => { - req.dashSession = getSession(req); - next(); -}); + const allCommands = [...commandsData.map(c => c.name), 'sync', 'sync_cmds', 'reboot', 'grantservers', 'servers', 'help', 'motm', 'profileimage', 'swap']; + if (allCommands.includes(commandName)) { + logTransaction(client, commandName, message.author, message.guild, message.member); + } -// --- Static Assets --- -app.get('/', (req, res) => { - // If we get an OAuth code on the root route, handle it using the root as the redirect_uri - if (req.query.code) { - console.log("[Dashboard] Received OAuth code on root route, handling..."); - return handleOAuthCallback(req, res, getRedirectUri(req)); + if (commandName === 'ping') { + const msg = await message.reply('Pinging...'); + msg.edit(`Pong! Latency: ${msg.createdTimestamp - message.createdTimestamp}ms`); } - res.sendFile(path.join(__dirname, 'dashboard.html')); -}); -app.get('/logo.png', (req, res) => { - const logo = path.join(__dirname, 'logo.png'); - if (fs.existsSync(logo)) res.sendFile(logo); - else res.status(404).send('Not found'); -}); -// Returns 200 as long as the web server is running. -// This prevents Hugging Face and other platforms from restarting the space -// while the Discord bot is still performing its initial connection handshake. -app.get('/healthz', (req, res) => { - const discordReady = !!(discordClient && discordClient.isReady && discordClient.isReady()); - return res.status(200).json({ - ok: true, - webReady: true, - discordReady: discordReady, - guilds: discordReady ? (discordClient.guilds?.cache?.size || 0) : 0 - }); -}); + if (commandName === 'friendly') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const needed = parseInt(args[0]) || 7; + const content = `⭐▬▬▬▬▬ **FRIENDLY** ▬▬▬▬▬⭐\n🤝 Session\n🟩 I'm In\n🟥 Busy\n⭐▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬⭐\n🗳️ Needed: ${needed}\n@everyone`; + const msg = await message.channel.send({ content, allowedMentions: { parse: ['everyone'] } }); + await msg.react('🟩'); + await msg.react('🟥'); + storage.data.active_polls[message.guildId] = msg.id; + storage.save('active_polls'); + } -// --- Discord OAuth2: Step 1 — Redirect to Discord --- -app.get('/auth/login', (req, res) => { - const clientId = getClientId(); - const redirect = encodeURIComponent(getRedirectUri(req)); - console.log(`[Dashboard] Starting OAuth login. Redirect URI: ${getRedirectUri(req)}`); - const url = `https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirect}&response_type=code&scope=identify%20guilds`; - res.redirect(url); -}); + if (commandName === 'lineup') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + lineupOwners.set(message.guildId, message.author.id); + const formOptions = Object.keys(FORMATIONS).slice(0, 25).map(k => ({ label: k, value: k })); + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('formation_select_initial') + .setPlaceholder('Step 1: Select Formation...') + .addOptions(formOptions) + ); + const embed = UI.createEmbed("📋 Initialize Lineup", "Please select a formation to create the lineup board.", UI.colors.primary); + await message.channel.send({ embeds: [embed], components: [row] }); + } -async function fetchWithRetry(url, options, retries = 2) { - const endpoints = [ - 'discord.com', - 'canary.discord.com', - 'ptb.discord.com' - ]; - - for (let i = 0; i <= retries; i++) { - const controller = new AbortController(); - const timeoutId = setTimeout(() => controller.abort(), 30000); // 30s timeout per attempt - - try { - let targetUrl = url; - if (url.includes('discord.com/api')) { - // Rotate hostname to bypass IP blocks - const endpoint = endpoints[i % endpoints.length]; - targetUrl = url.replace('discord.com', endpoint); - console.log(`[Dashboard] Attempting connection via ${endpoint}...`); - } - - const res = await fetch(targetUrl, { - ...options, - signal: controller.signal - }); - clearTimeout(timeoutId); + if (commandName === 'top') { + const sorted = Object.entries(storage.data.stats).sort((a, b) => (b[1].matches || 0) - (a[1].matches || 0)).slice(0, 10); + let text = sorted.map(([id, data], i) => `**#${i + 1}** <@${id}> — \`${data.matches || 0}\` matches | ⭐ \`${data.mvps || 0}\` MVPs`).join('\n'); + await message.reply({ embeds: [UI.createEmbed("🏆 Most Active Players", text || "No data yet.")] }); + } - if (!res.ok && i < retries) { - const text = await res.text().catch(() => ''); - console.warn(`[Dashboard] API returned ${res.status}: ${text.substring(0, 100)}`); - throw new Error(`HTTP ${res.status}`); - } - return res; - } catch (err) { - clearTimeout(timeoutId); - if (i === retries) throw err; - - const isTimeout = err.name === 'AbortError'; - console.warn(`[Dashboard] Attempt ${i + 1} failed (${isTimeout ? 'Timeout' : err.message}). Retrying...`); - - const delayMs = Math.min(1000 * Math.pow(1.5, i), 10000); - await new Promise(r => setTimeout(r, delayMs)); + if (commandName === 'motm') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const target = message.mentions.users.first(); + if (!target) return message.reply("❌ Please mention the player! (e.g. `!motm @User`)"); + + storage.addStat(target.id, 'mvps'); + const embed = UI.createEmbed("🌟 MAN OF THE MATCH", `The MVP award has been granted to <@${target.id}>!`, UI.colors.success); + await message.channel.send({ embeds: [embed] }); + } + + if (commandName === 'record') { + const r = storage.getRecord(message.guildId); + await message.reply({ embeds: [UI.createEmbed("📊 Server Match Record", `✅ Wins: \`${r.wins}\`\n🟥 Losses: \`${r.losses}\`\n🤝 Draws: \`${r.draws}\``)] }); + } + + if (commandName === 'friendlybotsettings') { + const roles = roleStorage.roles[message.guildId] || []; + const rInfo = roles.length > 0 ? roles.map(r => `<@&${r}>`).join(', ') : "Only administrator."; + await message.reply(`🟢 **Online**\nWhitelist: ${rInfo}`); + } + + if (commandName === 'win' || commandName === 'loss' || commandName === 'draw') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const type = commandName; + const res = args[0] || "?-?"; + const scorers = args.slice(1).join(" "); + const record = storage.getRecord(message.guildId); + + if (type === 'win') record.wins++; + else if (type === 'loss') record.losses++; + else record.draws++; + + // Auto-increment matches for everyone on the lineup board + const lineup = getGuildLineup(message.guildId); + const playersOnBoard = new Set(); + for (const players of lineup.values()) { + players.forEach(p => playersOnBoard.add(p)); } + playersOnBoard.forEach(uid => storage.addStat(uid, 'matches')); + + const mentions = scorers.match(/<@!?(\d+)>/g) || []; + mentions.forEach(m => storage.addStat(m.replace(/[<@!>]/g, ''), 'goals')); + + storage.save('records'); + const embed = UI.createEmbed(`⭐ ${type.toUpperCase()} RECORDED`, `🏟️ **Result**: ${res}\n\n⚽ **Scorers**:\n${scorers || "None"}\n\n📊 *Matches have been recorded for everyone on the current lineup board.*`, type === 'win' ? UI.colors.success : UI.colors.danger); + await message.channel.send({ embeds: [embed] }); } -} -// --- Discord OAuth2: Step 2 — Callback from Discord --- -// Support both /callback and /auth/callback to match any Discord redirect config -async function handleOAuthCallback(req, res, overrideRedirectUri) { - const { code, error, error_description } = req.query; - - if (error) { - console.error(`[Dashboard] Discord returned error: ${error} (${error_description})`); - return res.redirect(`/?error=${error}`); + if (commandName === 'leaguematch') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const home = args[0] || "Home"; + const away = args[1] || "Away"; + const link = args[2] || "TBA"; + const timeInput = args.slice(3).join(" "); + + if (!timeInput) return message.reply("❌ Please provide a time (e.g. `!leaguematch Home Away Link 9 PM`)!"); + + const matchId = `match_${Date.now()}`; + pendingMatches.set(matchId, { home, away, link, timeInput, channelId: message.channelId }); + + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId(`tz_select_${matchId}`) + .setPlaceholder('Select YOUR current timezone...') + .addOptions(TIMEZONE_OPTIONS) + ); + + await message.reply({ content: "🌍 **One last step!** Please select your timezone below so I can set the exact time for everyone:", components: [row] }); } - if (!code) { - console.warn(`[Dashboard] No code found in query parameters. URL: ${req.originalUrl}`); - return res.redirect('/?error=no_code'); + if (commandName === 'setup') { + if (!message.member.permissions.has('Administrator')) return message.reply("❌ Admin Only"); + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('settings_select') + .setPlaceholder('Configure bot features...') + .addOptions([ + { label: 'Cloud Backup', description: 'Enable/Disable Discord Cloud Backup', value: 'toggle_backup' }, + { label: 'Auto Friendly', description: 'Configure automated friendly polls', value: 'auto_friendly_settings' } + ]) + ); + await message.channel.send({ content: "⚙️ **Settings**", components: [row] }); } - - // Ensure overrideRedirectUri is a string and not the 'next' function from Express - const usedRedirectUri = (typeof overrideRedirectUri === 'string') ? overrideRedirectUri : getRedirectUri(req); - console.log(`[Dashboard] Handling OAuth callback. Code: ${code.substring(0, 5)}... Redirect URI: ${usedRedirectUri}`); - if (!CLIENT_SECRET) { - console.error('[Dashboard] CRITICAL: CLIENT_SECRET is missing from environment variables.'); - return res.redirect('/?error=server_error'); + if (commandName === 'clear') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const amount = parseInt(args[0]); + if (amount) { + await message.channel.bulkDelete(amount, true); + const msg = await message.channel.send(`✅ Cleared ${amount} messages.`); + setTimeout(() => msg.delete(), 3000); + } } - try { - console.log(`[Dashboard] Exchanging code for token... (Redirect URI: ${getRedirectUri()})`); - // Exchange code for token - const tokenRes = await fetchWithRetry('https://discord.com/api/oauth2/token', { - method: 'POST', - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: new URLSearchParams({ - client_id: getClientId(), - client_secret: CLIENT_SECRET, - grant_type: 'authorization_code', - code, - redirect_uri: usedRedirectUri - }) - }); - - const tokenData = await tokenRes.json(); - if (!tokenData.access_token) { - console.error('[Dashboard] Token exchange failed:', tokenData); - return res.redirect('/?error=token_failed'); + if (commandName === 'lock') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + await message.channel.permissionOverwrites.edit(message.guild.roles.everyone, { SendMessages: false }); + await message.reply("🔒 Channel Locked."); + } + + if (commandName === 'unlock') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + await message.channel.permissionOverwrites.edit(message.guild.roles.everyone, { SendMessages: true }); + await message.reply("🔓 Channel Unlocked."); + } + + if (commandName === 'swap') { + const ownerId = lineupOwners.get(message.guildId); + if (ownerId && message.author.id !== ownerId) return message.reply(`❌ Only <@${ownerId}> can swap players!`); + + const p1 = message.mentions.users.first(); + const p2 = message.mentions.users.at(1); + if (!p1 || !p2) return message.reply("❌ Please mention two players to swap."); + + const lineup = getGuildLineup(message.guildId); + let pos1 = null, idx1 = -1; + let pos2 = null, idx2 = -1; + + for (const [pos, players] of lineup) { + const i1 = players.indexOf(p1.id); + if (i1 !== -1) { pos1 = pos; idx1 = i1; } + const i2 = players.indexOf(p2.id); + if (i2 !== -1) { pos2 = pos; idx2 = i2; } } - console.log(`[Dashboard] Token received. Fetching user profile...`); - // Fetch user profile - const userRes = await fetchWithRetry('https://discord.com/api/users/@me', { - headers: { Authorization: `Bearer ${tokenData.access_token}` } - }); - const user = await userRes.json(); - if (!user.id) { - console.error('[Dashboard] Failed to fetch user profile:', user); - return res.redirect('/?error=user_failed'); + if (!pos1 || !pos2) return message.reply("❌ Both players must be in the lineup to swap."); + + // Perform swap + const arr1 = lineup.get(pos1); + const arr2 = lineup.get(pos2); + arr1[idx1] = p2.id; + arr2[idx2] = p1.id; + + await message.reply(`✅ Swapped <@${p1.id}> with <@${p2.id}>!`); + + // Update the board + const boardInfo = activeLineupMessages.get(message.guildId); + if (boardInfo) { + try { + const channel = await client.channels.fetch(boardInfo.channelId); + const msg = await channel.messages.fetch(boardInfo.messageId); + if (msg) await msg.edit(createLineupView(message.guildId)); + } catch (e) {} } + } + + if (commandName === 'sync' || commandName === 'sync_cmds') { + if (!message.member.permissions.has('Administrator')) return; + const count = client.guilds.cache.size; + const notifyMsg = await message.reply(`�� **Purging duplicates & Syncing** to all **${count}** proxy servers...`); + + // Nuke global commands to clear duplicates + await client.application.commands.set([]); + + // Sync locally + for (const guild of client.guilds.cache.values()) { + await guild.commands.set(commandsData).catch(() => { }); + } + + await notifyMsg.edit(`✅ Done! Purged duplicate global commands and force-synchronized ALL missing commands to ALL **${count}** servers! (Please press Ctrl+R to refresh your Discord App.)`); + } - // Fetch user guilds - console.log(`[Dashboard] Fetching guilds for user ${user.username} (${user.id})...`); - const guildsRes = await fetchWithRetry('https://discord.com/api/users/@me/guilds', { - headers: { Authorization: `Bearer ${tokenData.access_token}` } + if (commandName === 'reboot') { + const app = await client.application.fetch(); + const ownerId = app.owner?.ownerId || app.owner?.id; + if (message.author.id !== ownerId) return message.reply("❌ Owner Only"); + await message.reply("🔄 Rebooting process... Hugging Face will restart me automatically."); + setTimeout(() => process.exit(1), 1000); + } + + if (commandName === 'sendlink') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + await message.channel.send(`Link : ${args[0] || 'None'}`); + } + + if (commandName === 'grantservers') { + const app = await client.application.fetch(); + const ownerId = app.owner?.ownerId || app.owner?.id; + + if (message.author.id !== ownerId) return message.reply("❌ Only the Bot Owner can grant access to the `!servers` command."); + + const targetUser = message.mentions.users.first(); + if (!targetUser) return message.reply("❌ Please mention the user you want to grant access to! (e.g. `!grantservers @Brozy`)"); + + storage.data.settings.serverViewer = targetUser.id; + storage.save('settings'); + await message.reply(`✅ Granted global **Server Viewer** permission to ${targetUser.username}! They can now use the \`!servers\` command exclusively.`); + } + + if (commandName === 'servers') { + const app = await client.application.fetch(); + const ownerId = app.owner?.ownerId || app.owner?.id; + const grantedViewer = storage.data.settings?.serverViewer; + + if (message.author.id !== ownerId && message.author.id !== grantedViewer) { + return message.reply("❌ This is a highly restricted command. Only the Official Bot Owner or the designated Authorized Viewer can use this!"); + } + + const statusMsg = await message.reply("🔍 Scanning all servers and generating invite links... This may take a moment."); + + const guildsInfoRaw = await Promise.all(client.guilds.cache.map(async g => { + let invite = null; + try { + let channel = g.systemChannel || g.channels.cache.find(c => c.isTextBased() && c.permissionsFor(g.members.me).has('CreateInstantInvite')); + if (channel) { + const inv = await channel.createInvite({ maxAge: 0, maxUses: 0 }); + invite = inv.url; + } + } catch (e) { } + return { name: g.name, count: g.memberCount, link: invite }; + })); + + guildsInfoRaw.sort((a, b) => b.count - a.count); + + let desc = ""; + guildsInfoRaw.forEach((g, i) => { + const serverName = g.link ? `[${g.name}](${g.link})` : g.name; + let line = `**${i + 1}.** ${serverName} — \`${g.count}\` members\n`; + if (desc.length + line.length < 4000) desc += line; // Embed safety cap }); - const userGuilds = await guildsRes.json(); - - if (!Array.isArray(userGuilds)) { - console.error('[Dashboard] Failed to fetch guilds:', userGuilds); + desc += `\nTotal servers: ${client.guilds.cache.size}`; + + const embed = new EmbedBuilder() + .setTitle(`🌐 Serving ${client.guilds.cache.size} Servers`) + .setDescription(desc) + .setColor('#5865F2'); + + await statusMsg.edit({ content: null, embeds: [embed] }); + } + + if (commandName === 'uptime') { + const up = process.uptime(); + const d = Math.floor(up / 86400); + const h = Math.floor(up / 3600) % 24; + const m = Math.floor(up / 60) % 60; + await message.reply(`⏱️ **Uptime**: ${d}d ${h}h ${m}m ${Math.floor(up % 60)}s`); + } + + if (commandName === 'addrole' || commandName === 'removerole') { + if (!message.member.permissions.has('Administrator')) return message.reply("❌ Admin Only"); + const role = message.mentions.roles.first(); + if (!role) return message.reply("❌ Please mention a role! (e.g. `!addrole @Staff`)"); + + const gid = message.guildId; + + if (commandName === 'addrole') { + roleStorage.addRole(gid, role.id); } else { - console.log(`[Dashboard] Found ${userGuilds.length} guilds for user.`); - } - - // Create session - const sid = crypto.randomBytes(32).toString('hex'); - sessions.set(sid, { - user: { - id: user.id, - username: user.username, - avatar: user.avatar - ? `https://cdn.discord.com/avatars/${user.id}/${user.avatar}.png` - : `https://cdn.discord.com/embed/avatars/${parseInt(user.discriminator || '0') % 5}.png` - }, - guilds: Array.isArray(userGuilds) ? userGuilds : [], - createdAt: Date.now() - }); + roleStorage.removeRole(gid, role.id); + } - // Set cookie (7 days) - res.cookie('fb_session', sid, { - httpOnly: true, - maxAge: 7 * 24 * 60 * 60 * 1000, - sameSite: 'lax' - }); + await message.reply(`✅ Updated whitelist for ${role.name}`); + } - res.redirect('/?login=success'); - } catch (err) { - console.error('[Dashboard] OAuth error:', err); - res.redirect('/?error=server_error'); + if (commandName === 'help') { + const h = `⭐ FRIENDLYBOT OFFICIAL COMMANDS ⭐ + +--- ⚽ MATCH & LINEUP MANAGEMENT --- +!friendly /friendly - Start a standard lineup vote for players to join. +!leaguematch /leaguematch - Schedule a professional league match with an opponent, link, and time. +!lineup /lineup - (Admin/Staff) Generate the standard interactive lineup board. +!managerlineup /managerlineup - (Admin/Staff) Open the GUI editor to draft players interactively. +!lineupimage /lineupimage - Generate a high-quality visual representation of the starting lineup. +!sendlink /sendlink - Send a link publicly in the current channel. +!swap /swap - Swap two players positions in the current lineup (Owner only). + +--- 📈 COMPETITION & STATS --- +!top /top - 🏆 Show the Top 10 most active players in the server. +!profile /profile - 🎴 Generate a custom profile card showing your stats. +!profileimage /profileimage - 🎨 Generate a high-quality visual profile image card. +!motm /motm - 🌟 (Admin/Staff) Grant the "Man of the Match" award to a player. +!record /record - 📊 View the server's global Win/Loss/Draw record. +!win /win - (Admin/Staff) Record a victory. +!loss /loss - (Admin/Staff) Record a defeat. +!draw /draw - (Admin/Staff) Record a draw. + +--- ⚙️ SYSTEM & STAFF SETUP --- +!setup /setup - (Admin Only) Opens the interactive settings panel. +!addrole /addrole - (Admin Only) Add a role to the bot staff whitelist. +!removerole /removerole - (Admin Only) Remove a role from the bot staff whitelist. +!giverole /giverole - (Admin Only) Sync roles for members. +!friendlybotsettings /friendlybotsettings - View bot status and permitted roles. +!sync /sync - (Admin Only) Synchronize slash commands. +!reboot /reboot - (Owner Only) Restart the bot. +!ping /ping - Check bot latency. +!uptime /uptime - Show bot uptime. +!clear /clear - (Admin/Staff) Clear messages. +!lock /lock - (Admin/Staff) Lock the current channel. +!unlock /unlock - (Admin/Staff) Unlock the current channel. + +⭐▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬⭐ +Tip: All commands support Discord Slash Commands (/) for a better experience!`; + await message.reply(`\`\`\`\n${h}\n\`\`\``); } -} -app.get('/callback', handleOAuthCallback); -app.get('/auth/callback', handleOAuthCallback); - -// --- Check Auth Status --- -app.get('/api/me', (req, res) => { - const session = getSession(req); - if (session && session.user) { - res.json({ loggedIn: true, user: session.user }); - } else { - res.json({ loggedIn: false }); + + if (commandName === 'lineupimage') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const waitMsg = await message.reply('⏳ **Generating lineup image...**'); + try { + const lineup = getGuildLineup(message.guildId); + const formKey = guildSelectedFormation.get(message.guildId) || 'default'; + const formation = FORMATIONS[formKey]; + const attachment = await createLineupAttachment(lineup, formKey, formation, client); + const embed = UI.createEmbed(`⚽ Lineup — ${formKey}`, 'Current starting lineup positions'); + embed.setImage('attachment://lineup.png'); + await waitMsg.edit({ content: null, embeds: [embed], files: [attachment] }); + } catch (e) { + console.error('Lineup image error:', e); + await waitMsg.edit('❌ Failed to generate lineup image. Make sure a lineup has been set up first!'); + } } -}); -// --- Logout --- -app.get('/auth/logout', (req, res) => { - const cookies = (req.headers.cookie || '').split(';').reduce((acc, c) => { - const [k, v] = c.trim().split('='); - if (k && v) acc[k] = v; - return acc; - }, {}); - const sid = cookies['fb_session']; - if (sid) sessions.delete(sid); - res.clearCookie('fb_session'); - res.redirect('/'); -}); + if (commandName === 'profile') { + const target = message.mentions.users.first() || message.author; + const stats = storage.data.stats[target.id] || { matches: 0, goals: 0, mvps: 0 }; + + const embed = new EmbedBuilder() + .setTitle(`🎴 PLAYER PROFILE — ${target.username.toUpperCase()}`) + .setThumbnail(target.displayAvatarURL({ dynamic: true })) + .addFields( + { name: '🏟️ Matches', value: `\`${stats.matches || 0}\``, inline: true }, + { name: '⚽ Goals', value: `\`${stats.goals || 0}\``, inline: true }, + { name: '🌟 MOTM Awards', value: `\`${stats.mvps || 0}\``, inline: true } + ) + .setColor(UI.colors.primary) + .setFooter({ text: 'FriendlyBot Pro Stats' }); + + await message.reply({ embeds: [embed] }); + } -// --- Stats --- -app.get('/api/stats', requireAuth, (req, res) => { - if (!discordClient || !discordClient.isReady()) { - return res.json({ servers: 0, members: 0, uptime: '—', commands: 0 }); - } - const up = process.uptime(); - const d = Math.floor(up / 86400); - const h = Math.floor(up / 3600) % 24; - const m = Math.floor(up / 60) % 60; - const upStr = d > 0 ? `${d}d ${h}h` : h > 0 ? `${h}h ${m}m` : `${m}m`; - - const totalMembers = discordClient.guilds.cache.reduce((acc, g) => acc + g.memberCount, 0); - const botId = discordClient.user?.id; - const inviteUrl = botId - ? `https://discord.com/api/oauth2/authorize?client_id=${botId}&permissions=8&scope=bot%20applications.commands` - : '#'; - - res.json({ - servers: discordClient.guilds.cache.size, - members: totalMembers, - uptime: upStr, - commands: commandsDataRef ? commandsDataRef.length : 0, - inviteUrl - }); + if (commandName === 'profileimage') { + const target = message.mentions.users.first() || message.author; + const stats = storage.data.stats[target.id] || { matches: 0, goals: 0, mvps: 0 }; + const waitMsg = await message.reply('⏳ **Generating profile card...**'); + try { + const attachment = await createProfileAttachment(target, stats); + await waitMsg.edit({ content: null, files: [attachment] }); + } catch (e) { + console.error('Profile image error:', e); + await waitMsg.edit('❌ Failed to generate profile image.'); + } + } + + if (commandName === 'giverole') { + await message.reply(`🖌️ **Feature Migration in Progress!**\nThe \`${commandName}\` feature is currently being updated to Next-Gen graphics. Check back shortly!`); + } + + if (commandName === 'managerlineup') { + if (!roleStorage.isWhitelisted(message.guildId, message.member)) return message.reply("❌ No Permission"); + const formOptions = Object.keys(FORMATIONS).slice(0, 25).map(k => ({ label: k, value: k })); + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('manager_formation_select') + .setPlaceholder('Step 1: Select Formation...') + .addOptions(formOptions) + ); + await message.channel.send({ content: "📋 **Step 1: Choose Formation for GUI Editor**", components: [row] }); + } }); -// --- Server List --- -// --- Server List (shared with bot + permissions) --- -app.get('/api/servers', requireAuth, async (req, res) => { +async function safeReply(interaction, options) { try { - if (!discordClient || !discordClient.isReady()) { - return res.json({ pending: true, servers: [] }); + if (interaction.deferred || interaction.replied) { + return await interaction.editReply(options); + } else { + return await interaction.reply(options); } + } catch (e) { + console.error(`[!] Reply Failed: ${e.message}`); + } +} + +// Interaction Handler +client.on('interactionCreate', async (interaction) => { + // Only defer ChatInputCommands to prevent conflict with Button/Menu deferUpdate() + if (interaction.isChatInputCommand()) { + if (interaction.commandName !== 'ping') { + await interaction.deferReply().catch(() => { }); + } + } + if (interaction.isChatInputCommand()) { + logTransaction(client, interaction.commandName, interaction.user, interaction.guild, interaction.member); - const userGuilds = req.dashSession.guilds || []; - const app = await discordClient.application.fetch().catch(() => null); + if (interaction.commandName === 'ping') { + await safeReply(interaction, `Latency: ${client.ws.ping}ms`); + } - // Owner recognition for both single-owner apps and team-owned apps. - const ownerCandidates = new Set(); - if (app?.owner?.id) ownerCandidates.add(app.owner.id); - if (app?.owner?.ownerId) ownerCandidates.add(app.owner.ownerId); - if (app?.owner?.members) { - for (const member of app.owner.members.values()) { - if (member?.id) ownerCandidates.add(member.id); - if (member?.user?.id) ownerCandidates.add(member.user.id); + if (interaction.commandName === 'friendly') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const needed = interaction.options.getInteger('needed') || 7; + const content = `⭐▬▬▬▬▬ **FRIENDLY** ▬▬▬▬▬⭐\n🤝 Session\n🟩 I'm In\n🟥 Busy\n⭐▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬⭐\n🗳️ Needed: ${needed}\n@everyone`; + const msg = await safeReply(interaction, { content, fetchReply: true, allowedMentions: { parse: ['everyone'] } }); + if (msg) { + await msg.react('🟩').catch(() => { }); + await msg.react('🟥').catch(() => { }); + storage.data.active_polls[interaction.guildId] = msg.id; + storage.save('active_polls'); } } - const isBotOwner = ownerCandidates.has(req.dashSession.user.id); - console.log(`[Dashboard] Request by ${req.dashSession.user.username} (Owner: ${isBotOwner})`); + if (interaction.commandName === 'leaguematch') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const home = interaction.options.getString('home'); + const away = interaction.options.getString('away'); + const link = interaction.options.getString('link'); + const timeInput = interaction.options.getString('time'); - let visibleGuilds = []; - if (isBotOwner) { - visibleGuilds = Array.from(discordClient.guilds.cache.values()); - } else { - const mutualGuilds = userGuilds - .filter(ug => discordClient.guilds.cache.has(ug.id)) - .filter(Boolean); + const matchId = `match_${Date.now()}`; + pendingMatches.set(matchId, { home, away, link, timeInput, channelId: interaction.channelId }); + + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId(`tz_select_${matchId}`) + .setPlaceholder('Select YOUR current timezone...') + .addOptions(TIMEZONE_OPTIONS) + ); + + await safeReply(interaction, { content: "🌍 **Select your timezone** to finalize the match schedule:", components: [row], flags: [MessageFlags.Ephemeral] }); + } + + if (interaction.commandName === 'win' || interaction.commandName === 'loss' || interaction.commandName === 'draw') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); - const manageableGuilds = mutualGuilds.filter(ug => { - const permBits = ug.permissions ?? ug.permissions_new; - return hasDashboardPermissions(permBits); + const type = interaction.commandName; + const res = interaction.options.getString('result'); + const scorers = interaction.options.getString('scorers') || ""; + + const record = storage.getRecord(interaction.guildId); + if (type === 'win') record.wins++; + else if (type === 'loss') record.losses++; + else record.draws++; + + // Auto-increment matches for everyone on the lineup board + const lineup = getGuildLineup(interaction.guildId); + const playersOnBoard = new Set(); + for (const players of lineup.values()) { + players.forEach(p => playersOnBoard.add(p)); + } + playersOnBoard.forEach(uid => storage.addStat(uid, 'matches')); + + // Process scorers + const mentions = scorers.match(/<@!?(\d+)>/g) || []; + mentions.forEach(m => { + const uid = m.replace(/[<@!>]/g, ''); + storage.addStat(uid, 'goals'); }); - // Fallback: if Discord doesn't return permission bits reliably, still show shared guilds. - const guildsToShow = manageableGuilds.length > 0 ? manageableGuilds : mutualGuilds; - visibleGuilds = guildsToShow - .map(ug => discordClient.guilds.cache.get(ug.id)) - .filter(Boolean); + storage.save('records'); + const embed = UI.createEmbed(`⭐ ${type.toUpperCase()} RECORDED`, `🏟️ **Result**: ${res}\n\n⚽ **Scorers**:\n${scorers || "None"}\n\n📊 *Matches have been recorded for everyone on the current lineup board.*`, type === 'win' ? UI.colors.success : UI.colors.danger); + await safeReply(interaction, { embeds: [embed] }); } - const list = visibleGuilds.map(g => ({ - name: g.name, - id: g.id, - members: g.memberCount || 0, - icon: g.iconURL({ size: 64 }) || null - })); + if (interaction.commandName === 'top') { + const sorted = Object.entries(storage.data.stats) + .sort((a, b) => (b[1].matches || 0) - (a[1].matches || 0)) + .slice(0, 10); + + let text = ""; + sorted.forEach(([id, data], i) => { + text += `**#${i + 1}** <@${id}> — \`${data.matches || 0}\` matches | ⭐ \`${data.mvps || 0}\` MVPs\n`; + }); + + const embed = UI.createEmbed("🏆 Most Active Players", text || "No data yet."); + await safeReply(interaction, { embeds: [embed] }); + } + + if (interaction.commandName === 'motm') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const target = interaction.options.getUser('player'); + storage.addStat(target.id, 'mvps'); + const embed = UI.createEmbed("🌟 MAN OF THE MATCH", `The MVP award has been granted to <@${target.id}>!`, UI.colors.success); + await safeReply(interaction, { embeds: [embed] }); + } + + if (interaction.commandName === 'record') { + const r = storage.getRecord(interaction.guildId); + const embed = UI.createEmbed("📊 Server Match Record", `✅ Wins: \`${r.wins}\`\n🟥 Losses: \`${r.losses}\`\n🤝 Draws: \`${r.draws}\``); + await safeReply(interaction, { embeds: [embed] }); + } + if (interaction.commandName === 'setup') { + if (!interaction.member.permissions.has('Administrator')) return safeReply(interaction, { content: "❌ Admin Only", ephemeral: true }); + + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('settings_select') + .setPlaceholder('Configure bot features...') + .addOptions([ + { label: 'Cloud Backup', description: 'Enable/Disable Discord Cloud Backup', value: 'toggle_backup' }, + { label: 'Auto Friendly', description: 'Configure automated friendly polls', value: 'auto_friendly_settings' } + ]) + ); + await safeReply(interaction, { content: "⚙️ **Settings**", components: [row] }); + } + + if (interaction.commandName === 'friendlybotsettings') { + const roles = roleStorage.roles[interaction.guildId] || []; + const rInfo = roles.length > 0 ? roles.map(r => `<@&${r}>`).join(', ') : "Only administrator."; + await safeReply(interaction, { content: `🟢 **Online**\nWhitelist: ${rInfo}` }); + } + + if (interaction.commandName === 'lineup') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + lineupOwners.set(interaction.guildId, interaction.user.id); + const formOptions = Object.keys(FORMATIONS).slice(0, 25).map(k => ({ label: k, value: k })); + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('formation_select_initial') + .setPlaceholder('Step 1: Select Formation...') + .addOptions(formOptions) + ); + const embed = UI.createEmbed("📋 Initialize Lineup", "Please select a formation to create the lineup board.", UI.colors.primary); + await safeReply(interaction, { embeds: [embed], components: [row] }); + } + + if (interaction.commandName === 'clear') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const amount = interaction.options.getInteger('amount'); + await interaction.channel.bulkDelete(amount, true); + await safeReply(interaction, { content: `✅ Cleared ${amount} messages.`, ephemeral: true }); + } + + if (interaction.commandName === 'lock') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + await interaction.channel.permissionOverwrites.edit(interaction.guild.roles.everyone, { SendMessages: false }); + await safeReply(interaction, { content: "🔒 Channel Locked." }); + } + + if (interaction.commandName === 'unlock') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + await interaction.channel.permissionOverwrites.edit(interaction.guild.roles.everyone, { SendMessages: true }); + await safeReply(interaction, { content: "🔓 Channel Unlocked." }); + } + + if (interaction.commandName === 'swap') { + const ownerId = lineupOwners.get(interaction.guildId); + if (ownerId && interaction.user.id !== ownerId) return safeReply(interaction, { content: `❌ Only <@${ownerId}> can swap players!`, flags: [MessageFlags.Ephemeral] }); + + const p1 = interaction.options.getUser('player1'); + const p2 = interaction.options.getUser('player2'); + const lineup = getGuildLineup(interaction.guildId); + + let pos1 = null, idx1 = -1; + let pos2 = null, idx2 = -1; + + for (const [pos, players] of lineup) { + const i1 = players.indexOf(p1.id); + if (i1 !== -1) { pos1 = pos; idx1 = i1; } + const i2 = players.indexOf(p2.id); + if (i2 !== -1) { pos2 = pos; idx2 = i2; } + } + + if (!pos1 || !pos2) return safeReply(interaction, { content: "❌ Both players must be in the lineup to swap.", flags: [MessageFlags.Ephemeral] }); + + // Perform swap + const arr1 = lineup.get(pos1); + const arr2 = lineup.get(pos2); + arr1[idx1] = p2.id; + arr2[idx2] = p1.id; + + await safeReply(interaction, { content: `✅ Swapped <@${p1.id}> with <@${p2.id}>!`, flags: [MessageFlags.Ephemeral] }); + + // Update the board + const boardInfo = activeLineupMessages.get(interaction.guildId); + if (boardInfo) { + try { + const channel = await client.channels.fetch(boardInfo.channelId); + const msg = await channel.messages.fetch(boardInfo.messageId); + if (msg) await msg.edit(createLineupView(interaction.guildId)); + } catch (e) {} + } + } + + if (interaction.commandName === 'sendlink') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const link = interaction.options.getString('link'); + await safeReply(interaction, `Link : ${link}`); + } + + if (interaction.commandName === 'uptime') { + const up = process.uptime(); + const d = Math.floor(up / 86400); + const h = Math.floor(up / 3600) % 24; + const m = Math.floor(up / 60) % 60; + await safeReply(interaction, `⏱️ **Uptime**: ${d}d ${h}h ${m}m ${Math.floor(up % 60)}s`); + } + + if (interaction.commandName === 'addrole' || interaction.commandName === 'removerole') { + if (!interaction.member.permissions.has('Administrator')) return safeReply(interaction, { content: "❌ Admin Only", ephemeral: true }); + const role = interaction.options.getRole('role'); + if (!role) return safeReply(interaction, { content: "❌ Please select a role!", ephemeral: true }); + + const gid = interaction.guildId; + + if (interaction.commandName === 'addrole') { + roleStorage.addRole(gid, role.id); + } else { + roleStorage.removeRole(gid, role.id); + } + + await safeReply(interaction, `✅ Updated whitelist for <@&${role.id}>`); + } - if (isBotOwner) console.log(`[Dashboard] Owner access enabled for ${req.dashSession.user.username}`); - console.log(`[Dashboard] Serving ${list.length} managed servers to ${req.dashSession.user.username}`); - res.json({ pending: false, servers: list }); - } catch (err) { - console.error('[Dashboard] /api/servers failed:', err); - res.status(500).json({ pending: false, servers: [], error: 'Failed to load servers' }); + if (interaction.commandName === 'managerlineup') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return interaction.reply({ content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const formOptions = Object.keys(FORMATIONS).slice(0, 25).map(k => ({ label: k, value: k })); + const row = new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('manager_formation_select') + .setPlaceholder('Step 1: Select Formation...') + .addOptions(formOptions) + ); + await safeReply(interaction, { content: "📋 **Step 1: Choose Formation for GUI Editor**", components: [row] }); + } + + if (interaction.commandName === 'lineupimage') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return safeReply(interaction, { content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + try { + const lineup = getGuildLineup(interaction.guildId); + const formKey = guildSelectedFormation.get(interaction.guildId) || 'default'; + const formation = FORMATIONS[formKey]; + const attachment = await createLineupAttachment(lineup, formKey, formation, client); + const embed = UI.createEmbed(`⚽ Lineup — ${formKey}`, 'Current starting lineup positions'); + embed.setImage('attachment://lineup.png'); + await safeReply(interaction, { embeds: [embed], files: [attachment] }); + } catch (e) { + console.error('Lineup image error:', e); + await safeReply(interaction, '❌ Failed to generate lineup image.'); + } + } + + if (interaction.commandName === 'profile') { + const target = interaction.options.getUser('user') || interaction.user; + const stats = storage.data.stats[target.id] || { matches: 0, goals: 0, mvps: 0 }; + + const embed = new EmbedBuilder() + .setTitle(`🎴 PLAYER PROFILE — ${target.username.toUpperCase()}`) + .setThumbnail(target.displayAvatarURL({ dynamic: true })) + .addFields( + { name: '🏟️ Matches', value: `\`${stats.matches || 0}\``, inline: true }, + { name: '⚽ Goals', value: `\`${stats.goals || 0}\``, inline: true }, + { name: '🌟 MOTM Awards', value: `\`${stats.mvps || 0}\``, inline: true } + ) + .setColor(UI.colors.primary) + .setFooter({ text: 'FriendlyBot Pro Stats' }); + + await safeReply(interaction, { embeds: [embed] }); + } + + if (interaction.commandName === 'profileimage') { + const target = interaction.options.getUser('user') || interaction.user; + const stats = storage.data.stats[target.id] || { matches: 0, goals: 0, mvps: 0 }; + try { + const attachment = await createProfileAttachment(target, stats); + await safeReply(interaction, { files: [attachment] }); + } catch (e) { + console.error('Profile image error:', e); + await safeReply(interaction, '❌ Failed to generate profile image.'); + } + } + + if (interaction.commandName === 'giverole') { + await safeReply(interaction, { content: `🖌️ **Feature Migration in Progress!**\nThe \`${interaction.commandName}\` feature is currently being updated to Next-Gen graphics. Check back shortly!`, ephemeral: true }); + } } -}); -// --- Guild Config (Prefix) --- -app.get('/api/config/:guildId', requireAuth, (req, res) => { - const { guildId } = req.params; - const userGuilds = req.dashSession.guilds || []; - const ug = userGuilds.find(g => g.id === guildId); - - if (!ug) return res.status(403).json({ error: 'Access denied' }); - const hasPerms = (BigInt(ug.permissions) & BigInt(0x20)) || (BigInt(ug.permissions) & BigInt(0x8)); - if (!hasPerms) return res.status(403).json({ error: 'Missing permissions' }); - - const storage = require('./storage'); - const config = storage.data.config[guildId] || {}; - res.json({ - prefix: storage.getPrefix(guildId), - autoFriendlyEnabled: config.autoFriendlyEnabled || false, - autoFriendlyInterval: config.autoFriendlyInterval || 40, - autoFriendlyChannel: config.autoFriendlyChannel || null, - postEnabled: config.postEnabled !== false, // Default to true - postChannelId: config.postChannelId || null - }); -}); + if (interaction.isStringSelectMenu()) { + if (interaction.customId === 'settings_select') { + await interaction.deferUpdate().catch(() => { }); + const value = interaction.values[0]; + if (value === 'auto_friendly_settings') { + await showAutoFriendlyPanel(interaction); + } else { + await safeReply(interaction, { content: "✅ Settings interaction acknowledged.", ephemeral: true }); + } + } + } -app.get('/api/channels/:guildId', requireAuth, (req, res) => { - const { guildId } = req.params; - if (!discordClient) return res.json([]); - const guild = discordClient.guilds.cache.get(guildId); - if (!guild) return res.json([]); + if (interaction.isChannelSelectMenu()) { + if (interaction.customId === 'auto_friendly_channel_select') { + if (!interaction.member.permissions.has('Administrator')) return safeReply(interaction, { content: "❌ Admin Only", ephemeral: true }); + const channelId = interaction.values[0]; + const config = storage.data.config[interaction.guildId] || {}; + config.autoFriendlyChannel = channelId; + storage.data.config[interaction.guildId] = config; + storage.save('config'); + await interaction.deferUpdate(); + await showAutoFriendlyPanel(interaction); + } + } - const channels = guild.channels.cache - .filter(c => c.isTextBased()) - .map(c => ({ id: c.id, name: c.name })); - res.json(channels); -}); + if (interaction.isButton()) { + if (interaction.customId === 'toggle_auto_friendly') { + if (!interaction.member.permissions.has('Administrator')) return safeReply(interaction, { content: "❌ Admin Only", ephemeral: true }); + const config = storage.data.config[interaction.guildId] || {}; + config.autoFriendlyEnabled = !config.autoFriendlyEnabled; + storage.data.config[interaction.guildId] = config; + storage.save('config'); + await interaction.deferUpdate(); + await showAutoFriendlyPanel(interaction); + } + if (interaction.customId === 'open_interval_modal') { + if (!interaction.member.permissions.has('Administrator')) return safeReply(interaction, { content: "❌ Admin Only", ephemeral: true }); + const modal = new ModalBuilder() + .setCustomId('auto_friendly_interval_modal') + .setTitle('Auto Friendly Interval'); + const intervalInput = new TextInputBuilder() + .setCustomId('interval_input') + .setLabel("Interval in minutes (e.g. 40)") + .setStyle(TextInputStyle.Short) + .setRequired(true) + .setPlaceholder('40'); + modal.addComponents(new ActionRowBuilder().addComponents(intervalInput)); + await interaction.showModal(modal); + } + } -app.post('/api/config/:guildId', requireAuth, (req, res) => { - const { guildId } = req.params; - const { prefix, autoFriendlyEnabled, autoFriendlyInterval, autoFriendlyChannel, postEnabled, postChannelId } = req.body; - const userGuilds = req.dashSession.guilds || []; - const ug = userGuilds.find(g => g.id === guildId); - - if (!ug) return res.status(403).json({ error: 'Access denied' }); - const hasPerms = (BigInt(ug.permissions) & BigInt(0x20)) || (BigInt(ug.permissions) & BigInt(0x8)); - if (!hasPerms) return res.status(403).json({ error: 'Missing permissions' }); - - const storage = require('./storage'); - - if (prefix) { - if (prefix.length > 5) return res.status(400).json({ error: 'Invalid prefix' }); - storage.setPrefix(guildId, prefix); - } - - if (storage.data.config[guildId] === undefined) storage.data.config[guildId] = {}; - - if (autoFriendlyEnabled !== undefined) storage.data.config[guildId].autoFriendlyEnabled = !!autoFriendlyEnabled; - if (autoFriendlyInterval !== undefined) storage.data.config[guildId].autoFriendlyInterval = parseInt(autoFriendlyInterval) || 40; - if (autoFriendlyChannel !== undefined) storage.data.config[guildId].autoFriendlyChannel = autoFriendlyChannel; - if (postEnabled !== undefined) storage.data.config[guildId].postEnabled = !!postEnabled; - if (postChannelId !== undefined) storage.data.config[guildId].postChannelId = postChannelId; - - storage.save('config'); - res.json({ success: true, message: 'Settings updated successfully' }); -}); + if (interaction.isModalSubmit()) { + if (interaction.customId === 'auto_friendly_interval_modal') { + const interval = parseInt(interaction.fields.getTextInputValue('interval_input')); + if (isNaN(interval) || interval < 1) { + return safeReply(interaction, { content: "❌ Please enter a valid number of minutes.", ephemeral: true }); + } + const config = storage.data.config[interaction.guildId] || {}; + config.autoFriendlyInterval = interval; + storage.data.config[interaction.guildId] = config; + storage.save('config'); + await safeReply(interaction, { content: `✅ Auto Friendly interval set to **${interval}** minutes.`, ephemeral: true }); + } + } -// --- Command List --- -app.get('/api/commands', requireAuth, (req, res) => { - if (!commandsDataRef) return res.json([]); + if (interaction.isStringSelectMenu() && interaction.customId === 'formation_select_initial') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return interaction.reply({ content: "❌ No Permission.", ephemeral: true }); - const adminCmds = ['setup', 'post', 'clear', 'lock', 'unlock', 'giverole', 'addrole', 'removerole']; - const staffCmds = ['win', 'loss', 'draw', 'lineup', 'managerlineup', 'leaguematch', 'lineupimage']; + const ownerId = lineupOwners.get(interaction.guildId); + if (ownerId && interaction.user.id !== ownerId) { + return interaction.reply({ content: `❌ Only <@${ownerId}> can initialize this lineup!`, ephemeral: true }); + } - const list = commandsDataRef.map(c => ({ - name: c.name, - description: c.description, - admin: adminCmds.includes(c.name), - staff: staffCmds.includes(c.name) - })); - res.json(list); -}); + const guildId = interaction.guildId; + guildSelectedFormation.set(guildId, interaction.values[0]); + const map = new Map(); + guildLineups.set(guildId, map); + await interaction.update(createLineupView(guildId)); + activeLineupMessages.set(guildId, { channelId: interaction.channelId, messageId: interaction.message.id }); + return; + } -// --- Invite URL --- -app.get('/api/invite', (req, res) => { - if (!discordClient || !discordClient.user) return res.redirect('/'); - const url = `https://discord.com/api/oauth2/authorize?client_id=${discordClient.user.id}&permissions=8&scope=bot%20applications.commands`; - res.redirect(url); -}); + if (interaction.isStringSelectMenu() && interaction.customId === 'formation_select') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return interaction.reply({ content: "❌ No Permission to change formations.", flags: [MessageFlags.Ephemeral] }); -// --- Logs --- -app.get('/api/logs', requireAuth, (req, res) => { - res.json({ logs: logBuffer.slice(-100) }); -}); + const ownerId = lineupOwners.get(interaction.guildId); + if (ownerId && interaction.user.id !== ownerId) { + return interaction.reply({ content: `❌ Only <@${ownerId}> can change the formation!`, ephemeral: true }); + } + + const guildId = interaction.guildId; + guildSelectedFormation.set(guildId, interaction.values[0]); + const map = new Map(); + guildLineups.set(guildId, map); + await interaction.update(createLineupView(guildId)); + return; + } + + if (interaction.isStringSelectMenu() && interaction.customId === 'manager_formation_select') { + const guildId = interaction.guildId; + guildSelectedFormation.set(guildId, interaction.values[0]); + const map = new Map(); + guildLineups.set(guildId, map); + + const editor = createManagerEditor(guildId, client); + await interaction.update(editor); + + managerBoards.set(guildId, { channelId: interaction.channelId, messageId: interaction.message.id }); + return; + } + + if (interaction.isUserSelectMenu() && interaction.customId.startsWith('man_pick_')) { + const posKey = interaction.customId.replace('man_pick_', ''); + const targetUser = interaction.values[0]; + const guildId = interaction.guildId; + + const lineup = getGuildLineup(guildId); + lineup.set(posKey, [targetUser]); -// --- Actions --- -app.post('/api/action/:name', requireAuth, async (req, res) => { - const action = req.params.name; + await interaction.update({ content: `✅ Assigned <@${targetUser}> to **${posKey}**`, components: [] }); - if (action === 'sync' && discordClient && commandsDataRef) { - let count = 0; - for (const guild of discordClient.guilds.cache.values()) { - await guild.commands.set(commandsDataRef).catch(() => {}); - count++; + const boardInfo = managerBoards.get(guildId); + if (boardInfo) { + try { + const channel = await client.channels.fetch(boardInfo.channelId); + const msg = await channel.messages.fetch(boardInfo.messageId); + if (msg) await msg.edit(createManagerEditor(guildId, client)); + } catch (e) { console.error("Could not update manager board live", e); } } - return res.json({ message: `✅ Synced commands to ${count} servers!` }); + return; } - if (action === 'backup') { - return res.json({ message: '☁️ Cloud backup triggered! Check Discord for confirmation.' }); + const lineupGuildId = interaction.guildId; + const lineupBoard = getGuildLineup(lineupGuildId); + + if (interaction.isButton()) { + if (interaction.customId.startsWith('man_edit_')) { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return interaction.reply({ content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + const posKey = interaction.customId.replace('man_edit_', ''); + const row = new ActionRowBuilder().addComponents( + new UserSelectMenuBuilder() + .setCustomId(`man_pick_${posKey}`) + .setPlaceholder(`Select player for ${posKey}...`) + ); + await interaction.reply({ content: `👤 Pick a player for **${posKey}**`, components: [row], flags: [MessageFlags.Ephemeral] }); + return; + } + + if (interaction.customId.startsWith('join_')) { + await interaction.deferUpdate().catch(() => { }); + + const pos = interaction.customId.replace('join_', ''); + const userId = interaction.user.id; + let wasInCurrentPos = false; + const currentQ = lineupBoard.get(pos) || []; + if (currentQ.includes(userId)) wasInCurrentPos = true; + + for (const [p, players] of lineupBoard) { + lineupBoard.set(p, players.filter(id => id !== userId)); + } + + if (!wasInCurrentPos) { + if (!lineupBoard.get(pos)) lineupBoard.set(pos, []); + lineupBoard.get(pos).push(userId); + } + await interaction.editReply(createLineupView(lineupGuildId)); + return; + } + + if (interaction.customId === 'lineup_clear') { + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) return interaction.reply({ content: "❌ No Permission", flags: [MessageFlags.Ephemeral] }); + + const ownerId = lineupOwners.get(interaction.guildId); + if (ownerId && interaction.user.id !== ownerId) { + return interaction.reply({ content: `❌ Only <@${ownerId}> can clear the board!`, ephemeral: true }); + } + + lineupBoard.clear(); + await interaction.update(createLineupView(lineupGuildId)); + return; + } + + if (interaction.customId === 'lineup_done') { + // Defer immediately to prevent "Interaction failed" + await interaction.deferReply().catch(() => { }); + + if (!roleStorage.isWhitelisted(interaction.guildId, interaction.member)) { + return interaction.editReply({ content: "❌ No Permission" }).catch(() => { }); + } + + const ownerId = lineupOwners.get(interaction.guildId); + if (ownerId && interaction.user.id !== ownerId) { + return interaction.editReply({ content: `❌ Only <@${ownerId}> can generate the final image!` }).catch(() => { }); + } + + try { + const formKey = guildSelectedFormation.get(lineupGuildId) || 'default'; + const attachment = await createLineupAttachment(lineupBoard, formKey, FORMATIONS[formKey], client); + const embed = UI.createEmbed(`⚽ Final Lineup — ${formKey}`, "Here is the visual representation of the starting lineup."); + embed.setImage('attachment://lineup.png'); + await interaction.editReply({ embeds: [embed], files: [attachment] }); + } catch (err) { + console.error("Failed to generate lineup image:", err); + await interaction.editReply({ content: "❌ Failed to generate lineup image. Please try again." }).catch(() => { }); + } + return; + } } - if (action === 'restart') { - res.json({ message: '🔁 Restarting bot in 2 seconds...' }); - setTimeout(() => process.exit(1), 2000); + if (interaction.customId && interaction.customId.startsWith('tz_select_')) { + try { + await interaction.deferUpdate(); + console.log(`[TZ] Handling selection for ${interaction.customId}`); + + const matchId = interaction.customId.replace('tz_select_', ''); + const matchData = pendingMatches.get(matchId); + + if (!matchData) { + console.warn(`[TZ] Match data not found for ${matchId}`); + return interaction.followUp({ content: "❌ Match session expired or bot restarted. Please try the command again.", flags: [MessageFlags.Ephemeral] }); + } + + const offset = parseFloat(interaction.values[0]); + const baseTs = parseMatchTime(matchData.timeInput); + + if (!baseTs) { + return interaction.followUp({ content: "❌ Could not parse your time input. Use `9 PM` or `4/23 9 PM`.", flags: [MessageFlags.Ephemeral] }); + } + + const finalTs = baseTs - (offset * 3600); + const content = `⭐▬▬ 🏆 **LEAGUE MATCH** 🏆 ▬▬⭐\n🏟️ **${matchData.home}** vs **${matchData.away}**\n🔗 ${matchData.link}\n🕒 **Time**: ()\n🤝 Session\n⭐▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬⭐\n@everyone`; + + console.log(`[TZ] Attempting to send message to channel ${matchData.channelId}`); + const channel = await client.channels.fetch(matchData.channelId).catch(e => { + console.error("[TZ] Failed to fetch channel:", e); + return null; + }); + + if (!channel) { + return interaction.followUp({ content: "❌ Could not find the original channel. Please try again in a public channel.", flags: [MessageFlags.Ephemeral] }); + } + + const msg = await channel.send({ content, allowedMentions: { parse: ['everyone'] } }); + await msg.react('🟩').catch(() => { }); + await msg.react('🟥').catch(() => { }); + + storage.data.active_polls[interaction.guildId] = msg.id; + storage.save('active_polls'); + + pendingMatches.delete(matchId); + await interaction.editReply({ content: "✅ Match scheduled! I have posted it in the channel.", components: [] }); + console.log(`[TZ] Successfully completed match scheduling.`); + + } catch (err) { + console.error("[TZ] Critical Error:", err); + try { + await interaction.followUp({ content: "❌ A system error occurred while processing your selection.", flags: [MessageFlags.Ephemeral] }); + } catch (e) { } + } return; } +}); + +// Global Reaction Handler (Triggers Lineup) +client.on('messageReactionAdd', async (reaction, user) => { + if (user.bot) return; + if (reaction.partial) await reaction.fetch(); + + if (reaction.emoji.name === '🟩') { + const guildId = reaction.message.guildId; + const activePollId = storage.data.active_polls[guildId]; + + if (reaction.message.id === activePollId) { + const needed = 6; + const count = reaction.count - (reaction.me ? 1 : 0); + + if (count >= needed) { + const embed = UI.createEmbed(`${UI.icons.success} Friendly Ready!`, `We have **${count}** players! Starting lineup board...`, UI.colors.success); + await reaction.message.channel.send({ embeds: [embed] }); - res.json({ message: 'Unknown action.' }); + // Show interactive lineup board + await reaction.message.channel.send(createLineupView(guildId)); + + delete storage.data.active_polls[guildId]; + storage.save('active_polls'); + } + } + } }); -// --- Start --- -function startServer(client, cmdsData) { - const net = require('net'); - const checkServer = net.createServer(); +const TIMEZONE_MAP = { + 'EST': -5, 'EDT': -4, 'CST': -6, 'CDT': -5, 'MST': -7, 'MDT': -6, + 'PST': -8, 'PDT': -7, 'GMT': 0, 'UTC': 0, 'BST': 1, 'CET': 1, + 'CEST': 2, 'EET': 2, 'EEST': 3, 'MSK': 3, 'IST': 5.5, 'KST': 9, 'JST': 9 +}; + +const pendingMatches = new Map(); +const TIMEZONE_OPTIONS = [ + { label: 'UTC -11 (Midway)', value: '-11' }, + { label: 'UTC -10 (Hawaii)', value: '-10' }, + { label: 'UTC -9 (Alaska)', value: '-9' }, + { label: 'UTC -8 (Pacific Time)', value: '-8' }, + { label: 'UTC -7 (Mountain Time)', value: '-7' }, + { label: 'UTC -6 (Central Time)', value: '-6' }, + { label: 'UTC -5 (Eastern Time)', value: '-5' }, + { label: 'UTC -4 (Atlantic Time)', value: '-4' }, + { label: 'UTC -3 (Brazil/Argentina)', value: '-3' }, + { label: 'UTC -2 (Mid-Atlantic)', value: '-2' }, + { label: 'UTC -1 (Azores)', value: '-1' }, + { label: 'UTC +0 (GMT / London)', value: '0' }, + { label: 'UTC +1 (Central Europe)', value: '1' }, + { label: 'UTC +2 (Eastern Europe / Egypt)', value: '2' }, + { label: 'UTC +3 (Moscow / Saudi / Iraq / EEST)', value: '3' }, + { label: 'UTC +4 (Dubai / Baku)', value: '4' }, + { label: 'UTC +5 (Pakistan / Uzbekistan)', value: '5' }, + { label: 'UTC +5:30 (India / IST)', value: '5.5' }, + { label: 'UTC +6 (Bangladesh)', value: '6' }, + { label: 'UTC +7 (Thailand / Vietnam)', value: '7' }, + { label: 'UTC +8 (China / Singapore / Perth)', value: '8' }, + { label: 'UTC +9 (Japan / Korea)', value: '9' }, + { label: 'UTC +10 (Sydney / Guam)', value: '10' }, + { label: 'UTC +11 (Solomon Islands)', value: '11' }, + { label: 'UTC +12 (New Zealand / Fiji)', value: '12' } +]; + +function parseMatchTime(timeInput) { + if (!timeInput) return null; + const now = new Date(); + let targetDate = new Date(); + + // Support formats like "9 PM", "4/23 9 PM", "4-23 9PM" + const regex = /(?:(\d+)[/-](\d+)\s+)?(\d+)\s*(am|pm)/i; + const match = timeInput.match(regex); + if (match) { + const month = match[1]; + const day = match[2]; + let hours = parseInt(match[3]); + const ampm = match[4].toLowerCase(); + if (month && day) { + targetDate.setMonth(parseInt(month) - 1); + targetDate.setDate(parseInt(day)); + } + if (ampm === 'pm' && hours !== 12) hours += 12; + if (ampm === 'am' && hours === 12) hours = 0; + targetDate.setHours(hours, 0, 0, 0); + return Math.floor(targetDate.getTime() / 1000); + } + return null; +} - checkServer.on('listening', () => { - checkServer.close(() => { - console.log(`[*] Port ${port} is available, starting server...`); - startServerInternal(client, cmdsData); - }); - }); +// --- 4. LINEUP LOGIC --- +const FORMATIONS = { + "default": [["GK", "GK"], ["CB", "CB"], ["LB", "LB"], ["RB", "RB"], ["CM", "CM"], ["RW", "RW"], ["LW", "LW"]], + "3-1-2": [["GK", "GK"], ["CB", "CB"], ["LB", "LB"], ["RB", "RB"], ["CM", "CM"], ["ST1", "ST"], ["ST2", "ST"]], + "3-0-3": [["GK", "GK"], ["CB", "CB"], ["LB", "LB"], ["RB", "RB"], ["LW", "LW"], ["ST", "ST"], ["RW", "RW"]], + "3-2-1": [["GK", "GK"], ["CB", "CB"], ["LB", "LB"], ["RB", "RB"], ["CM1", "CM"], ["CM2", "CM"], ["ST", "ST"]], + "2-3-1": [["GK", "GK"], ["CB1", "CB"], ["CB2", "CB"], ["LCM", "LCM"], ["CM", "CM"], ["RCM", "RCM"], ["ST", "ST"]], + "2-2-2": [["GK", "GK"], ["CB1", "CB"], ["CB2", "CB"], ["CM1", "CM"], ["CM2", "CM"], ["ST1", "ST"], ["ST2", "ST"]], + "4-1-1": [["GK", "GK"], ["LCB", "LCB"], ["RCB", "RCB"], ["LB", "LB"], ["RB", "RB"], ["CDM", "CDM"], ["ST", "ST"]], + "3-3-0": [["GK", "GK"], ["CB", "CB"], ["LB", "LB"], ["RB", "RB"], ["LCM", "LCM"], ["CM", "CM"], ["RCM", "RCM"]], + "2-1-3": [["GK", "GK"], ["CB1", "CB"], ["CB2", "CB"], ["CM", "CM"], ["LW", "LW"], ["ST", "ST"], ["RW", "RW"]], + "1-4-1": [["GK", "GK"], ["CB", "CB"], ["LM", "LM"], ["LCM", "LCM"], ["RCM", "RCM"], ["RM", "RM"], ["ST", "ST"]], + "4-2-0": [["GK", "GK"], ["LCB", "LCB"], ["RCB", "RCB"], ["LB", "LB"], ["RB", "RB"], ["LCM", "LCM"], ["RCM", "RCM"]], + "3-1-3": [["GK", "GK"], ["CB", "CB"], ["LW", "LW"], ["ST", "ST"], ["RW", "RW"], ["ST1", "ST"], ["ST2", "ST"]], + "5-0-1": [["GK", "GK"], ["LCB", "LCB"], ["CB", "CB"], ["RCB", "RCB"], ["LB", "LB"], ["RB", "RB"], ["ST", "ST"]], + "3-1-1-1": [["GK", "GK"], ["CB", "CB"], ["LB", "LB"], ["RB", "RB"], ["CDM", "CDM"], ["CAM", "CAM"], ["ST", "ST"]], + "2-2-1-1": [["GK", "GK"], ["CB1", "CB"], ["CB2", "CB"], ["LCM", "LCM"], ["RCM", "RCM"], ["CAM", "CAM"], ["ST", "ST"]], + "4-1-2": [["GK", "GK"], ["LCB", "LCB"], ["RCB", "RCB"], ["CDM", "CDM"], ["LB", "LB"], ["RB", "RB"], ["ST", "ST"]], + "2-3-2": [["GK", "GK"], ["CB1", "CB"], ["CB2", "CB"], ["CM", "CM"], ["CAM", "CAM"], ["ST1", "ST"], ["ST2", "ST"]] +}; + +const guildLineups = new Map(); +const guildSelectedFormation = new Map(); +const lineupOwners = new Map(); +const activeLineupMessages = new Map(); + +function getGuildLineup(guildId) { + if (!guildLineups.get(guildId)) guildLineups.set(guildId, new Map()); + return guildLineups.get(guildId); +} - checkServer.on('error', (err) => { - if (err.code === 'EADDRINUSE') { - console.error(`[!] ERROR: Port ${port} is already in use. Another bot instance is running.`); - console.error(`[!] Please close other instances before starting this one.`); - process.exit(1); +function createLineupView(guildId) { + const lineup = getGuildLineup(guildId); + const formKey = guildSelectedFormation.get(guildId) || 'default'; + const formation = FORMATIONS[formKey]; + + let text = ""; + for (const [posKey, posLabel] of formation) { + const players = lineup.get(posKey) || []; + if (players.length > 0) { + const starter = players[0]; + const subs = players.slice(1); + let line = `🟩 **${posLabel}** ➔ <@${starter}>`; + if (subs.length > 0) { + line += ` (Substitute: ` + subs.map(p => `<@${p}>`).join(', ') + `)`; + } + text += line + "\n\n"; } else { - console.error(`[!] Server error: ${err.message}`); - process.exit(1); + text += `⬛ **${posLabel}** ➔ *Open*\n\n`; + } + } + + const embed = UI.createEmbed(`⚽ MATCH LINEUP — ${formKey}`, text, UI.colors.primary); + embed.setFooter({ text: "Click a position below to join/leave! Use the dropdown to change formation." }); + + const rows = []; + + // Formation Dropdown + const formOptions = Object.keys(FORMATIONS).slice(0, 25).map(k => ({ label: k, value: k })); + rows.push(new ActionRowBuilder().addComponents( + new StringSelectMenuBuilder() + .setCustomId('formation_select') + .setPlaceholder(`Current Formation: ${formKey} (Click to change)`) + .addOptions(formOptions) + )); + + // Position Buttons + let currentRow = new ActionRowBuilder(); + let currentPosCount = 0; + + formation.forEach(([posKey, posLabel]) => { + if (currentPosCount === 4) { + rows.push(currentRow); + currentRow = new ActionRowBuilder(); + currentPosCount = 0; } + const players = lineup.get(posKey) || []; + const btnLabel = posLabel; + + const btn = new ButtonBuilder() + .setCustomId(`join_${posKey}`) + .setLabel(players.length > 0 ? `${btnLabel}: ✅` : btnLabel) + .setStyle(players.length > 0 ? ButtonStyle.Secondary : ButtonStyle.Primary); + + if (players.length > 0) { + btn.setEmoji("✅"); + } + currentRow.addComponents(btn); + currentPosCount++; }); + if (currentPosCount > 0) rows.push(currentRow); - checkServer.listen(port, '0.0.0.0'); + // Clear and Done Buttons + rows.push(new ActionRowBuilder().addComponents( + new ButtonBuilder().setCustomId('lineup_done').setLabel('Show Final Image').setStyle(ButtonStyle.Success).setEmoji("🖼️"), + new ButtonBuilder().setCustomId('lineup_clear').setLabel('Clear Board').setStyle(ButtonStyle.Danger) + )); + + return { content: "@everyone\n**Lineup Session Active!**", embeds: [embed], components: rows, allowedMentions: { parse: ['everyone'] } }; } -function startServerInternal(client, cmdsData) { - hijackConsole(); - discordClient = client || null; - commandsDataRef = cmdsData || null; - - app.listen(port, '0.0.0.0', () => { - const finalRedirect = getRedirectUri(); - console.log(`[*] --------------------------------------------------`); - console.log(`[*] Web Server running on port ${port}`); - console.log(`[*] --------------------------------------------------`); - console.log(`[*] DASHBOARD URL: ${finalRedirect.replace('/callback', '').replace('/auth/callback', '')}`); - console.log(`[*] REDIRECT URI: ${finalRedirect}`); - console.log(`[*] --------------------------------------------------`); - console.log(`[*] IMPORTANT: Ensure the REDIRECT URI above is added exactly to your`); - console.log(`[*] Discord Developer Portal -> OAuth2 -> Redirects section.`); - console.log(`[*] --------------------------------------------------`); +// --- 5. MANAGER GUI LOGIC --- +const managerBoards = new Map(); + +function createManagerEditor(guildId, client) { + const lineup = getGuildLineup(guildId); + const formKey = guildSelectedFormation.get(guildId) || 'default'; + const formation = FORMATIONS[formKey]; + + let text = "🛠️ **LINEUP GUI EDITOR**\n▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\n"; + for (const [posKey, posLabel] of formation) { + const players = lineup.get(posKey) || []; + const userStr = players.length > 0 ? `<@${players[0]}>` : "---"; + text += `📦 **${posLabel}**: ${userStr}\n`; + } + text += "▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\n*Click a box below to edit a position!*"; + + const rows = []; + let currentRow = new ActionRowBuilder(); + let currentPosCount = 0; + + formation.forEach(([posKey, posLabel]) => { + if (currentPosCount === 4) { + rows.push(currentRow); + currentRow = new ActionRowBuilder(); + currentPosCount = 0; + } + const players = lineup.get(posKey) || []; + let valTitle = "---"; + if (players.length > 0 && client) { + const tempUser = client.users.cache.get(players[0]); + valTitle = tempUser ? tempUser.username : "Player"; + } else if (players.length > 0) { valTitle = "Player"; } + + currentRow.addComponents( + new ButtonBuilder() + .setCustomId(`man_edit_${posKey}`) + .setLabel(`${posLabel}: ${valTitle}`) + .setStyle(ButtonStyle.Secondary) + .setEmoji("🛠️") + ); + currentPosCount++; }); + if (currentPosCount > 0) rows.push(currentRow); + + // Add a Done/Submit button at the bottom + rows.push(new ActionRowBuilder().addComponents( + new ButtonBuilder().setCustomId('lineup_done').setLabel('Generate Final Image').setStyle(ButtonStyle.Success).setEmoji("🖼️") + )); + + return { content: text, components: rows }; +} + +// --- 5. 24/7 HEARTBEAT & RECOVERY --- +const fetch = require('node-fetch'); + +/** Public Space URL for keep-alive pings (HF injects these at runtime). */ +function getSpacePublicUrl() { + const host = (process.env.SPACE_HOST || '').trim(); + if (host) return host.startsWith('http') ? host : `https://${host}`; + const spaceId = (process.env.SPACE_ID || '').trim(); + if (spaceId) return `https://${spaceId.replace('/', '-').toLowerCase()}.hf.space`; + const author = (process.env.SPACE_AUTHOR_NAME || '').trim(); + const repo = (process.env.SPACE_REPO_NAME || '').trim(); + if (author && repo) return `https://${author}-${repo}`.toLowerCase().replace(/\s+/g, '') + '.hf.space'; + return ''; } +setInterval(async () => { + const url = getSpacePublicUrl(); + if (url) { + fetch(url, { method: 'GET' }).catch(() => { }); + } + + // Recovery Check: If client is not ready or WS is dead + if (client.isReady()) { + if (client.ws.status !== 0) { + console.log("[!] Gateway issues detected. Status:", client.ws.status); + console.log("[*] Performing emergency network rotation and restart..."); + networkFix.rotateEndpoint(); + + // Attempt to keep session alive or force a new login if really dead + if (client.ws.status === 7 || client.ws.status === 9) { // DISCONNECTED or INVALID_SESSION + process.exit(1); // Force a full container restart by PM2/HuggingFace + } + } + } +}, 2 * 60 * 1000); // Every 2 minutes + +async function showAutoFriendlyPanel(interaction) { + const guildId = interaction.guildId; + const config = storage.data.config[guildId] || {}; + const enabled = config.autoFriendlyEnabled || false; + const interval = config.autoFriendlyInterval || 40; + const channelId = config.autoFriendlyChannel; + + const embed = UI.createEmbed("🤖 Auto Friendly Settings", + `Configure the bot to automatically start friendly polls.\n\n` + + `**Status**: ${enabled ? '✅ Enabled' : '❌ Disabled'}\n` + + `**Interval**: \`${interval}\` minutes\n` + + `**Target Channel**: ${channelId ? `<#${channelId}>` : '`Not Set`'}`, + enabled ? UI.colors.success : UI.colors.primary + ); + + const row1 = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setCustomId('toggle_auto_friendly') + .setLabel(enabled ? 'Disable Auto Friendly' : 'Enable Auto Friendly') + .setStyle(enabled ? ButtonStyle.Danger : ButtonStyle.Success), + new ButtonBuilder() + .setCustomId('open_interval_modal') + .setLabel('Set Interval') + .setStyle(ButtonStyle.Primary) + ); + + const row2 = new ActionRowBuilder().addComponents( + new ChannelSelectMenuBuilder() + .setCustomId('auto_friendly_channel_select') + .setPlaceholder('Select target channel...') + .addChannelTypes(ChannelType.GuildText) + ); + + if (interaction.replied || interaction.deferred) { + await interaction.editReply({ embeds: [embed], components: [row1, row2] }); + } else { + await interaction.reply({ embeds: [embed], components: [row1, row2], ephemeral: true }); + } +} + +/** + * Application Entry Point + */ +async function initialize() { + let retryCount = 0; + const maxRetryDelay = 60000; // Cap at 1 minute between retries + + // STEP 1: Start the web server FIRST. + // HF will kill the container if port 7860 doesn't respond quickly. + console.log("[*] Starting Web Dashboard..."); + server.startServer(client, commandsData); + + // STEP 2: Give the container's network stack a moment to settle. + // On HF cold-starts, the outbound network may not be ready immediately. + console.log("[*] Waiting 3s for network stack to stabilize..."); + await new Promise(r => setTimeout(r, 3000)); + + // STEP 3: Start the Discord login loop. + login(); + + async function login() { + if (!TOKEN || TOKEN.length < 10) { + console.error("[CRITICAL] Discord Token is missing or malformed."); + process.exit(1); + } + + retryCount++; + try { + const currentApi = DISCORD_ENDPOINTS[currentEndpointIndex]; + console.log(`[*] 📡 VERIFYING: ${currentApi}...`); + + // Manual Force-Fetch to bypass Discord.js token-not-present error + const { request } = require('undici'); + let testRes = null; + let testError = null; + + try { + const response = await request(`${currentApi}/v10/users/@me`, { + method: 'GET', + headers: { + 'Authorization': `Bot ${TOKEN}`, + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' + }, + dispatcher: ultimateFix.dispatcher + }); + + if (response.statusCode === 200) testRes = { ok: true }; + else if (response.statusCode === 401) testRes = { status: 401 }; + else testError = { status: response.statusCode, message: "Blocked or Rate Limited" }; + } catch (e) { + testError = e; + } + + if (testRes && testRes.status === 401) { + console.error("[CRITICAL] ❌ TOKEN INVALID (401). Check Discord Dev Portal!"); + return; + } else if (testRes) { + console.log(`[SUCCESS] ✅ Connected via ${currentApi}`); + // Lock the bot to the tunnel + client.token = TOKEN; + client.options.rest.api = currentApi; + if (client.rest) client.rest.options.api = currentApi; + } else { + const errDetail = testError ? `${testError.status || 'TIMEOUT'} - ${testError.message}` : "Unknown Error"; + console.warn(`[BLOCKED] ⚠️ ${currentApi} failed: ${errDetail}`); + + if (process.env.DISCORD_BASE_URL && currentEndpointIndex === 0) { + console.log("[!] Still blocked via Worker. Retrying worker path in 5s..."); + await new Promise(r => setTimeout(r, 5000)); + login(); + return; + } + + currentEndpointIndex = (currentEndpointIndex + 1) % DISCORD_ENDPOINTS.length; + throw new Error("Cycle endpoints"); + } + + console.log(`[*] Attempting connection to Discord (Attempt ${retryCount}) using ${currentApi}...`); + + + // Destroy any stale connection before retrying + if (retryCount > 1) { + try { client.destroy(); } catch (_) { } + await new Promise(r => setTimeout(r, 1000)); + } + + await client.login(TOKEN); + console.log('[*] Discord login() resolved successfully.'); + if (connectionTimeout) clearTimeout(connectionTimeout); + retryCount = 0; // Reset on success + + } catch (err) { + const msg = err && err.message ? err.message : String(err); + console.error(`[ERROR] Discord login failed: ${msg}`); + + if (/intent|disallowed|4014|privileg/i.test(msg)) { + console.error('[HINT] https://discord.com/developers/applications → your app → Bot → Privileged Gateway Intents: turn ON "Server Members Intent" and "Message Content Intent", then restart this Space.'); + } + + // Token errors are fatal — don't retry forever + if (/token|unauthorized|authentication|403|401/i.test(msg) && retryCount >= 3) { + console.error('[CRITICAL] Token appears invalid after 3 attempts. Stopping retries.'); + return; + } + + const delay = Math.min(1000 * Math.pow(2, retryCount), maxRetryDelay); + console.log(`[*] Retrying in ${delay / 1000}s... (attempt ${retryCount})`); + setTimeout(login, delay); + } + } + + // --- 24/7 Heartbeat + Self-Healing --- + // Runs every 5 minutes (not hourly!) for faster detection of problems. + let disconnectedSince = null; + + setInterval(() => { + const up = process.uptime(); + const h = Math.floor(up / 3600); + const m = Math.floor(up / 60) % 60; + const connected = client.isReady(); + + console.log(`[24/7 HEARTBEAT] Uptime: ${h}h ${m}m | Discord: ${connected ? 'CONNECTED ✓' : 'DISCONNECTED ✗'}`); + + // Track how long we've been disconnected + if (!connected) { + if (!disconnectedSince) disconnectedSince = Date.now(); + const disconnectedMs = Date.now() - disconnectedSince; + + // If disconnected for over 10 minutes, force restart so HF rebuilds the container + if (disconnectedMs > 10 * 60 * 1000) { + console.error(`[!] Bot disconnected for ${Math.round(disconnectedMs / 60000)} minutes. Forcing process exit for container restart...`); + process.exit(1); + } + } else { + disconnectedSince = null; + } + + // Ping own public URL to prevent HF from sleeping the Space + const spaceHost = process.env.SPACE_HOST; + if (spaceHost) { + fetch(`https://${spaceHost}/healthz`).catch(() => { }); + } + // Also ping localhost as fallback + fetch(`http://0.0.0.0:${process.env.PORT || 7860}/healthz`).catch(() => { }); + }, 5 * 60 * 1000); // Every 5 minutes +} + +initialize().catch(err => { + console.error(">>> [BOOT] CRITICAL INITIALIZATION ERROR:", err); + process.exit(1); +}); -module.exports = { startServer, app };