-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
390 lines (368 loc) · 15.9 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const { Client, Intents, Permissions, MessageActionRow, MessageButton, MessageEmbed } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const mongoose = require('mongoose');
const Config = require('./models/Config');
require('dotenv').config();
const token = process.env.DISCORD_TOKEN;
const clientId = process.env.CLIENT_ID;
const mongoUri = process.env.MONGODB_URI;
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS]
});
mongoose.connect(mongoUri).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('Failed to connect to MongoDB', err);
});
const userMessageCount = new Map();
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const guilds = await client.guilds.fetch();
guilds.forEach(async (guildData) => {
const guild = await client.guilds.fetch(guildData.id);
await guild.roles.fetch(); // Ensure roles are cached
let muteRole = guild.roles.cache.find(role => role.name === "Muted");
if (!muteRole) {
muteRole = await guild.roles.create({
name: "Muted",
color: "#000000",
permissions: []
});
guild.channels.cache.forEach(channel => {
channel.permissionOverwrites.create(muteRole, {
SEND_MESSAGES: false,
ADD_REACTIONS: false,
SPEAK: false
});
});
}
});
});
client.on('messageCreate', async message => {
if (message.author.bot || !message.guild) return;
const config = await Config.findOne({ guildId: message.guild.id });
if (!config) return;
// Link Detection
if (config.antiLinkEnabled) {
const member = await message.guild.members.fetch(message.author.id);
if (!(config.allowedRoleId && member.roles.cache.has(config.allowedRoleId))) {
if (message.content.includes('http://') || message.content.includes('https://')) {
await message.delete();
await message.channel.send(`${message.author}, you are not allowed to send links.`);
return;
}
}
}
// Bad Word Detection
if (config.badWordsEnabled) {
const lowerContent = message.content.toLowerCase();
const foundBadWord = config.badWords.some(word => lowerContent.includes(word));
if (foundBadWord) {
await message.delete();
await message.channel.send(`${message.author}, you used a prohibited word.`);
return;
}
}
// Spam Detection
if (config.spamProtectionEnabled) {
const userId = message.author.id;
const currentTime = Date.now();
if (!userMessageCount.has(userId)) {
userMessageCount.set(userId, []);
}
const timestamps = userMessageCount.get(userId);
timestamps.push(currentTime);
const timeWindow = 60000; // 1 minute
const threshold = config.spamThreshold;
const recentMessages = timestamps.filter(timestamp => currentTime - timestamp < timeWindow);
userMessageCount.set(userId, recentMessages);
if (recentMessages.length > threshold) {
const muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
await message.member.roles.add(muteRole);
await message.channel.send(`${message.author} has been muted for spamming.`);
setTimeout(async () => {
await message.member.roles.remove(muteRole);
await message.channel.send(`${message.author} has been unmuted.`);
}, config.muteDuration * 60000);
}
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand() && !interaction.isButton()) return;
if (interaction.isCommand()) {
const { commandName, options } = interaction;
if (commandName === 'setrole') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
const role = options.getRole('role');
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ allowedRoleId: role.id },
{ upsert: true, new: true }
);
await interaction.reply(`Allowed role set to ${role.name}`);
} else if (commandName === 'enable_antilink') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ antiLinkEnabled: true },
{ upsert: true, new: true }
);
await interaction.reply('Anti-link system enabled.');
} else if (commandName === 'disable_antilink') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ antiLinkEnabled: false },
{ upsert: true, new: true }
);
await interaction.reply('Anti-link system disabled.');
} else if (commandName === 'enable_badwords') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ badWordsEnabled: true },
{ upsert: true, new: true }
);
await interaction.reply('Bad words filter enabled.');
} else if (commandName === 'disable_badwords') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ badWordsEnabled: false },
{ upsert: true, new: true }
);
await interaction.reply('Bad words filter disabled.');
} else if (commandName === 'add_badword') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
const badWord = options.getString('word');
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ $addToSet: { badWords: badWord } },
{ upsert: true, new: true }
);
await interaction.reply(`Added "${badWord}" to the list of bad words.`);
} else if (commandName === 'remove_badword') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
const badWord = options.getString('word');
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ $pull: { badWords: badWord } },
{ upsert: true, new: true }
);
await interaction.reply(`Removed "${badWord}" from the list of bad words.`);
} else if (commandName === 'enable_spam') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ spamProtectionEnabled: true },
{ upsert: true, new: true }
);
await interaction.reply('Spam protection enabled.');
} else if (commandName === 'disable_spam') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ spamProtectionEnabled: false },
{ upsert: true, new: true }
);
await interaction.reply('Spam protection disabled.');
} else if (commandName === 'set_spam_threshold') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
const threshold = options.getInteger('threshold');
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ spamThreshold: threshold },
{ upsert: true, new: true }
);
await interaction.reply(`Spam threshold set to ${threshold} messages per minute.`);
} else if (commandName === 'set_mute_duration') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
return interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
}
const duration = options.getInteger('duration');
await Config.findOneAndUpdate(
{ guildId: interaction.guild.id },
{ muteDuration: duration },
{ upsert: true, new: true }
);
await interaction.reply(`Mute duration set to ${duration} minutes.`);
} else if (commandName === 'ping') {
await interaction.reply('Pong!');
} else if (commandName === 'invite') {
await interaction.reply(`Invite me using this link: [INVITE ME](https://discord.com/oauth2/authorize?client_id=1109396083299328090&permissions=1101927558160&scope=applications.commands+bot)`);
} else if (commandName === 'help') {
const embed = new MessageEmbed()
.setTitle('Help')
.setDescription('List of available commands:')
.addField('/setrole [role]', 'Set the role that can send links')
.addField('/enable_antilink', 'Enable the anti-link system')
.addField('/disable_antilink', 'Disable the anti-link system')
.addField('/enable_badwords', 'Enable the bad words filter')
.addField('/disable_badwords', 'Disable the bad words filter')
.addField('/add_badword [word]', 'Add a word to the bad words list')
.addField('/remove_badword [word]', 'Remove a word from the bad words list')
.addField('/enable_spam', 'Enable the spam protection')
.addField('/disable_spam', 'Disable the spam protection')
.addField('/set_spam_threshold [number]', 'Set the spam threshold')
.addField('/set_mute_duration [number]', 'Set the mute duration')
.addField('/ping', 'Replies with Pong!')
.addField('/invite', 'Generates an invite link for the bot')
.addField('/help', 'Shows this help message');
await interaction.reply({ embeds: [embed] });
}
} else if (interaction.isButton()) {
const memberId = interaction.message.embeds[0].footer.text;
const member = await interaction.guild.members.fetch(memberId).catch(() => null);
if (!member) {
return interaction.reply({ content: `Member not found.`, ephemeral: true });
}
if (interaction.customId === 'mute_button') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_ROLES)) {
return interaction.reply({ content: 'You do not have permission to use this button.', ephemeral: true });
}
const muteRole = interaction.guild.roles.cache.find(role => role.name === "Muted");
await member.roles.add(muteRole);
await interaction.reply(`${member.user.tag} has been muted.`);
} else if (interaction.customId === 'unmute_button') {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_ROLES)) {
return interaction.reply({ content: 'You do not have permission to use this button.', ephemeral: true });
}
const muteRole = interaction.guild.roles.cache.find(role => role.name === "Muted");
await member.roles.remove(muteRole);
await interaction.reply(`${member.user.tag} has been unmuted.`);
}
}
});
client.login(token);
const commands = [
{
name: 'setrole',
description: 'Set the role that can send links',
options: [
{
name: 'role',
type: 8, // Corrected type for ROLE
description: 'The role to set',
required: true,
},
],
},
{
name: 'enable_antilink',
description: 'Enable the anti-link system',
},
{
name: 'disable_antilink',
description: 'Disable the anti-link system',
},
{
name: 'enable_badwords',
description: 'Enable the bad words filter',
},
{
name: 'disable_badwords',
description: 'Disable the bad words filter',
},
{
name: 'add_badword',
description: 'Add a word to the bad words list',
options: [
{
name: 'word',
type: 3, // STRING
description: 'The word to add',
required: true,
},
],
},
{
name: 'remove_badword',
description: 'Remove a word from the bad words list',
options: [
{
name: 'word',
type: 3, // STRING
description: 'The word to remove',
required: true,
},
],
},
{
name: 'enable_spam',
description: 'Enable the spam protection',
},
{
name: 'disable_spam',
description: 'Disable the spam protection',
},
{
name: 'set_spam_threshold',
description: 'Set the spam threshold',
options: [
{
name: 'threshold',
type: 4, // INTEGER
description: 'Number of messages per minute',
required: true,
},
],
},
{
name: 'set_mute_duration',
description: 'Set the mute duration',
options: [
{
name: 'duration',
type: 4, // INTEGER
description: 'Duration in minutes',
required: true,
},
],
},
{
name: 'ping',
description: 'Replies with Pong!'
},
{
name: 'invite',
description: 'Generates an invite link for the bot'
},
{
name: 'help',
description: 'Shows the help message'
},
];
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands globally.');
await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands globally.');
} catch (error) {
console.error(error);
}
})();