Skip to content

Commit

Permalink
2.4.6 - Rate limits
Browse files Browse the repository at this point in the history
* added SetRatelimit Command
* added Toggle.Ratelimiting
* ratelimiting has been added this means that if enabled over a 10
second period if a user sends more than the Guild's Ratelimit their
messages will automatically be deleted and a DM will be sent telling
them that they are being rate limited. if a muted role is set up and
muteRepeatOffenders is enabled it S.A.I.L will now automatically mute
the user if they oversend by 3 messages and if an admin channel is set
up will send a message there else it will post the message in the
channel they abused the rate limit in. (This is not a slowmode and is
global across the whole guild.)
  • Loading branch information
Vaerys-Dawn committed Feb 21, 2017
1 parent f6e4461 commit 502fa62
Show file tree
Hide file tree
Showing 19 changed files with 785 additions and 285 deletions.
716 changes: 484 additions & 232 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/main/java/Commands/Admin/ChannelHere.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import Commands.Command;
import Commands.CommandObject;
import Main.Globals;
import Main.Utility;
import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.util.EmbedBuilder;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;

Expand Down
88 changes: 88 additions & 0 deletions src/main/java/Commands/Admin/MaxMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package Commands.Admin;

import Commands.Command;
import Commands.CommandObject;
import sx.blah.discord.handle.obj.Permissions;

/**
* Created by Vaerys on 21/02/2017.
*/
public class MaxMessages implements Command {

@Override
public String execute(String args, CommandObject command) {
try {
int max = Integer.parseInt(args);
if (max <= 0){
return "> Rate Limit must be larger than 0";
}else if (max > 10){
return "> That would be stopped by Discord's Rate Limit.";
}else{
command.guildConfig.setRateLimit(max);
return "> Guild Rate limit set to **" + max + "** messages per user every 10 seconds.";
}
}catch (NumberFormatException e){
return "> You need to specify a number.";
}
}

@Override
public String[] names() {
return new String[]{"SetRateLimit"};
}

@Override
public String description() {
return "Sets the rate limit for your Guild. (Maximum Messages per 10 seconds per person.)";
}

@Override
public String usage() {
return "[Max messages per 10 sec]";
}

@Override
public String type() {
return TYPE_ADMIN;
}

@Override
public String channel() {
return null;
}

@Override
public Permissions[] perms() {
return new Permissions[]{Permissions.MANAGE_SERVER};
}

@Override
public boolean requiresArgs() {
return true;
}

@Override
public boolean doAdminLogging() {
return true;
}

@Override
public String dualDescription() {
return null;
}

@Override
public String dualUsage() {
return null;
}

@Override
public String dualType() {
return null;
}

@Override
public Permissions[] dualPerms() {
return new Permissions[0];
}
}
4 changes: 0 additions & 4 deletions src/main/java/Commands/Admin/Toggle.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package Commands.Admin;

