From 977a105778e4a9ae086f0a627daad101454ca8a3 Mon Sep 17 00:00:00 2001 From: Anix Date: Sun, 10 Oct 2021 16:43:09 +0200 Subject: [PATCH] spam protection added --- Protocol.txt | 5 ++++- src/Util/NetworkUtil.js | 6 ++++-- src/Util/Util.js | 36 ++++++++++++++++++++++++++++++++++++ src/commands/Ping.js | 3 ++- src/commands/WhatsMyIp.js | 8 ++++---- src/events/messageCreate.js | 8 ++++++++ src/index.js | 13 ++++++++----- 7 files changed, 66 insertions(+), 13 deletions(-) diff --git a/Protocol.txt b/Protocol.txt index d7ce86e..e848c93 100644 --- a/Protocol.txt +++ b/Protocol.txt @@ -3,4 +3,7 @@ npm i discord.js npm i enmap curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - -sudo apt-get install -y nodejs \ No newline at end of file +sudo apt-get install -y nodejs + +crontab -e +@reboot npm start --prefix location/GlatzBot \ No newline at end of file diff --git a/src/Util/NetworkUtil.js b/src/Util/NetworkUtil.js index aaef42f..817506e 100644 --- a/src/Util/NetworkUtil.js +++ b/src/Util/NetworkUtil.js @@ -1,4 +1,5 @@ -const { networkInterfaces } = require('os'); +const { networkInterfaces } = require("os"); +const Util = require("./Util"); class NetworkUtil { static getLocalIP() { @@ -8,7 +9,7 @@ class NetworkUtil { for (const name of Object.keys(nets)) { for (const net of nets[name]) { // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses - if (net.family === 'IPv4' && !net.internal) { + if (net.family === "IPv4" && !net.internal) { if (!results[name]) { results[name] = []; } @@ -16,6 +17,7 @@ class NetworkUtil { } } } + if (Util.isEmpty(results)) return undefined; return results; } } diff --git a/src/Util/Util.js b/src/Util/Util.js index 465aa3b..68434e1 100644 --- a/src/Util/Util.js +++ b/src/Util/Util.js @@ -1,4 +1,40 @@ +const Discord = require("discord.js"); + class Util { + constructor(client) { + this.sleepList = new(require("enmap")); + } + + addSleepUser(user) { + this.sleepList.set(user, Date.now()); + } + + isUserInList(userTag) { + const removeSleepList = []; + let flag = false; + this.sleepList.forEach((time, user) => { + if (Date.now() - time > (1000 * 6)) { + removeSleepList.push(user); + return; + } + + if (userTag == user) flag = true; + }); + + for (let i in removeSleepList) { + this.sleepList.delete(removeSleepList[i]); + } + + return flag; + } + + static getEmbed(client) { + return new Discord.MessageEmbed() + .setColor(0xAA55E0) + .setTimestamp() + .setFooter("Glatz regiert!", client.user.avatarURL()); + } + static isEmpty(obj) { return Object.keys(obj).length === 0; } diff --git a/src/commands/Ping.js b/src/commands/Ping.js index e2363cf..5d13844 100644 --- a/src/commands/Ping.js +++ b/src/commands/Ping.js @@ -1,5 +1,6 @@ const Base = require("./Command.js"); const Discord = require('discord.js'); +const Util = require("../Util/Util.js"); class Ping extends Base { constructor() { @@ -9,7 +10,7 @@ class Ping extends Base { async run(message, args) { if (!message.guild) return; - const embed = new Discord.MessageEmbed().setColor(0xAA55E0).setTimestamp().setFooter("GLATZ regiert!", this.client.user.avatarURL()); + const embed = Util.getEmbed(this.client); var msg = await message.channel.send("Pinging..."); embed.setTitle("🏓 Pong!") diff --git a/src/commands/WhatsMyIp.js b/src/commands/WhatsMyIp.js index e54191a..a4b1109 100644 --- a/src/commands/WhatsMyIp.js +++ b/src/commands/WhatsMyIp.js @@ -1,7 +1,7 @@ -const Discord = require('discord.js'); -const Util = require("../Util/Util"); +const Discord = require("discord.js"); const NetworkUtil = require("../Util/NetworkUtil.js"); const Base = require("./Command.js"); +const Util = require("../Util/Util.js"); class WhatsMyIp extends Base { constructor() { @@ -10,13 +10,13 @@ class WhatsMyIp extends Base { async run(message, args) { const ips = NetworkUtil.getLocalIP(); - if (Util.isEmpty(ips)) return; + if (ips == undefined) return; var ipsFormatted = ""; for (var i in Object.keys(ips)) { ipsFormatted += Object.keys(ips)[i] + " : " + Object.values(ips)[i] + "\n"; } - const embed = new Discord.MessageEmbed().setColor(0xAA55E0).setTimestamp().setFooter("GLATZ regiert!", this.client.user.avatarURL()); + const embed = Util.getEmbed(this.client); embed.setTitle("👨‍💻IPS") .setDescription(ipsFormatted); diff --git a/src/events/messageCreate.js b/src/events/messageCreate.js index da02fd8..3f86edb 100644 --- a/src/events/messageCreate.js +++ b/src/events/messageCreate.js @@ -13,6 +13,14 @@ module.exports = (client, message) => { if (!cmd) return; + if (client.util.isUserInList(message.author.tag)) { + message.channel.send(`${message.author.toString()} Please don't spam!`); + return; + } + + client.util.addSleepUser(message.author.tag); + + cmd.setClient(client) cmd.run(message, args, CMD_NAME); }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index cf2cd55..bf8a2a2 100644 --- a/src/index.js +++ b/src/index.js @@ -4,12 +4,15 @@ const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MEMBERS", "GUILD_ const config = require("./config.json") const EventManager = require("./events/EventManager.js"); -client.commands = new(require("enmap")); +async function init() { + client.commands = new(require("enmap")); + const eventManager = new EventManager(client); + const commandManager = new CommandManager(client); + await client.login(config.token); + client.util = new(require("./Util/Util"))(client); +} -const eventManager = new EventManager(client); -const commandManager = new CommandManager(client); - -client.login(config.token) +init(); process.on("unhandledRejection", (error) => { console.error("Unhandled promise rejection:", error);