Skip to content

Commit

Permalink
feat: message detector
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Sep 25, 2021
1 parent 2896496 commit 972f111
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 5 deletions.
15 changes: 15 additions & 0 deletions detector/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = async (client, guildId, userId) => {
// Update global count
const { count } = client.db.prepare('SELECT count FROM global_data').get();
client.db.prepare('UPDATE global_data SET count = ?').run(count + 1);

// Update user count
const userData = client.db.prepare('SELECT count FROM users WHERE id = ?').get(userId);
const userStatement = userData ? 'UPDATE users SET count = @count WHERE id = @id' : 'INSERT INTO users (id, count) VALUES (@id, @count)';
client.db.prepare(userStatement).run({ id: userId, count: (userData?.count ?? 1) + 1 });

// Update guild count
const guildData = client.db.prepare('SELECT count FROM guilds WHERE id = ?').get(guildId);
const guildStatement = guildData ? 'UPDATE guilds SET count = @count WHERE id = @id' : 'INSERT INTO guilds (id, count) VALUES (@id, @count)';
client.db.prepare(guildStatement).run({ id: guildId, count: (guildData?.count ?? 1) + 1 });
};
1 change: 1 addition & 0 deletions detector/detection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["g", "", "", "𝔤", "𝖌", "𝐠", "𝘨", "𝙜", "𝚐", "𝕘", "𝗀", "𝗴", "ɡ", "𝘨", "", "𝗚", "", "ɓ", "", "", "", "", "𝓰", "𝐠", "", "", "𝙶", "🄶", "𝙂", "𝒢", "🇬", "", "", "𝖦", "", "ʛ", "𝘎", "", "", "🅶", "", "𝓖", "🅖", "𝔾", "𝔊", "", "𝕲", "𝑔", "ģ", "𝐆", "ƍ", "𝐺", "𝑮", "Ġ", "𝒈", "", "ԍ", "ɢ", "ǵ", "", "ց", "𝚐", "", "ƃ", "", "", "", "Ɠ", "ɢ", "", "", "", "", "", "Ĝ", "ǧ", "Ğ", "Ǥ", "", "", "𝞋", "𝟅", "", "", ""]
38 changes: 38 additions & 0 deletions detector/detector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = async (client, message, database) => {
const levelNames = { 0: 'low', 1: 'medium', 2: 'high' };
const detect = require(`./levels/${levelNames[database?.level ?? 1]}.js`);

if (!detect(message.content)) {
return false;
}

if (message.deletable) {
message.delete();
}

if (message.channel.permissionsFor(client.user.id).has('SEND_MESSAGES')) {
message.channel.send(`${message.author}, don't use the bad letter!`).then(m => setTimeout(() => m.delete(), 4000));
}

require('./counter.js')(client, message.guildId, message.author.id);

const logs = client.db.prepare('SELECT logs FROM guilds WHERE id = ?').get(message.guildId)?.logs;
const channel = message.guild.channels.cache.get(logs);

channel?.send({
embeds: [{
title: 'G Removal',
url: 'https://h-projects.github.io/app/fuck-g/',
color: client.config.color,
fields: [
{ name: 'Type', value: 'Messaqe' },
{ name: 'User', value: `${message.author} (${message.author.id})` },
{ name: 'Channel', value: `${message.channel} (${message.channelId})` },
{ name: 'Content', value: message.content }
],
thumbnail: {
url: message.author.displayAvatarURL({ dynamic: true })
}
}]
});
};
1 change: 0 additions & 1 deletion detector/high.js

This file was deleted.

4 changes: 4 additions & 0 deletions detector/levels/high.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = content => {
const badLetters = require('../detection.json').join('');
return RegExp(`[${badLetters}]`, 'iu').test(content);
};
4 changes: 4 additions & 0 deletions detector/levels/low.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = content => {
const badLetters = require('../detection.json').join('');
return !RegExp(`[^\\s${badLetters}]`, 'iu').test(content);
};
5 changes: 5 additions & 0 deletions detector/levels/medium.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = content => {
const badLetters = require('../detection.json').join('');
const lowDetection = require('./low.js');
return RegExp(`(\\s[${badLetters}]+\\s)|(^[${badLetters}]+\\s)|(\\s[${badLetters}]+$)`, 'iu').test(content) || lowDetection(content);
};
1 change: 0 additions & 1 deletion detector/low.js

This file was deleted.

1 change: 0 additions & 1 deletion detector/medium.js

This file was deleted.

5 changes: 3 additions & 2 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ module.exports = {
async execute(message, client) {
if (message.author.bot || message.author.system || message.type !== 'DEFAULT' || !message.content) return;

const database = client.db.prepare('SELECT prefix FROM guilds WHERE id = ?').get(message.guildId);
const database = client.db.prepare('SELECT * FROM guilds WHERE id = ?').get(message.guildId);
message.prefix = database?.prefix ?? client.prefix;

const array = message.content.replace(message.prefix, '').split(' ');
const args = array.slice(1);
const [ command ] = array;

const badLetterDetected = await require('../detector/detector.js')(client, message, database);

if (!client.commands.has(command) || !message.content.startsWith(message.prefix)) return;
if (badLetterDetected || !client.commands.has(command) || !message.content.startsWith(message.prefix)) return;

if (!message.member.permissions.has(command.permissions ?? 0)) {
return message.channel.send(`You need the \`${command.permissions}\` permission to use this command`);
Expand Down

0 comments on commit 972f111

Please sign in to comment.