File size: 3,602 Bytes
3c7e34b
 
 
 
 
 
 
 
 
5fb7488
3c7e34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fb7488
 
3c7e34b
 
 
 
 
 
90fc18c
3c7e34b
 
90fc18c
 
 
 
 
 
5fb7488
 
90fc18c
 
 
 
3c7e34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90fc18c
 
 
 
5fb7488
 
90fc18c
 
 
 
3c7e34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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 };