This commit is contained in:
Steven Tracey 2023-07-06 12:40:57 -04:00
parent 373a889f95
commit ca5aa87019
4 changed files with 165 additions and 136 deletions

View File

@ -1,82 +1,79 @@
package tech.nevets.modbot; 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 tech.nevets.modbot.api.commands.*;
import javax.annotation.Nullable; import java.util.Arrays;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
public class CommandManager { 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(List<? extends ICommand> cmds) {
for (ICommand cmd : cmds) { public boolean hasPermission(Member member, ICommand cmd) {
ALL_COMMANDS.add(cmd); List<Role> memberRoles = member.getRoles();
if (cmd instanceof IPrefixCommand) { int permLevel = 0;
ALL_PREFIX_COMMANDS.add((IPrefixCommand) cmd);
} for (Role memberRole : memberRoles) {
if (cmd instanceof ISlashCommand) { switch (memberRole.getName().toLowerCase()) {
ALL_SLASH_COMMANDS.add((ISlashCommand) cmd); 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);
public static void addFromRegistry(CommandRegistry registry) { assert event.getMember() != null;
ALL_COMMANDS.addAll(registry.getCommands()); assert cmd != null;
ALL_PREFIX_COMMANDS.addAll(registry.getPrefixCommands()); if (!hasPermission(event.getMember(), cmd)) {
ALL_SLASH_COMMANDS.addAll(registry.getSlashCommands()); event.getMessage().reply("You do not have permission to use this command.").queue();
} return;
@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;
event.getChannel().sendTyping().queue();
List<String> args = Arrays.asList(split).subList(1, split.length);
CommandContext ctx = new CommandContext(event, args);
cmd.handle(ctx);
} }
public static List<ICommand> getAllCommands() { public void handle(SlashCommandEvent event) {
return ALL_COMMANDS; String invoke = event.getName();
} ISlashCommand cmd = Registry.getSlashCommand(invoke);
if (cmd != null) {
event.deferReply().queue();
@Nullable assert event.getMember() != null;
public static IPrefixCommand getPrefixCommand(String search) { if (!hasPermission(event.getMember(), cmd)) {
String searchLower = search.toLowerCase(); event.getHook().sendMessage("You do not have permission to use this command.").queue();
return;
for (IPrefixCommand cmd : ALL_PREFIX_COMMANDS) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
} }
CommandContext ctx = new CommandContext(event);
cmd.handleSlash(ctx);
} }
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;
} }
} }

View File

@ -4,18 +4,18 @@ import ch.qos.logback.classic.Level;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.hooks.ListenerAdapter; 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 net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tech.nevets.modbot.api.BotModule; import tech.nevets.modbot.api.BotModule;
import tech.nevets.modbot.api.Config; import tech.nevets.modbot.api.Config;
import tech.nevets.modbot.internal.InternalListener;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.util.*; import java.util.*;
public class CoreBot { public class CoreBot {
public static JDA jda; private static JDA jda;
public static final Config CORE_CONFIG = new Config("core"); public static final Config CORE_CONFIG = new Config("core");
public static final List<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>(); public static final List<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
private static String botPrefix; private static String botPrefix;
@ -28,11 +28,10 @@ public class CoreBot {
CORE_CONFIG.loadConfig(); CORE_CONFIG.loadConfig();
botPrefix = CORE_CONFIG.getConfig().getString("bot.prefix"); botPrefix = CORE_CONFIG.getConfig().getString("bot.prefix");
modules = ModuleLoader.loadModules(); modules = ModuleLoader.loadModules();
InternalListener coreListener = new InternalListener();
JDABuilder builder = JDABuilder.createDefault(CORE_CONFIG.getConfig().getString("bot.token")) JDABuilder builder = JDABuilder.createDefault(CORE_CONFIG.getConfig().getString("bot.token"))
.enableCache(CacheFlag.VOICE_STATE) .enableCache(CacheFlag.VOICE_STATE)
.addEventListeners(coreListener); .addEventListeners(Registry.getListeners());
for (ListenerAdapter listener : PLUGIN_LISTENERS) { for (ListenerAdapter listener : PLUGIN_LISTENERS) {
builder.addEventListeners(listener); builder.addEventListeners(listener);
@ -44,7 +43,8 @@ public class CoreBot {
e.printStackTrace(); e.printStackTrace();
} }
coreListener.getCommandRegistry().registerSlashCommands(); Registry.loadSlashCommands();
for (BotModule module : modules) { for (BotModule module : modules) {
if (module.loadCommandRegistry() == null) { if (module.loadCommandRegistry() == null) {
LOGGER.warn("Module has no commands to load"); LOGGER.warn("Module has no commands to load");
@ -56,11 +56,10 @@ public class CoreBot {
} }
public static void addListener(ListenerAdapter listener) { public static void loadSlashCommands(List<CommandData> commandData) {
PLUGIN_LISTENERS.add(listener); jda.updateCommands().addCommands(commandData).queue();
} }
private static Map<String, Object> getDefaults() { private static Map<String, Object> getDefaults() {
Map<String, Object> defaults = new HashMap<>(); Map<String, Object> defaults = new HashMap<>();
defaults.put("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); defaults.put("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

View File

@ -1,20 +1,120 @@
package tech.nevets.modbot; 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.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.IPrefixCommand;
import tech.nevets.modbot.api.commands.ISlashCommand; 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 { 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) { public static void register(IPrefixCommand cmd) {
if (!exists(cmd)) {
COMMANDS.add(cmd);
PREFIX_COMMANDS.add(cmd);
}
} }
public static void register(ISlashCommand cmd) { public static void register(ISlashCommand cmd) {
if (!exists(cmd)) {
COMMANDS.add(cmd);
SLASH_COMMANDS.add(cmd);
}
} }
public static void register(ListenerAdapter listener) { 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);
}
} }
} }

View File

@ -23,33 +23,6 @@ public class CommandRegistry {
public 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 @Nullable
public IPrefixCommand getPrefixCommand(String search) { public IPrefixCommand getPrefixCommand(String search) {
String searchLower = search.toLowerCase(); String searchLower = search.toLowerCase();
@ -109,45 +82,5 @@ public class CommandRegistry {
return permLevel >= cmd.getPermissionLevel(); 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;
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 = 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);
}
}
} }