Skip to content

Commit

Permalink
spam protection added
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorTrojan committed Oct 10, 2021
1 parent 073fbcd commit 977a105
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Protocol.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
sudo apt-get install -y nodejs

crontab -e
@reboot npm start --prefix location/GlatzBot
6 changes: 4 additions & 2 deletions src/Util/NetworkUtil.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { networkInterfaces } = require('os');
const { networkInterfaces } = require("os");
const Util = require("./Util");

class NetworkUtil {
static getLocalIP() {
Expand All @@ -8,14 +9,15 @@ 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] = [];
}
results[name].push(net.address);
}
}
}
if (Util.isEmpty(results)) return undefined;
return results;
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/Util/Util.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Ping.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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!")
Expand Down
8 changes: 4 additions & 4 deletions src/commands/WhatsMyIp.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 977a105

Please sign in to comment.