Separated commands list from MessageReceivedEvent
Created a separate class for managing commands
This commit is contained in:
1 parent
7dbf7dc979
commit
818ddbf884
2 files changed
+78
-87
No files matched your search
@@ -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<String, AbstractCommand> commands = new HashMap<>();
|
||||
public static HashMap<String, AbstractCommand> 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<Class<? extends AbstractCommand>> classes = reflections.getSubTypesOf(AbstractCommand.class);
|
||||
|
||||
for (Class<? extends AbstractCommand> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, AbstractCommand> commands = new HashMap<>();
|
||||
private HashMap<String, AbstractCommand> 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<MBlacklist> 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<Class<? extends AbstractCommand>> classes = reflections.getSubTypesOf(AbstractCommand.class);
|
||||
|
||||
for (Class<? extends AbstractCommand> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user