-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactionHandler.js
More file actions
106 lines (86 loc) · 3.13 KB
/
Copy pathreactionHandler.js
File metadata and controls
106 lines (86 loc) · 3.13 KB
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
102
103
104
105
106
// Reaction handler for flag approval/disapproval
const db = require('./db');
async function handleFlagReaction(api, reaction, userId, isAdding) {
try {
// Only handle check and x reactions
if (reaction.emoji.name !== '✅' && reaction.emoji.name !== '❌') {
return;
}
// Get the message
const message = await api.channels.getMessage(reaction.channel_id, reaction.message_id);
if (!message || !message.embeds || message.embeds.length === 0) {
return;
}
// Extract flag ID from embed footer
const footer = message.embeds[0].footer?.text || '';
const flagIdMatch = footer.match(/Flag ID: (\d+)/);
if (!flagIdMatch) {
return;
}
const flagId = parseInt(flagIdMatch[1]);
const guildId = reaction.guild_id;
// Get flag from database
const flags = await db.getPendingFlags(guildId, 100);
const flag = flags.find(f => f.id === flagId);
if (!flag) {
return;
}
// Only process if reaction is being added
if (!isAdding) {
return;
}
// Approve with check
if (reaction.emoji.name === '✅') {
await db.updateFlagStatus(guildId, flag.message_id, 'approved', userId, 'Approved via reaction');
// Update the message
try {
const updatedEmbed = message.embeds[0];
updatedEmbed.footer = { text: `✅ Approved by <@${userId}>` };
updatedEmbed.color = 3066993;
await api.channels.editMessage(reaction.channel_id, reaction.message_id, {
embeds: [updatedEmbed]
});
} catch (e) {}
console.log(`✅ Flag #${flagId} approved via reaction`);
}
// Disapprove with x
if (reaction.emoji.name === '❌') {
// Delete the original message
try {
await api.channels.deleteMessage(flag.channel_id, flag.message_id);
} catch (e) {}
// Add infraction
try {
await db.addInfraction(flag.user_id, guildId, 'automod', flag.reason, userId, true);
} catch (e) {}
// Update flag status
await db.updateFlagStatus(guildId, flag.message_id, 'deleted', userId, 'Disapproved via reaction');
// Update the message
try {
const updatedEmbed = message.embeds[0];
updatedEmbed.footer = { text: `❌ Disapproved by <@${userId}>` };
updatedEmbed.color = 15158332;
await api.channels.editMessage(reaction.channel_id, reaction.message_id, {
embeds: [updatedEmbed]
});
} catch (e) {}
// Send DM to user
try {
const dmChannel = await api.users.createDM(flag.user_id);
await api.channels.createMessage(dmChannel.id, {
embeds: [{
title: '⚠️ Message Deleted',
description: 'Your flagged message was deleted by a moderator.',
color: 15105570,
fields: [{ name: 'Reason', value: flag.reason }],
timestamp: new Date().toISOString()
}]
});
} catch (e) {}
console.log(`✅ Flag #${flagId} disapproved via reaction`);
}
} catch (error) {
console.error('Reaction handler error:', error.message);
}
}
module.exports = { handleFlagReaction };