Spaces:
Runtime error
Runtime error
Update index.js
Browse files
index.js
CHANGED
|
@@ -1354,6 +1354,121 @@ async function sendNoPermission(ctx) {
|
|
| 1354 |
|
| 1355 |
// Interaction Handler
|
| 1356 |
client.on('interactionCreate', async (interaction) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1357 |
// Handle slash commands in DMs
|
| 1358 |
if (!interaction.guildId) {
|
| 1359 |
if (interaction.isChatInputCommand()) {
|
|
|
|
| 1354 |
|
| 1355 |
// Interaction Handler
|
| 1356 |
client.on('interactionCreate', async (interaction) => {
|
| 1357 |
+
// 1. Handle Timezone Selection IMMEDIATELY (Priority)
|
| 1358 |
+
if (interaction.isStringSelectMenu() && interaction.customId && interaction.customId.startsWith('tz_select_')) {
|
| 1359 |
+
try {
|
| 1360 |
+
await interaction.deferUpdate().catch(() => {});
|
| 1361 |
+
const matchId = interaction.customId.replace('tz_select_', '');
|
| 1362 |
+
const matchData = pendingMatches.get(matchId);
|
| 1363 |
+
|
| 1364 |
+
if (!matchData) {
|
| 1365 |
+
return interaction.followUp({ content: "❌ Match session expired or bot restarted. Please try the command again.", flags: [MessageFlags.Ephemeral] });
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
const offset = parseFloat(interaction.values[0]);
|
| 1369 |
+
let timeStr = matchData.timeInput;
|
| 1370 |
+
|
| 1371 |
+
if (timeStr.includes(' / ')) {
|
| 1372 |
+
timeStr = timeStr.split(' / ').pop().trim();
|
| 1373 |
+
}
|
| 1374 |
+
|
| 1375 |
+
let normalized = timeStr.replace(/(\d+)\s*(AM|PM)/i, "$1:00 $2");
|
| 1376 |
+
const targetDate = new Date();
|
| 1377 |
+
const hoursMatch = normalized.match(/(\d+):(\d+)\s*(am|pm)/i) || normalized.match(/(\d+)\s*(am|pm)/i);
|
| 1378 |
+
|
| 1379 |
+
if (!hoursMatch) {
|
| 1380 |
+
return interaction.followUp({ content: "❌ Could not parse your time input. Please use a format like `9 PM`.", flags: [MessageFlags.Ephemeral] });
|
| 1381 |
+
}
|
| 1382 |
+
|
| 1383 |
+
let hours = parseInt(hoursMatch[1]);
|
| 1384 |
+
const minutes = parseInt(hoursMatch[2] || "0");
|
| 1385 |
+
const ampm = (hoursMatch[3] || "").toLowerCase();
|
| 1386 |
+
|
| 1387 |
+
if (ampm === 'pm' && hours !== 12) hours += 12;
|
| 1388 |
+
if (ampm === 'am' && hours === 12) hours = 0;
|
| 1389 |
+
|
| 1390 |
+
const dateMatch = normalized.match(/(\d+)[/-](\d+)[/-](\d+)/) || normalized.match(/(\w+)\s+(\d+),?\s+(\d+)/);
|
| 1391 |
+
if (dateMatch) {
|
| 1392 |
+
const d = new Date(normalized);
|
| 1393 |
+
if (!isNaN(d.getTime())) {
|
| 1394 |
+
targetDate.setUTCFullYear(d.getFullYear());
|
| 1395 |
+
targetDate.setUTCMonth(d.getMonth());
|
| 1396 |
+
targetDate.setUTCDate(d.getDate());
|
| 1397 |
+
}
|
| 1398 |
+
}
|
| 1399 |
+
|
| 1400 |
+
targetDate.setUTCHours(hours, minutes, 0, 0);
|
| 1401 |
+
const finalTs = Math.floor(targetDate.getTime() / 1000) - (offset * 3600);
|
| 1402 |
+
|
| 1403 |
+
if (matchData.type === 'friendly') {
|
| 1404 |
+
if (!storage.data.scheduled_matches) storage.data.scheduled_matches = [];
|
| 1405 |
+
storage.data.scheduled_matches.push({
|
| 1406 |
+
guildId: interaction.guildId,
|
| 1407 |
+
type: 'friendly',
|
| 1408 |
+
timeInput: matchData.timeInput,
|
| 1409 |
+
timestamp: finalTs * 1000,
|
| 1410 |
+
reminded: false
|
| 1411 |
+
});
|
| 1412 |
+
storage.save('active_polls');
|
| 1413 |
+
|
| 1414 |
+
await interaction.editReply({ content: `✅ Friendly scheduled for <t:${finalTs}:F>!`, components: [] });
|
| 1415 |
+
|
| 1416 |
+
if (matchData.friendlyType === 'voting') {
|
| 1417 |
+
const lineup = getGuildLineup(interaction.guildId);
|
| 1418 |
+
lineup.clear();
|
| 1419 |
+
saveLineup();
|
| 1420 |
+
const view = createLineupView(interaction.guildId);
|
| 1421 |
+
const lineupMsg = await interaction.channel.send(view);
|
| 1422 |
+
await lineupMsg.react('🟩').catch(() => { });
|
| 1423 |
+
await lineupMsg.react('🟥').catch(() => { });
|
| 1424 |
+
activeLineupMessages.set(interaction.guildId, { channelId: interaction.channelId, messageId: lineupMsg.id });
|
| 1425 |
+
addToCleanupQueue(interaction.guildId, interaction.channelId, lineupMsg.id, 'lineup');
|
| 1426 |
+
|
| 1427 |
+
if (storage.data.scheduled_matches.length > 0) {
|
| 1428 |
+
storage.data.scheduled_matches[storage.data.scheduled_matches.length - 1].messageId = lineupMsg.id;
|
| 1429 |
+
storage.data.scheduled_matches[storage.data.scheduled_matches.length - 1].channelId = interaction.channelId;
|
| 1430 |
+
storage.save('active_polls');
|
| 1431 |
+
}
|
| 1432 |
+
} else {
|
| 1433 |
+
const guildId = interaction.guildId;
|
| 1434 |
+
guildSelectedFormationA.set(guildId, matchData.selectedFormation);
|
| 1435 |
+
const map = new Map();
|
| 1436 |
+
guildLineups.set(guildId, map);
|
| 1437 |
+
saveLineup();
|
| 1438 |
+
const editor = createManagerEditor(guildId, client);
|
| 1439 |
+
const editorMsg = await interaction.channel.send(editor);
|
| 1440 |
+
managerBoards.set(guildId, { channelId: interaction.channelId, messageId: editorMsg.id });
|
| 1441 |
+
}
|
| 1442 |
+
} else {
|
| 1443 |
+
const content = `⭐▬▬ 🏆 **LEAGUE MATCH** 🏆 ▬▬⭐\n🏟️ **${matchData.home}** vs **${matchData.away}**\n🔗 ${matchData.link}\n🕒 **Time**: <t:${finalTs}:F> (<t:${finalTs}:R>)\n🤝 Session\n⭐▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬⭐\n@everyone`;
|
| 1444 |
+
const channel = await client.channels.fetch(matchData.channelId).catch(() => null);
|
| 1445 |
+
if (!channel) return interaction.followUp({ content: "❌ Original channel not found.", flags: [MessageFlags.Ephemeral] });
|
| 1446 |
+
const msg = await channel.send({ content, allowedMentions: { parse: ['everyone', 'roles', 'users'] } });
|
| 1447 |
+
await msg.react('🟩').catch(() => { });
|
| 1448 |
+
await msg.react('🟥').catch(() => { });
|
| 1449 |
+
storage.data.active_polls[interaction.guildId] = msg.id;
|
| 1450 |
+
if (!storage.data.scheduled_matches) storage.data.scheduled_matches = [];
|
| 1451 |
+
storage.data.scheduled_matches.push({
|
| 1452 |
+
guildId: interaction.guildId,
|
| 1453 |
+
home: matchData.home,
|
| 1454 |
+
away: matchData.away,
|
| 1455 |
+
link: matchData.link,
|
| 1456 |
+
timestamp: finalTs * 1000,
|
| 1457 |
+
reminded: false,
|
| 1458 |
+
messageId: msg.id,
|
| 1459 |
+
channelId: interaction.channelId
|
| 1460 |
+
});
|
| 1461 |
+
storage.save('active_polls');
|
| 1462 |
+
addToCleanupQueue(interaction.guildId, interaction.channelId, msg.id, 'poll');
|
| 1463 |
+
await interaction.editReply({ content: "✅ Match scheduled! I have posted it in the channel.", components: [] });
|
| 1464 |
+
}
|
| 1465 |
+
pendingMatches.delete(matchId);
|
| 1466 |
+
} catch (err) {
|
| 1467 |
+
console.error("[TZ] Priority Error:", err);
|
| 1468 |
+
}
|
| 1469 |
+
return;
|
| 1470 |
+
}
|
| 1471 |
+
|
| 1472 |
// Handle slash commands in DMs
|
| 1473 |
if (!interaction.guildId) {
|
| 1474 |
if (interaction.isChatInputCommand()) {
|