wsb-bott / src /systems /verification.js
APRKDEV's picture
Upload 43 files
e5c9966 verified
const { createEmbed } = require('../utils/embeds');
const { Colors } = require('../config');
const { stmts } = require('../database');
const { logVerification } = require('./logger');
/**
* Send the verification embed to the ✅・verify channel.
*/
async function sendVerificationEmbed(client) {
const row = await stmts.getState('channel_✅・verify');
if (!row) throw new Error('Verify channel not found in bot state.');
const channel = await client.channels.fetch(row.value);
if (!channel) throw new Error('Could not fetch verify channel.');
const embed = createEmbed({
title: '✅ Verification',
description: [
'> Welcome to **Wyvern Softworks**!',
'',
'React with ✅ below to verify yourself and gain access to the server.',
'',
'```',
'• You will receive the Verified role',
'• Removing your reaction will remove the role',
'```',
].join('\n'),
color: Colors.SUCCESS,
});
const msg = await channel.send({ embeds: [embed] });
await msg.react('✅');
// Store the message ID so we can listen for reactions
await stmts.setState('verify_message_id', msg.id);
await stmts.setState('verify_channel_id', channel.id);
return msg;
}
/**
* Handle verification reaction add.
* Works by checking channel name OR stored message ID (resilient to DB resets).
*/
async function handleVerifyReaction(reaction, user, client) {
// Check if this is a reaction in the verify channel on a bot message
const channel = reaction.message.channel;
const isVerifyChannel = channel.name === '✅・verify';
const isBotMessage = reaction.message.author?.id === client.user.id;
// Also check stored message ID as fallback
const stateVal = await stmts.getState('verify_message_id');
const verifyMsgId = stateVal;
const isStoredMsg = verifyMsgId && reaction.message.id === verifyMsgId;
if (!isVerifyChannel && !isStoredMsg) return false;
if (isVerifyChannel && !isBotMessage) return false;
if (reaction.emoji.name !== '✅') return false;
const guild = reaction.message.guild;
const member = await guild.members.fetch(user.id).catch(() => null);
if (!member) return false;
const role = guild.roles.cache.find(r => r.name === '@@ Verified');
if (!role) return false;
await member.roles.add(role);
await logVerification(client, user, 'verified');
return true;
}
/**
* Handle verification reaction remove.
*/
async function handleVerifyReactionRemove(reaction, user, client) {
const channel = reaction.message.channel;
const isVerifyChannel = channel.name === '✅・verify';
const isBotMessage = reaction.message.author?.id === client.user.id;
const stateVal = await stmts.getState('verify_message_id');
const verifyMsgId = stateVal;
const isStoredMsg = verifyMsgId && reaction.message.id === verifyMsgId;
if (!isVerifyChannel && !isStoredMsg) return false;
if (isVerifyChannel && !isBotMessage) return false;
if (reaction.emoji.name !== '✅') return false;
const guild = reaction.message.guild;
const member = await guild.members.fetch(user.id).catch(() => null);
if (!member) return false;
const role = guild.roles.cache.find(r => r.name === '@@ Verified');
if (!role) return false;
await member.roles.remove(role);
await logVerification(client, user, 'unverified');
return true;
}
module.exports = { sendVerificationEmbed, handleVerifyReaction, handleVerifyReactionRemove };