Compare commits
6 Commits
c2fcc1cee0
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| ca5aa87019 | |||
|
|
373a889f95 | ||
|
|
30bb267366 | ||
|
|
0478c6df63 | ||
| 7a485e9869 | |||
| 68194b3879 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -147,4 +147,4 @@ bin/
|
||||
build
|
||||
|
||||
*-config.yml
|
||||
modules/
|
||||
modulesre/
|
||||
|
||||
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@@ -0,0 +1 @@
|
||||
ModBot
|
||||
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.net.URLClassLoader,newInstance" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
@@ -8,7 +8,7 @@ plugins {
|
||||
mainClassName = 'tech.nevets.modbot.CoreBot'
|
||||
|
||||
group 'tech.nevets'
|
||||
version '0.1.0-beta'
|
||||
version '1.2.0'
|
||||
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
@@ -25,7 +25,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'net.dv8tion:JDA:4.4.0_350'
|
||||
implementation 'ch.qos.logback:logback-classic:1.2.11'
|
||||
implementation 'ch.qos.logback:logback-classic:1.4.0'
|
||||
implementation 'com.google.code.gson:gson:2.9.0'
|
||||
implementation 'me.carleslc.Simple-YAML:Simple-Yaml:1.7.2'
|
||||
}
|
||||
|
||||
79
src/main/java/tech/nevets/modbot/CommandManager.java
Normal file
79
src/main/java/tech/nevets/modbot/CommandManager.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package tech.nevets.modbot;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import tech.nevets.modbot.api.commands.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CommandManager {
|
||||
|
||||
|
||||
public boolean hasPermission(Member member, ICommand cmd) {
|
||||
List<Role> memberRoles = member.getRoles();
|
||||
int permLevel = 0;
|
||||
|
||||
for (Role memberRole : memberRoles) {
|
||||
switch (memberRole.getName().toLowerCase()) {
|
||||
case "helper", "1":
|
||||
if (permLevel < 1) permLevel = 1;
|
||||
continue;
|
||||
case "moderator", "mod", "2":
|
||||
if (permLevel < 2) permLevel = 2;
|
||||
continue;
|
||||
case "administrator", "admin", "3":
|
||||
if (permLevel < 3) permLevel = 3;
|
||||
continue;
|
||||
case "owner", "4":
|
||||
if (permLevel < 4) permLevel = 4;
|
||||
}
|
||||
}
|
||||
return permLevel >= cmd.getPermissionLevel();
|
||||
}
|
||||
|
||||
public void handle(GuildMessageReceivedEvent event) {
|
||||
String[] split = event.getMessage().getContentRaw()
|
||||
.replaceFirst("(?i)" + Pattern.quote(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix")), "")
|
||||
.split("\\s+");
|
||||
|
||||
String invoke = split[0].toLowerCase();
|
||||
IPrefixCommand cmd = Registry.getPrefixCommand(invoke);
|
||||
|
||||
assert event.getMember() != null;
|
||||
assert cmd != null;
|
||||
if (!hasPermission(event.getMember(), cmd)) {
|
||||
event.getMessage().reply("You do not have permission to use this command.").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
event.getChannel().sendTyping().queue();
|
||||
List<String> args = Arrays.asList(split).subList(1, split.length);
|
||||
|
||||
CommandContext ctx = new CommandContext(event, args);
|
||||
|
||||
cmd.handle(ctx);
|
||||
}
|
||||
|
||||
public void handle(SlashCommandEvent event) {
|
||||
String invoke = event.getName();
|
||||
ISlashCommand cmd = Registry.getSlashCommand(invoke);
|
||||
|
||||
if (cmd != null) {
|
||||
event.deferReply().queue();
|
||||
|
||||
assert event.getMember() != null;
|
||||
if (!hasPermission(event.getMember(), cmd)) {
|
||||
event.getHook().sendMessage("You do not have permission to use this command.").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
CommandContext ctx = new CommandContext(event);
|
||||
|
||||
cmd.handleSlash(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,20 +4,21 @@ import ch.qos.logback.classic.Level;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tech.nevets.modbot.api.BotModule;
|
||||
import tech.nevets.modbot.api.Config;
|
||||
import tech.nevets.modbot.util.commands.CoreListener;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.util.*;
|
||||
|
||||
public class CoreBot {
|
||||
public static JDA jda;
|
||||
private static JDA jda;
|
||||
public static final Config CORE_CONFIG = new Config("core");
|
||||
public static final List<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
|
||||
private static String botPrefix;
|
||||
private static List<BotModule> modules;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CoreBot.class);
|
||||
|
||||
@@ -25,12 +26,12 @@ public class CoreBot {
|
||||
setLoggingLevel(Level.INFO);
|
||||
CORE_CONFIG.addDefaults(getDefaults());
|
||||
CORE_CONFIG.loadConfig();
|
||||
botPrefix = CORE_CONFIG.getConfig().getString("bot.prefix");
|
||||
modules = ModuleLoader.loadModules();
|
||||
CoreListener coreListener = new CoreListener();
|
||||
|
||||
JDABuilder builder = JDABuilder.createDefault(CORE_CONFIG.getConfig().getString("bot.token"))
|
||||
.enableCache(CacheFlag.VOICE_STATE)
|
||||
.addEventListeners(coreListener);
|
||||
.addEventListeners(Registry.getListeners());
|
||||
|
||||
for (ListenerAdapter listener : PLUGIN_LISTENERS) {
|
||||
builder.addEventListeners(listener);
|
||||
@@ -42,7 +43,8 @@ public class CoreBot {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
coreListener.getCommandRegistry().registerSlashCommands();
|
||||
Registry.loadSlashCommands();
|
||||
|
||||
for (BotModule module : modules) {
|
||||
if (module.loadCommandRegistry() == null) {
|
||||
LOGGER.warn("Module has no commands to load");
|
||||
@@ -54,11 +56,10 @@ public class CoreBot {
|
||||
|
||||
}
|
||||
|
||||
public static void addListener(ListenerAdapter listener) {
|
||||
PLUGIN_LISTENERS.add(listener);
|
||||
public static void loadSlashCommands(List<CommandData> commandData) {
|
||||
jda.updateCommands().addCommands(commandData).queue();
|
||||
}
|
||||
|
||||
|
||||
private static Map<String, Object> getDefaults() {
|
||||
Map<String, Object> defaults = new HashMap<>();
|
||||
defaults.put("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
|
||||
@@ -67,8 +68,12 @@ public class CoreBot {
|
||||
return defaults;
|
||||
}
|
||||
|
||||
public static void setLoggingLevel(Level level) {
|
||||
private static void setLoggingLevel(Level level) {
|
||||
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
root.setLevel(level);
|
||||
}
|
||||
|
||||
public static String getPrefix() {
|
||||
return botPrefix;
|
||||
}
|
||||
}
|
||||
|
||||
1019
src/main/java/tech/nevets/modbot/EventExecutor.java
Normal file
1019
src/main/java/tech/nevets/modbot/EventExecutor.java
Normal file
File diff suppressed because it is too large
Load Diff
15
src/main/java/tech/nevets/modbot/Listener.java
Normal file
15
src/main/java/tech/nevets/modbot/Listener.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package tech.nevets.modbot;
|
||||
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import tech.nevets.modbot.api.commands.ICommand;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Listener extends ListenerAdapter {
|
||||
public final List<ICommand> commands = new ArrayList<>();
|
||||
|
||||
void addCommand(ICommand cmd) {
|
||||
commands.add(cmd);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package tech.nevets.modbot;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tech.nevets.modbot.api.BotModule;
|
||||
import tech.nevets.modbot.api.BotModuleFactory;
|
||||
import tech.nevets.modbot.api.IBotModule;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -51,20 +53,22 @@ public class ModuleLoader {
|
||||
Manifest mf;
|
||||
Attributes attributes;
|
||||
String mainClassName;
|
||||
String moduleName;
|
||||
|
||||
try {
|
||||
jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
|
||||
mf = jarStream.getManifest();
|
||||
attributes = mf.getMainAttributes();
|
||||
mainClassName = attributes.getValue("Main-Class");
|
||||
moduleName = attributes.getValue("Module-Name");
|
||||
} catch (final Exception e) {
|
||||
LOGGER.error("Error while reading module attributes from module: " + module.toString());
|
||||
LOGGER.error("Error while reading module attributes from module: " + module.getFileName());
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mainClassName == null) {
|
||||
LOGGER.error("Unable to get mainClassName from module: " + module.toString());
|
||||
LOGGER.error("Unable to get mainClassName from module: " + moduleName);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -73,7 +77,7 @@ public class ModuleLoader {
|
||||
try {
|
||||
url = new URL[]{file.toURI().toURL()};
|
||||
} catch (MalformedURLException e) {
|
||||
LOGGER.error("Error occurred while getting file URL");
|
||||
LOGGER.error("Error occurred while getting file URL from module " + moduleName);
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
@@ -83,18 +87,38 @@ public class ModuleLoader {
|
||||
try {
|
||||
mainClass = Class.forName(mainClassName, true, urlClassLoader);
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOGGER.error("Error getting class from classLoader, Class Not Found!");
|
||||
LOGGER.error("Error getting class from classLoader, Class Not Found in module " + moduleName + "!");
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
|
||||
BotModule newModule;
|
||||
try {
|
||||
newModule = (BotModule) mainClass.getDeclaredConstructor().newInstance();
|
||||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
LOGGER.error("Error while creating instance of module " + module.toString());
|
||||
e.printStackTrace();
|
||||
String moduleType = attributes.getValue("Module-Type");
|
||||
if (moduleType.equalsIgnoreCase("inherit") || moduleType.equalsIgnoreCase("i")) {
|
||||
IBotModule iNewModule;
|
||||
try {
|
||||
iNewModule = (IBotModule) mainClass.getDeclaredConstructor().newInstance();
|
||||
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
|
||||
LOGGER.error("Error while creating instance of module " + moduleName);
|
||||
//TODO: Check if setting Module-Type to inherit wile using extend breaks here
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
newModule = BotModuleFactory.fromIBotModule(iNewModule, moduleName);
|
||||
} else if (moduleType.equalsIgnoreCase("extend") || moduleType.equalsIgnoreCase("e")) {
|
||||
try {
|
||||
newModule = (BotModule) mainClass.getDeclaredConstructor(String.class).newInstance(moduleName);
|
||||
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
|
||||
LOGGER.error("Error while creating instance of module " + moduleName);
|
||||
LOGGER.error("HINT: Check the Module-Type of module " + moduleName);
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
LOGGER.error("Error loading module: Module " + moduleName + " has an invalid Module-Type!");
|
||||
continue;
|
||||
}
|
||||
|
||||
newModule.onPreEnable();
|
||||
modules.add(newModule);
|
||||
|
||||
@@ -109,7 +133,15 @@ public class ModuleLoader {
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.debug("Loaded module: " + modules.toArray().toString());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < modules.size(); i++) {
|
||||
sb.append(modules.get(i).getModuleName());
|
||||
if ((i + 1) < modules.size()) {
|
||||
sb.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.info("Loaded modules: " + sb);
|
||||
return modules;
|
||||
}
|
||||
}
|
||||
|
||||
120
src/main/java/tech/nevets/modbot/Registry.java
Normal file
120
src/main/java/tech/nevets/modbot/Registry.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package tech.nevets.modbot;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tech.nevets.modbot.api.commands.CommandContext;
|
||||
import tech.nevets.modbot.api.commands.ICommand;
|
||||
import tech.nevets.modbot.api.commands.IPrefixCommand;
|
||||
import tech.nevets.modbot.api.commands.ISlashCommand;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Registry {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Registry.class);
|
||||
|
||||
public static final List<ICommand> COMMANDS = new ArrayList<>();
|
||||
public static final List<IPrefixCommand> PREFIX_COMMANDS = new ArrayList<>();
|
||||
public static final List<ISlashCommand> SLASH_COMMANDS = new ArrayList<>();
|
||||
private static final EventExecutor EVENT_EXECUTOR = new EventExecutor();
|
||||
|
||||
@Nullable
|
||||
public static ICommand getCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (ICommand cmd : COMMANDS) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static IPrefixCommand getPrefixCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (IPrefixCommand cmd : PREFIX_COMMANDS) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
LOGGER.warn("Returned null for command search: " + searchLower);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ISlashCommand getSlashCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (ISlashCommand cmd : SLASH_COMMANDS) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
LOGGER.warn("Returned null for command search: " + searchLower);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<ICommand> getCommands() {
|
||||
return COMMANDS;
|
||||
}
|
||||
|
||||
public static List<IPrefixCommand> getPrefixCommands() {
|
||||
return PREFIX_COMMANDS;
|
||||
}
|
||||
|
||||
public static List<ISlashCommand> getSlashCommands() {
|
||||
return SLASH_COMMANDS;
|
||||
}
|
||||
|
||||
public static EventExecutor getListeners() {
|
||||
return EVENT_EXECUTOR;
|
||||
}
|
||||
|
||||
public static boolean exists(ICommand cmd) {
|
||||
boolean nameExists = COMMANDS.stream().anyMatch(it -> it.getName().equalsIgnoreCase(cmd.getName()));
|
||||
if (nameExists) {
|
||||
throw new IllegalArgumentException("A command with that name already exists!");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void register(IPrefixCommand cmd) {
|
||||
if (!exists(cmd)) {
|
||||
COMMANDS.add(cmd);
|
||||
PREFIX_COMMANDS.add(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
public static void register(ISlashCommand cmd) {
|
||||
if (!exists(cmd)) {
|
||||
COMMANDS.add(cmd);
|
||||
SLASH_COMMANDS.add(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
public static void register(ListenerAdapter listener) {
|
||||
EVENT_EXECUTOR.registerListener(listener);
|
||||
}
|
||||
|
||||
public static void loadSlashCommands() {
|
||||
if (SLASH_COMMANDS.size() > 0) {
|
||||
List<CommandData> cmdDataList = new ArrayList<>();
|
||||
for (ISlashCommand slashCmd : SLASH_COMMANDS) {
|
||||
cmdDataList.add(slashCmd.getCommandData());
|
||||
}
|
||||
CoreBot.loadSlashCommands(cmdDataList);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,13 @@
|
||||
package tech.nevets.modbot.api;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import tech.nevets.modbot.api.commands.CommandRegistry;
|
||||
public abstract class BotModule implements IBotModule {
|
||||
private String moduleName;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
public BotModule(String moduleName) {
|
||||
this.moduleName = moduleName;
|
||||
}
|
||||
|
||||
public interface BotModule {
|
||||
|
||||
default void onPreEnable() {}
|
||||
|
||||
CommandRegistry loadCommandRegistry();
|
||||
|
||||
void onEnable(JDA jda);
|
||||
|
||||
void onDisable();
|
||||
public String getModuleName() {
|
||||
return moduleName;
|
||||
}
|
||||
}
|
||||
|
||||
27
src/main/java/tech/nevets/modbot/api/BotModuleFactory.java
Normal file
27
src/main/java/tech/nevets/modbot/api/BotModuleFactory.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package tech.nevets.modbot.api;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import tech.nevets.modbot.api.commands.CommandRegistry;
|
||||
|
||||
public class BotModuleFactory {
|
||||
|
||||
public static BotModule fromIBotModule(IBotModule iBotModule, String moduleName) {
|
||||
|
||||
return new BotModule(moduleName) {
|
||||
@Override
|
||||
public CommandRegistry loadCommandRegistry() {
|
||||
return iBotModule.loadCommandRegistry();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable(JDA jda) {
|
||||
iBotModule.onEnable(jda);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
iBotModule.onDisable();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
15
src/main/java/tech/nevets/modbot/api/IBotModule.java
Normal file
15
src/main/java/tech/nevets/modbot/api/IBotModule.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package tech.nevets.modbot.api;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import tech.nevets.modbot.api.commands.CommandRegistry;
|
||||
|
||||
public interface IBotModule {
|
||||
|
||||
default void onPreEnable() {}
|
||||
|
||||
void registry();
|
||||
|
||||
void onEnable(JDA jda);
|
||||
|
||||
void onDisable();
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package tech.nevets.modbot.util.commands;
|
||||
package tech.nevets.modbot.api.commands;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import tech.nevets.modbot.api.commands.ICommandContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -8,7 +8,6 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tech.nevets.modbot.CoreBot;
|
||||
import tech.nevets.modbot.util.commands.CommandContext;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
@@ -24,33 +23,6 @@ public class CommandRegistry {
|
||||
|
||||
public CommandRegistry() {}
|
||||
|
||||
public void addCommand(ICommand cmd) {
|
||||
boolean nameFound = commands.stream().anyMatch(it -> it.getName().equalsIgnoreCase(cmd.getName()));
|
||||
if (nameFound) {
|
||||
throw new IllegalArgumentException("A command with that name is already present!");
|
||||
}
|
||||
|
||||
commands.add(cmd);
|
||||
|
||||
if (cmd instanceof IPrefixCommand) {
|
||||
prefixCommands.add((IPrefixCommand) cmd);
|
||||
}
|
||||
|
||||
if (cmd instanceof ISlashCommand) {
|
||||
slashCommands.add((ISlashCommand) cmd);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerSlashCommands() {
|
||||
if (slashCommands.size() > 0) {
|
||||
List<CommandData> cmdDataList = new ArrayList<>();
|
||||
for (ISlashCommand slashCmd : slashCommands) {
|
||||
cmdDataList.add(slashCmd.getCommandData());
|
||||
}
|
||||
CoreBot.jda.updateCommands().addCommands(cmdDataList).queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public IPrefixCommand getPrefixCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
@@ -60,6 +32,7 @@ public class CommandRegistry {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
LOGGER.warn("Returned null for command search: " + searchLower);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -109,46 +82,5 @@ public class CommandRegistry {
|
||||
return permLevel >= cmd.getPermissionLevel();
|
||||
}
|
||||
|
||||
public void handle(GuildMessageReceivedEvent event) {
|
||||
String[] split = event.getMessage().getContentRaw()
|
||||
.replaceFirst("(?i)" + Pattern.quote(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix")), "")
|
||||
.split("\\s+");
|
||||
|
||||
String invoke = split[0].toLowerCase();
|
||||
IPrefixCommand cmd = getPrefixCommand(invoke);
|
||||
|
||||
assert event.getMember() != null;
|
||||
if (!hasPermission(event.getMember(), cmd)) {
|
||||
event.getMessage().reply("You do not have permission to use this command.").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
if (cmd != null) {
|
||||
event.getChannel().sendTyping().queue();
|
||||
List<String> args = Arrays.asList(split).subList(1, split.length);
|
||||
|
||||
CommandContext ctx = new CommandContext(event, args);
|
||||
|
||||
cmd.handle(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
public void handle(SlashCommandEvent event) {
|
||||
String invoke = event.getName();
|
||||
ISlashCommand cmd = getSlashCommand(invoke);
|
||||
|
||||
if (cmd != null) {
|
||||
event.deferReply().queue();
|
||||
|
||||
assert event.getMember() != null;
|
||||
if (!hasPermission(event.getMember(), cmd)) {
|
||||
event.getHook().sendMessage("You do not have permission to use this command.").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
CommandContext ctx = new CommandContext(event);
|
||||
|
||||
cmd.handleSlash(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tech.nevets.modbot.util.commands;
|
||||
package tech.nevets.modbot.api.commands;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
@@ -1,7 +1,5 @@
|
||||
package tech.nevets.modbot.api.commands;
|
||||
|
||||
import tech.nevets.modbot.util.commands.CommandContext;
|
||||
|
||||
public interface IPrefixCommand extends ICommand {
|
||||
|
||||
void handle(CommandContext ctx);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package tech.nevets.modbot.api.commands;
|
||||
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import tech.nevets.modbot.util.commands.CommandContext;
|
||||
|
||||
public interface ISlashCommand extends ICommand {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tech.nevets.modbot.util.commands;
|
||||
package tech.nevets.modbot.internal;
|
||||
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.events.ReadyEvent;
|
||||
@@ -9,11 +9,14 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tech.nevets.modbot.CoreBot;
|
||||
import tech.nevets.modbot.api.commands.CommandRegistry;
|
||||
import tech.nevets.modbot.CommandManager;
|
||||
import tech.nevets.modbot.internal.commands.HelpCmd;
|
||||
import tech.nevets.modbot.internal.commands.TestCmd;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class CoreListener extends ListenerAdapter {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CoreListener.class);
|
||||
public class InternalListener extends ListenerAdapter {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(InternalListener.class);
|
||||
private final CommandRegistry registry = new CommandRegistry();
|
||||
|
||||
{
|
||||
@@ -40,7 +43,7 @@ public class CoreListener extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
|
||||
if (raw.startsWith(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix"))) {
|
||||
if (raw.startsWith(CoreBot.getPrefix())) {
|
||||
registry.handle(event);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package tech.nevets.modbot.util.commands;
|
||||
package tech.nevets.modbot.internal.commands;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
@@ -7,7 +7,9 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import tech.nevets.modbot.CommandManager;
|
||||
import tech.nevets.modbot.CoreBot;
|
||||
import tech.nevets.modbot.api.commands.CommandContext;
|
||||
import tech.nevets.modbot.api.commands.ICommand;
|
||||
import tech.nevets.modbot.api.commands.IPrefixCommand;
|
||||
import tech.nevets.modbot.api.commands.ISlashCommand;
|
||||
@@ -1,8 +1,9 @@
|
||||
package tech.nevets.modbot.util.commands;
|
||||
package tech.nevets.modbot.internal.commands;
|
||||
|
||||
import net.dv8tion.jda.api.entities.IMentionable;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import tech.nevets.modbot.api.commands.CommandContext;
|
||||
import tech.nevets.modbot.api.commands.ISlashCommand;
|
||||
|
||||
public class TestCmd implements ISlashCommand {
|
||||
@@ -29,6 +29,6 @@ public class Pair<L,R> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pair{" + left + ", right=" + right + "}";
|
||||
return "Pair{left=" + left + ", right=" + right + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
package tech.nevets.modbot.util.commands;
|
||||
|
||||
import tech.nevets.modbot.api.commands.CommandRegistry;
|
||||
import tech.nevets.modbot.api.commands.ICommand;
|
||||
import tech.nevets.modbot.api.commands.IPrefixCommand;
|
||||
import tech.nevets.modbot.api.commands.ISlashCommand;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandManager {
|
||||
public static final List<ICommand> ALL_COMMANDS = new ArrayList<>();
|
||||
public static final List<IPrefixCommand> ALL_PREFIX_COMMANDS = new ArrayList<>();
|
||||
public static final List<ISlashCommand> ALL_SLASH_COMMANDS = new ArrayList<>();
|
||||
|
||||
public static void addFromRegistry(CommandRegistry registry) {
|
||||
ALL_COMMANDS.addAll(registry.getCommands());
|
||||
ALL_PREFIX_COMMANDS.addAll(registry.getPrefixCommands());
|
||||
ALL_SLASH_COMMANDS.addAll(registry.getSlashCommands());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ICommand getCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (ICommand cmd : ALL_COMMANDS) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<ICommand> getAllCommands() {
|
||||
return ALL_COMMANDS;
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
public static IPrefixCommand getPrefixCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (IPrefixCommand cmd : ALL_PREFIX_COMMANDS) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<IPrefixCommand> getAllPrefixCommands() {
|
||||
return ALL_PREFIX_COMMANDS;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ISlashCommand getSlashCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (ISlashCommand cmd : ALL_SLASH_COMMANDS) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<ISlashCommand> getAllSlashCommands() {
|
||||
return ALL_SLASH_COMMANDS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user