import Annotations.ToggleAnnotation;
import Commands.Command;
import Commands.CommandObject;
import GuildToggles.GuildToggle;
import Main.Utility;
import POGOs.GuildConfig;
import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.util.EmbedBuilder;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Collectors;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/Commands/CommandInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import Commands.General.Hello;
import Commands.General.RemindMe;
import Commands.General.Test;
import Commands.Help.*;
import Commands.Help.GetGuildInfo;
import Commands.Help.*;
import Commands.RoleSelect.CosmeticRoles;
import Commands.RoleSelect.ListModifs;
import Commands.RoleSelect.ListRoles;
Expand All @@ -43,6 +43,7 @@ public static ArrayList<Command> get() {

//Admin commands
commands.add(new ChannelHere());
commands.add(new MaxMessages());
commands.add(new FinalTally());
commands.add(new GetCompEntries());
commands.add(new SetAdminRole());
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/Commands/CommandObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CommandObject {
public List<IRole> authorRoles;
public String notAllowed;

public GuildContentObject guildContent;
public GuildConfig guildConfig;
public CustomCommands customCommands;
public Characters characters;
Expand Down Expand Up @@ -78,12 +79,12 @@ private void init() {
authorColour = Utility.getUsersColour(author, guild);
authorRoles = author.getRolesForGuild(guild);

GuildContentObject guildFiles = Globals.getGuildContent(guildID);
guildConfig = guildFiles.getGuildConfig();
customCommands = guildFiles.getCustomCommands();
characters = guildFiles.getCharacters();
servers = guildFiles.getServers();
competition = guildFiles.getCompetition();
guildContent = Globals.getGuildContent(guildID);
guildConfig = guildContent.getGuildConfig();
customCommands = guildContent.getCustomCommands();
characters = guildContent.getCharacters();
servers = guildContent.getServers();
competition = guildContent.getCompetition();
client = Globals.getClient();

commands = Globals.getCommands();
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/Commands/DMCommands/GetGuildList.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package Commands.DMCommands;

import Commands.Command;
import Commands.CommandObject;
import Commands.DMCommand;
import Commands.DMCommandObject;
import Main.Utility;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.util.EmbedBuilder;

import java.util.ArrayList;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/GuildToggles/ToggleInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static ArrayList<GuildToggle> get(){
guildToggles.add(new MuteRepeatOffender());
guildToggles.add(new ShitpostFiltering());
guildToggles.add(new Voting());
guildToggles.add(new RateLimiting());

//modules
guildToggles.add(new ModuleChars());
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/GuildToggles/Toggles/RateLimiting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package GuildToggles.Toggles;

import Commands.CommandObject;
import GuildToggles.GuildToggle;
import POGOs.GuildConfig;

/**
* Created by Vaerys on 21/02/2017.
*/
public class RateLimiting implements GuildToggle{

@Override
public String name() {
return "RateLimiting";
}

@Override
public boolean toggle(GuildConfig config) {
return config.rateLimiting = !config.rateLimiting;
}

@Override
public boolean get(GuildConfig config) {
return config.rateLimiting;
}

@Override
public void execute(CommandObject command) {

}

@Override
public boolean isModule() {
return false;
}
}
47 changes: 43 additions & 4 deletions src/main/java/Handlers/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.handle.obj.*;

import java.util.ArrayList;

Expand Down Expand Up @@ -42,6 +39,9 @@ public MessageHandler(String command, String args, CommandObject commandObject)

checkBlacklist(commandObject);
checkMentionCount(commandObject);
if (rateLimiting(commandObject)) {
return;
}

if (commandObject.message.getAuthor().isBot()) {
return;
Expand Down Expand Up @@ -159,6 +159,45 @@ private void checkMentionCount(CommandObject command) {
}
}

private boolean rateLimiting(CommandObject command) {
if (Utility.testForPerms(new Permissions[]{Permissions.MANAGE_MESSAGES}, command.author, command.guild, false) ||
Utility.canBypass(command.author, command.guild, false)) {
return false;
}
if (command.guildConfig.rateLimiting) {
if (command.guildContent.rateLimit(command.authorID)) {
command.message.delete();
Utility.sendDM("Your message was deleted because you are being rate limited.\nMax messages per 10 seconds : " + command.guildConfig.MessageLimit, command.authorID);
if (command.guildConfig.muteRepeatOffenders) {
int rate = command.guildContent.getUserRate(command.authorID);
if (rate - 3 > command.guildConfig.MessageLimit) {
//mutes users if they abuse it.
boolean failed = Utility.roleManagement(command.author, command.guild, command.guildConfig.getMutedRole().getRoleID(), true).get();
if (!failed) {
IChannel adminChannel = command.client.getChannelByID(command.guildConfig.getChannelTypeID(Command.CHANNEL_ADMIN));
if (adminChannel == null){
adminChannel = command.channel;
}
Utility.sendDM("You have been muted for abusing the Guild rate limit.", command.authorID);
Utility.sendMessage("> " + command.author.mention() + " has been muted for repetitively abusing Guild rateLimit.", adminChannel);
}
}
}
if (command.guildConfig.deleteLogging) {
if (command.guildConfig.getChannelTypeID(Command.CHANNEL_SERVER_LOG) != null) {
IChannel logging = command.client.getChannelByID(command.guildConfig.getChannelTypeID(Command.CHANNEL_SERVER_LOG));
Utility.sendMessage("> **@" + command.authorUserName + "** is being rate limited", logging);
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}

//File handlers

//BlackListed Phrase Remover
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Listeners/AnnotationListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.api.internal.json.event.MessageDeleteEventResponse;
import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.handle.impl.events.guild.GuildCreateEvent;
import sx.blah.discord.handle.impl.events.guild.GuildLeaveEvent;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/Main/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IUser;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/Main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ private static void consoleInput() {
Scanner scanner = new Scanner(System.in);
while (!Globals.isReady) ;
logger.info("Console input initiated.");

while (scanner.hasNextLine()) {
while (Globals.consoleMessageCID == null) ;
IChannel channel = Globals.getClient().getChannelByID(Globals.consoleMessageCID);
String message = scanner.nextLine();
message = message.replaceAll("#Dawn#", Globals.getClient().getUserByID("153159020528533505").toString());
message = message.replaceAll("teh", "the");
message = message.replaceAll("Teh", "The");
message = message.replace("#Dawn#", Globals.getClient().getUserByID("153159020528533505").getName());
message = message.replace("teh", "the");
message = message.replace("Teh", "The");
System.out.println(message);
if (!message.equals("")) {
Utility.sendMessage(message, channel);
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/Main/TimedEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import POGOs.GuildConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.Image;

import java.io.File;
import java.time.*;
import java.time.DayOfWeek;
import java.time.Month;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -32,6 +34,7 @@ public TimedEvents() {
ZonedDateTime nowUTC = ZonedDateTime.now(ZoneOffset.UTC);
doEventSec();
doEventThreeSec();
doEventTenSec();
doEventMin(nowUTC);
doEventFiveMin(nowUTC);
doEventDaily(nowUTC);
Expand Down Expand Up @@ -198,6 +201,18 @@ public void run() {
}, 3 * 1000, 3000);
}

private void doEventTenSec() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
for (TimedObject task : TimerObjects) {
Globals.getGuildContent(task.getGuildID()).resetRateLimit();
}
}
}, 1000, 10 * 1000);
}

private static void doEventMin(ZonedDateTime nowUTC) {
ZonedDateTime nextTimeUTC;
nextTimeUTC = nowUTC.withSecond(0).withMinute(nowUTC.getMinute() + 1);
Expand Down
Loading

0 comments on commit 502fa62

Please sign in to comment.