AeThex-Bot-Master/aethex-bot/commands/verify.js
sirpiglr 73ff5bcce3 Add verification status command and improve verification process
Implement upsert for verification codes to fix race conditions, add a webhook listener for role assignment upon verification, introduce error handling for service unavailability, and create a new `/verify-status` command.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: aed2e46d-25bb-4b73-81a1-bb9e8437c261
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Event-Id: 9196f58c-47c2-4081-b01f-74e1fcdae7cd
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/3bdfff67-975a-46ad-9845-fbb6b4a4c4b5/aed2e46d-25bb-4b73-81a1-bb9e8437c261/3tJ1Z1J
Replit-Helium-Checkpoint-Created: true
2025-12-13 10:05:53 +00:00

120 lines
3.9 KiB
JavaScript

const {
SlashCommandBuilder,
EmbedBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
} = require("discord.js");
const { syncRolesAcrossGuilds } = require("../utils/roleManager");
async function checkAethexAvailability() {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const response = await fetch('https://aethex.dev/api/health', {
method: 'HEAD',
signal: controller.signal
}).catch(() => null);
clearTimeout(timeoutId);
return response && response.ok;
} catch {
return false;
}
}
module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("Link your Discord account to your AeThex account"),
async execute(interaction, supabase, client) {
if (!supabase) {
return interaction.reply({ content: "This feature requires Supabase to be configured.", ephemeral: true });
}
await interaction.deferReply({ ephemeral: true });
try {
// Check if aethex.dev is reachable
const isAethexUp = await checkAethexAvailability();
if (!isAethexUp) {
const embed = new EmbedBuilder()
.setColor(0xff9500)
.setTitle("⚠️ Service Temporarily Unavailable")
.setDescription("The AeThex verification service is currently unavailable.")
.addFields(
{ name: "💡 What to do", value: "Please try again in a few minutes." },
{ name: "📊 Status", value: "Check [status.aethex.dev](https://status.aethex.dev) for updates." }
);
return await interaction.editReply({ embeds: [embed] });
}
const { data: existingLink } = await supabase
.from("discord_links")
.select("*")
.eq("discord_id", interaction.user.id)
.maybeSingle();
if (existingLink) {
const embed = new EmbedBuilder()
.setColor(0x00ff00)
.setTitle("✅ Already Linked")
.setDescription(
`Your Discord account is already linked to AeThex (User ID: ${existingLink.user_id})`,
);
return await interaction.editReply({ embeds: [embed] });
}
// Generate verification code
const verificationCode = Math.random()
.toString(36)
.substring(2, 8)
.toUpperCase();
const expiresAt = new Date(Date.now() + 15 * 60 * 1000); // 15 minutes
// Store verification code with Discord username (upsert to prevent race condition)
await supabase.from("discord_verifications").upsert({
discord_id: interaction.user.id,
verification_code: verificationCode,
username: interaction.user.username,
expires_at: expiresAt.toISOString(),
}, { onConflict: 'discord_id' });
const verifyUrl = `https://aethex.dev/discord-verify?code=${verificationCode}`;
const embed = new EmbedBuilder()
.setColor(0x7289da)
.setTitle("🔗 Link Your AeThex Account")
.setDescription(
"Click the button below to link your Discord account to AeThex.",
)
.addFields(
{ name: "⏱️ Expires In", value: "15 minutes" },
{ name: "📝 Verification Code", value: `\`${verificationCode}\`` },
)
.setFooter({ text: "Your security code will expire in 15 minutes" });
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setLabel("Link Account")
.setStyle(ButtonStyle.Link)
.setURL(verifyUrl),
);
await interaction.editReply({ embeds: [embed], components: [row] });
} catch (error) {
console.error("Verify command error:", error);
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle("❌ Error")
.setDescription(
"Failed to generate verification code. Please try again.",
);
await interaction.editReply({ embeds: [embed] });
}
},
};