From 818ddbf884694c060e643f576966986c17c69a56 Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 28 Mar 2018 21:20:31 -0400 Subject: [PATCH] Separated commands list from MessageReceivedEvent Created a separate class for managing commands --- .../netneutrality/command/CommandManager.java | 67 +++++++++++++ .../handler/NewCommandListener.java | 98 +++---------------- 2 files changed, 78 insertions(+), 87 deletions(-) create mode 100644 src/main/java/netneutrality/command/CommandManager.java diff --git a/src/main/java/netneutrality/command/CommandManager.java b/src/main/java/netneutrality/command/CommandManager.java new file mode 100644 index 0000000..58aea94 --- /dev/null +++ b/src/main/java/netneutrality/command/CommandManager.java @@ -0,0 +1,67 @@ +package netneutrality.command; + +/* + * Copyright (c) anthorob + * + * https://xtremcommunity.com/ + * https://discord.gg/CdFcyQ + */ + +import netneutrality.MusicBot; +import org.reflections.Reflections; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Set; + +public class CommandManager { + + public static HashMap commands = new HashMap<>(); + public static HashMap commandsAlias = new HashMap<>(); + + public static void init() { + initCommands(); + initAlias(); + } + + private static void initCommands() { + MusicBot.LOGGER.info("Loading commands.."); + + Reflections reflections = new Reflections("netneutrality.command"); + Set> classes = reflections.getSubTypesOf(AbstractCommand.class); + + for (Class s : classes) { + try { + if (Modifier.isAbstract(s.getModifiers())) { + continue; + } + + AbstractCommand command = s.getConstructor().newInstance(); + + if (!command.isEnabled()) + continue; + + if (!commands.containsKey(command.getCommand())) { + commands.put(command.getCommand(), command); + } + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + } + + MusicBot.LOGGER.info("Loaded " + commands.size() + " commands!"); + } + + private static void initAlias() { + + for (AbstractCommand command : commands.values()) { + for (String alias : command.getAlias()) { + if (!commandsAlias.containsKey(alias)) + commandsAlias.put(alias, command); + } + } + } + +} diff --git a/src/main/java/netneutrality/handler/NewCommandListener.java b/src/main/java/netneutrality/handler/NewCommandListener.java index 0539685..2e69b32 100644 --- a/src/main/java/netneutrality/handler/NewCommandListener.java +++ b/src/main/java/netneutrality/handler/NewCommandListener.java @@ -1,37 +1,25 @@ package netneutrality.handler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Modifier; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Set; - import net.dv8tion.jda.core.entities.ChannelType; -import netneutrality.MusicBot; -import netneutrality.command.Visibility; -import netneutrality.command.basic.HelpCommand; -import org.reflections.Reflections; - import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.hooks.ListenerAdapter; import netneutrality.command.AbstractCommand; +import netneutrality.command.CommandManager; +import netneutrality.command.Visibility; +import netneutrality.command.basic.HelpCommand; import netneutrality.db.DBController; -import netneutrality.db.controllers.DBBlacklist; import netneutrality.db.controllers.DBMessage; -import netneutrality.db.models.MBlacklist; +import netneutrality.utils.MessageFunction; import netneutrality.utils.Settings; import netneutrality.utils.UserFunction; +import java.time.format.DateTimeFormatter; + public class NewCommandListener extends ListenerAdapter { - - private HashMap commands = new HashMap<>(); - private HashMap commandsAlias = new HashMap<>(); public NewCommandListener() { - initCommands(); - initAlias(); + CommandManager.init(); } @Override @@ -41,7 +29,6 @@ public class NewCommandListener extends ListenerAdapter { if (event.isWebhookMessage()) return; if (MessageRateLimit.check(event.getMember(), event.getMessage(), event.getChannel())) return; - if (event.isFromType(ChannelType.TEXT)) { ((DBMessage) DBController.get("message")).log(event.getGuild().getName(), event.getTextChannel().getName(), event.getMember().getUser().getName(), @@ -49,7 +36,6 @@ public class NewCommandListener extends ListenerAdapter { event.getMessage().getContentDisplay().trim()); } - Message message = event.getMessage(); String msg = message.getContentDisplay().trim(); @@ -57,11 +43,9 @@ public class NewCommandListener extends ListenerAdapter { msg = msg.substring(1); String cmd = msg.split(" ", 2)[0]; + if (CommandManager.commands.containsKey(cmd) || CommandManager.commandsAlias.containsKey(cmd)) { - if (commands.containsKey(cmd) || commandsAlias.containsKey(cmd)) { - - AbstractCommand c = commands.containsKey(cmd) ? commands.get(cmd) : commandsAlias.get(cmd); - + AbstractCommand c = CommandManager.commands.containsKey(cmd) ? CommandManager.commands.get(cmd) : CommandManager.commandsAlias.get(cmd); if (Visibility.check(event.getChannel(), c.getVisibility())) { if (c.needAdmin() && c.getVisibility() != Visibility.PRIVATE) { @@ -71,74 +55,14 @@ public class NewCommandListener extends ListenerAdapter { } if (cmd.equals("help")) { - ((HelpCommand) commands.get("help")).sendHelp(commands, commandsAlias, event.getAuthor(), event.getChannel()); + ((HelpCommand) CommandManager.commands.get("help")).sendHelp(CommandManager.commands, CommandManager.commandsAlias, event.getAuthor(), event.getChannel()); } else { c.execute(event, msg.replaceFirst("(" + cmd + ")( )?", "")); } } } } else { - - ArrayList blacklist = new ArrayList<>(); - - try { - blacklist = ((DBBlacklist) DBController.get("blacklist")).getBlacklistFromServer(event.getGuild().getId()); - } catch (RuntimeException ignored) { } - - if (!blacklist.isEmpty()) { - - boolean found = false; - for (MBlacklist aBlacklist : blacklist) { - if (msg.toLowerCase().contains(aBlacklist.getWord())) - found = true; - } - - if (found) { - event.getMessage().delete().queue(); - event.getTextChannel().sendMessage("Not in my christian server").queue(); - } - } + MessageFunction.checkBlacklist(event.getGuild().getId(), event.getMessage(), event.getTextChannel(), msg); } } - - private void initCommands() { - MusicBot.LOGGER.info("Loading commands.."); - - Reflections reflections = new Reflections("netneutrality.command"); - Set> classes = reflections.getSubTypesOf(AbstractCommand.class); - - for (Class s : classes) { - try { - if (Modifier.isAbstract(s.getModifiers())) { - continue; - } - - AbstractCommand command = s.getConstructor().newInstance(); - - if (!command.isEnabled()) - continue; - - if (!commands.containsKey(command.getCommand())) { - commands.put(command.getCommand(), command); - } - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { - e.printStackTrace(); - } - } - - MusicBot.LOGGER.info("Loaded " + commands.size() + " commands!"); - } - - private void initAlias() { - - for (AbstractCommand command : commands.values()) { - for (String alias : command.getAlias()) { - if (!commandsAlias.containsKey(alias)) - commandsAlias.put(alias, command); - } - } - } - - }