-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (106 loc) · 4.18 KB
/
index.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const { Client } = require('discord.js');
const crypto = require('crypto');
const config = require('./config.json');
const { Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
let messageHashes = {};
function calculateDelay(count) {
return Math.min(6 * 60 * 60 * 1000, 2000 * Math.pow(4, count));
}
client.once('ready', () => {
console.log('Bot is online');
if (fs.existsSync('data.json')) {
try {
const data = fs.readFileSync('data.json', 'utf8');
const parsedData = JSON.parse(data);
messageHashes = parsedData.hashes || {};
} catch (err) {
console.error('Error reading file:', err);
}
}
});
client.on('message', async (message) => {
if (message.author.bot || message.channel.id !== config.channelID) return;
const contentWithoutSpecialChars = message.content.replace(/[^a-zA-Z0-9]/g, '');
const lowercaseContent = contentWithoutSpecialChars.toLowerCase();
const hash = crypto.createHash('md5').update(lowercaseContent).digest('hex');
// if (message.reactions.cache.has(emojiToCheck) && message.reactions.cache.get(emojiToCheck).count >= requiredReactions) {
// console.log(`Detected ${requiredReactions} reactions of ${emojiToCheck} on a message.`);
// const userPermissions = message.channel.permissionsFor(message.author);
// if (userPermissions && userPermissions.has('SEND_MESSAGES')) {
// await message.channel.updateOverwrite(message.author, {
// SEND_MESSAGES: false,
// });
// console.log(`User ${message.author.tag} has been restricted from sending messages in this channel.`);
// }
// }
if (messageHashes[hash]) {
const count = messageHashes[hash].count || 0;
const delay = calculateDelay(count);
const userPermissions = message.channel.permissionsFor(message.author);
if (userPermissions && userPermissions.has('SEND_MESSAGES')) {
await message.channel.updateOverwrite(message.author, {
SEND_MESSAGES: false,
});
setTimeout(async () => {
await message.channel.updateOverwrite(message.author, {
SEND_MESSAGES: true,
});
}, delay);
}
const deletedContent = message.content;
message.delete();
try {
const userToDM = await client.users.fetch(message.author.id);
const countdownTime = new Date(Date.now() + delay);
const unixTimestamp = Math.floor(countdownTime.getTime() / 1000);
await userToDM.send(`Sending "${deletedContent}" is very unwise. You can post again <t:${unixTimestamp}:R>.`);
}
catch (error) {
console.error('Error sending DM:', error);
}
messageHashes[hash].count = count + 1;
messageHashes[hash].expireTimestamp = Date.now() + delay;
}
else {
messageHashes[hash] = {
count: 1,
lowercaseContent: lowercaseContent, // Store lowercase content
};
console.log(`Message "${message.content}" hashed to ${hash}`);
}
});
client.on('messageReactionAdd', async (reaction, user) => {
const emojiToCheck = '➖'; // Replace with your emoji to check
const requiredReactions = 10;
try {
// Fetch the message associated with the reaction
const message = reaction.message;
// Your logic for handling reactions added to any message
if (message.reactions.cache.get(emojiToCheck).count >= requiredReactions) {
const userPermissions = message.channel.permissionsFor(message.author);
if (userPermissions && userPermissions.has('SEND_MESSAGES')) {
await message.channel.updateOverwrite(message.author, {
SEND_MESSAGES: false,
});
console.log('Saw specified emote! They are past limit!');
}
console.log('Saw reaction!');
}
} catch (error) {
console.error('Error handling reaction:', error);
}
});
client.login(config.token);
setInterval(() => {
const now = Date.now();
for (const hash in messageHashes) {
if (messageHashes[hash].expireTimestamp && messageHashes[hash].expireTimestamp <= now) {
delete messageHashes[hash];
}
}
fs.writeFile('data.json', JSON.stringify({ hashes: messageHashes }), (err) => {
if (err) throw err;
});
}, 30000);