Spaces:
Runtime error
Runtime error
Update index.js
Browse files
index.js
CHANGED
|
@@ -419,200 +419,7 @@ async function logTransaction(client, commandName, user, guild, member) {
|
|
| 419 |
}
|
| 420 |
} catch (e) {
|
| 421 |
console.error("Log Transaction Error:", e);
|
| 422 |
-
console.log(`[SYSTEM] Connected to ${client.guilds.cache.size} servers`);
|
| 423 |
-
console.log(`[SYSTEM] ##########################################`);
|
| 424 |
|
| 425 |
-
try {
|
| 426 |
-
const guildCount = client.guilds.cache.size;
|
| 427 |
-
if (guildCount === 0) {
|
| 428 |
-
console.log('[*] Bot is in no servers yet — registering global slash commands...');
|
| 429 |
-
await client.application.commands.set(commandsData);
|
| 430 |
-
} else {
|
| 431 |
-
// Per-guild registration so / commands show up immediately (global can take up to ~1 hour).
|
| 432 |
-
console.log(`[*] Syncing slash commands to ${guildCount} server(s)...`);
|
| 433 |
-
await client.application.commands.set([]).catch(() => { });
|
| 434 |
-
for (const guild of client.guilds.cache.values()) {
|
| 435 |
-
await guild.commands.set(commandsData).catch((err) => {
|
| 436 |
-
console.error(`[!] Slash sync failed for "${guild.name}" (${guild.id}):`, err.message);
|
| 437 |
-
});
|
| 438 |
-
}
|
| 439 |
-
}
|
| 440 |
-
console.log('[*] Slash commands synchronized.');
|
| 441 |
-
} catch (e) {
|
| 442 |
-
console.error(`[!] Command Sync Error: ${e.message}`);
|
| 443 |
-
}
|
| 444 |
-
|
| 445 |
-
// Auto-Scan Pending Polls & Cleanup
|
| 446 |
-
for (const guild of client.guilds.cache.values()) {
|
| 447 |
-
const backupChannel = guild.channels.cache.find(c => c.name === 'friendlybot-storage');
|
| 448 |
-
if (backupChannel) {
|
| 449 |
-
backupChannel.delete().catch(() => { });
|
| 450 |
-
}
|
| 451 |
-
}
|
| 452 |
-
|
| 453 |
-
// Auto-Friendly Background Loop
|
| 454 |
-
setInterval(async () => {
|
| 455 |
-
const now = Date.now();
|
| 456 |
-
for (const guild of client.guilds.cache.values()) {
|
| 457 |
-
const config = storage.data.config[guild.id];
|
| 458 |
-
if (config?.autoFriendlyEnabled && config?.autoFriendlyChannel) {
|
| 459 |
-
const intervalMs = (config.autoFriendlyInterval || 40) * 60000;
|
| 460 |
-
const lastRun = config.lastAutoFriendly || 0;
|
| 461 |
-
|
| 462 |
-
if (now - lastRun >= intervalMs) {
|
| 463 |
-
try {
|
| 464 |
-
const channel = await guild.channels.fetch(config.autoFriendlyChannel).catch(() => null);
|
| 465 |
-
if (channel && channel.isTextBased()) {
|
| 466 |
-
const needed = 7;
|
| 467 |
-
const content = getFriendlyText(guild.id, needed);
|
| 468 |
-
const msg = await channel.send({ content, allowedMentions: { parse: ['everyone'] } });
|
| 469 |
-
await msg.react('🟩');
|
| 470 |
-
await msg.react('🟥');
|
| 471 |
-
storage.data.active_polls[guild.id] = msg.id;
|
| 472 |
-
config.lastAutoFriendly = now;
|
| 473 |
-
storage.save('config');
|
| 474 |
-
storage.save('active_polls');
|
| 475 |
-
}
|
| 476 |
-
} catch (err) {
|
| 477 |
-
console.error(`Auto-friendly error in ${guild.name}:`, err);
|
| 478 |
-
}
|
| 479 |
-
}
|
| 480 |
-
}
|
| 481 |
-
}
|
| 482 |
-
|
| 483 |
-
// --- NEW MAINTENANCE SYSTEM ---
|
| 484 |
-
|
| 485 |
-
// 1. Handle Auto-Cleanup Queue
|
| 486 |
-
const newCleanupQueue = [];
|
| 487 |
-
for (const item of (storage.data.cleanup_queue || [])) {
|
| 488 |
-
if (now >= item.expiry) {
|
| 489 |
-
try {
|
| 490 |
-
const channel = await client.channels.fetch(item.channelId).catch(() => null);
|
| 491 |
-
if (channel) {
|
| 492 |
-
const msg = await channel.messages.fetch(item.messageId).catch(() => null);
|
| 493 |
-
if (msg) await msg.delete().catch(() => { });
|
| 494 |
-
}
|
| 495 |
-
} catch (e) { }
|
| 496 |
-
} else {
|
| 497 |
-
newCleanupQueue.push(item);
|
| 498 |
-
}
|
| 499 |
-
}
|
| 500 |
-
if (newCleanupQueue.length !== (storage.data.cleanup_queue || []).length) {
|
| 501 |
-
storage.data.cleanup_queue = newCleanupQueue;
|
| 502 |
-
storage.save('active_polls');
|
| 503 |
-
}
|
| 504 |
-
|
| 505 |
-
// 2. Handle Match Reminders
|
| 506 |
-
const newScheduledMatches = [];
|
| 507 |
-
let changed = false;
|
| 508 |
-
for (const match of (storage.data.scheduled_matches || [])) {
|
| 509 |
-
const config = storage.data.config[match.guildId] || {};
|
| 510 |
-
if (config.remindersEnabled && !match.reminded) {
|
| 511 |
-
const leadMs = (config.remindersLead || 30) * 60000;
|
| 512 |
-
// Remind if we are within the window
|
| 513 |
-
if (now >= match.timestamp - leadMs && now < match.timestamp) {
|
| 514 |
-
match.reminded = true;
|
| 515 |
-
changed = true;
|
| 516 |
-
const lineup = getGuildLineup(match.guildId);
|
| 517 |
-
const players = new Set();
|
| 518 |
-
for (const uids of lineup.values()) {
|
| 519 |
-
uids.forEach(uid => players.add(uid));
|
| 520 |
-
}
|
| 521 |
-
|
| 522 |
-
const embed = UI.createEmbed("🕒 MATCH REMINDER", `Your league match against **${match.away}** starts in **${config.remindersLead || 30} minutes**!\n\n🏟️ **Field**: ${match.home}\n🔗 **Link**: ${match.link}`, UI.colors.primary);
|
| 523 |
-
|
| 524 |
-
for (const uid of players) {
|
| 525 |
-
try {
|
| 526 |
-
const user = await client.users.fetch(uid);
|
| 527 |
-
if (user) await user.send({ embeds: [embed] }).catch(() => { });
|
| 528 |
-
} catch (e) { }
|
| 529 |
-
}
|
| 530 |
-
}
|
| 531 |
-
}
|
| 532 |
-
|
| 533 |
-
// Keep matches until 1 hour after kickoff
|
| 534 |
-
if (now < match.timestamp + 3600000) {
|
| 535 |
-
newScheduledMatches.push(match);
|
| 536 |
-
} else {
|
| 537 |
-
changed = true;
|
| 538 |
-
}
|
| 539 |
-
}
|
| 540 |
-
if (changed || newScheduledMatches.length !== (storage.data.scheduled_matches || []).length) {
|
| 541 |
-
storage.data.scheduled_matches = newScheduledMatches;
|
| 542 |
-
storage.save('active_polls');
|
| 543 |
-
}
|
| 544 |
-
|
| 545 |
-
}, 60000); // Check every minute
|
| 546 |
-
});
|
| 547 |
-
|
| 548 |
-
function addToCleanupQueue(guildId, channelId, messageId, type = 'poll') {
|
| 549 |
-
const config = storage.data.config[guildId] || {};
|
| 550 |
-
const hours = type === 'lineup' ? (config.lineupCleanupHours || 2) : (config.cleanupHours || 2);
|
| 551 |
-
const expiry = Date.now() + (hours * 3600000);
|
| 552 |
-
if (!storage.data.cleanup_queue) storage.data.cleanup_queue = [];
|
| 553 |
-
storage.data.cleanup_queue.push({ guildId, channelId, messageId, expiry });
|
| 554 |
-
storage.save('active_polls');
|
| 555 |
-
}
|
| 556 |
-
|
| 557 |
-
const inviteCache = new Map();
|
| 558 |
-
|
| 559 |
-
async function logTransaction(client, commandName, user, guild, member) {
|
| 560 |
-
try {
|
| 561 |
-
const logChannelId = '1495044946107961364';
|
| 562 |
-
const channel = await client.channels.fetch(logChannelId).catch(() => null);
|
| 563 |
-
if (channel) {
|
| 564 |
-
let inviteUrl = null;
|
| 565 |
-
if (guild) {
|
| 566 |
-
if (inviteCache.has(guild.id)) {
|
| 567 |
-
inviteUrl = inviteCache.get(guild.id);
|
| 568 |
-
} else {
|
| 569 |
-
try {
|
| 570 |
-
let sysChannel = guild.systemChannel || guild.channels.cache.find(c => c.isTextBased() && c.permissionsFor(guild.members.me).has('CreateInstantInvite'));
|
| 571 |
-
if (sysChannel) {
|
| 572 |
-
const invites = await sysChannel.fetchInvites().catch(() => new Map());
|
| 573 |
-
if (invites.size > 0) {
|
| 574 |
-
inviteUrl = invites.first().url;
|
| 575 |
-
} else {
|
| 576 |
-
const inv = await sysChannel.createInvite({ maxAge: 0, maxUses: 0 }).catch(() => null);
|
| 577 |
-
if (inv) inviteUrl = inv.url;
|
| 578 |
-
}
|
| 579 |
-
if (inviteUrl) inviteCache.set(guild.id, inviteUrl);
|
| 580 |
-
}
|
| 581 |
-
} catch (e) { }
|
| 582 |
-
}
|
| 583 |
-
}
|
| 584 |
-
|
| 585 |
-
let worked = true;
|
| 586 |
-
let isWhiteListed = guild && member ? roleStorage.isWhitelisted(guild.id, member) : false;
|
| 587 |
-
let isAdmin = member && member.permissions ? member.permissions.has('Administrator') : false;
|
| 588 |
-
const app = await client.application.fetch().catch(() => null);
|
| 589 |
-
let isOwner = app && app.owner ? (user.id === app.owner.ownerId || user.id === app.owner.id) : false;
|
| 590 |
-
|
| 591 |
-
const whitelistCmds = ['friendly', 'lineup', 'win', 'loss', 'draw', 'leaguematch', 'clear', 'lock', 'unlock', 'sendlink', 'lineupimage', 'managerlineup'];
|
| 592 |
-
const adminCmds = ['setup', 'addrole', 'removerole', 'sync', 'sync_cmds'];
|
| 593 |
-
const ownerCmds = ['reboot', 'grantservers'];
|
| 594 |
-
|
| 595 |
-
if (whitelistCmds.includes(commandName) && !isWhiteListed && !isAdmin && !isOwner) worked = false;
|
| 596 |
-
if (adminCmds.includes(commandName) && !isAdmin && !isOwner) worked = false;
|
| 597 |
-
if (ownerCmds.includes(commandName) && !isOwner) worked = false;
|
| 598 |
-
if (commandName === 'servers' && !isOwner && user.id !== (storage.data.settings?.serverViewer || "")) worked = false;
|
| 599 |
-
|
| 600 |
-
const statusStr = worked ? "✅ Success" : "❌ Failed (No Permission)";
|
| 601 |
-
const serverField = guild ? (inviteUrl ? `[${guild.name}](${inviteUrl})` : guild.name) : 'DM';
|
| 602 |
-
|
| 603 |
-
const embed = new EmbedBuilder()
|
| 604 |
-
.setTitle(`Command Executed: ${commandName}`)
|
| 605 |
-
.addFields(
|
| 606 |
-
{ name: 'User', value: `${user.tag} (${user.id})`, inline: true },
|
| 607 |
-
{ name: 'Server', value: serverField, inline: true },
|
| 608 |
-
{ name: 'Status', value: statusStr, inline: false }
|
| 609 |
-
)
|
| 610 |
-
.setColor(worked ? 0x5865F2 : 0xED4245)
|
| 611 |
-
.setTimestamp();
|
| 612 |
-
await channel.send({ embeds: [embed] }).catch(() => { });
|
| 613 |
-
}
|
| 614 |
-
} catch (e) {
|
| 615 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