Listeners progress

This commit is contained in:
Steven Tracey 2022-09-20 12:36:54 -04:00
parent 30bb267366
commit 373a889f95
18 changed files with 1094 additions and 27 deletions

2
.gitignore vendored
View File

@ -147,4 +147,4 @@ bin/
build build
*-config.yml *-config.yml
modules/ modulesre/

View File

@ -8,7 +8,7 @@ plugins {
mainClassName = 'tech.nevets.modbot.CoreBot' mainClassName = 'tech.nevets.modbot.CoreBot'
group 'tech.nevets' group 'tech.nevets'
version '1.1.0' version '1.2.0'
sourceCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17
@ -25,7 +25,7 @@ repositories {
dependencies { dependencies {
implementation 'net.dv8tion:JDA:4.4.0_350' 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 'com.google.code.gson:gson:2.9.0'
implementation 'me.carleslc.Simple-YAML:Simple-Yaml:1.7.2' implementation 'me.carleslc.Simple-YAML:Simple-Yaml:1.7.2'
} }

View File

@ -1,9 +1,6 @@
package tech.nevets.modbot.util.commands; package tech.nevets.modbot;
import tech.nevets.modbot.api.commands.CommandRegistry; import tech.nevets.modbot.api.commands.*;
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 javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,6 +11,20 @@ public class CommandManager {
public static final List<IPrefixCommand> ALL_PREFIX_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 final List<ISlashCommand> ALL_SLASH_COMMANDS = new ArrayList<>();
public static void addFromRegistry(List<? extends ICommand> cmds) {
for (ICommand cmd : cmds) {
ALL_COMMANDS.add(cmd);
if (cmd instanceof IPrefixCommand) {
ALL_PREFIX_COMMANDS.add((IPrefixCommand) cmd);
}
if (cmd instanceof ISlashCommand) {
ALL_SLASH_COMMANDS.add((ISlashCommand) cmd);
}
}
}
public static void addFromRegistry(CommandRegistry registry) { public static void addFromRegistry(CommandRegistry registry) {
ALL_COMMANDS.addAll(registry.getCommands()); ALL_COMMANDS.addAll(registry.getCommands());
ALL_PREFIX_COMMANDS.addAll(registry.getPrefixCommands()); ALL_PREFIX_COMMANDS.addAll(registry.getPrefixCommands());

View File

@ -9,7 +9,7 @@ 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.util.commands.CoreListener; import tech.nevets.modbot.internal.InternalListener;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.util.*; import java.util.*;
@ -28,7 +28,7 @@ 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();
CoreListener coreListener = new CoreListener(); 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)

File diff suppressed because it is too large Load Diff

View 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);
}
}

View File

@ -44,9 +44,6 @@ public class ModuleLoader {
return modules; return modules;
} }
//TODO: make this dynamically wrap all interface modules into abstract modules
//TODO: handle both types of modules
public static List<BotModule> loadModules() { public static List<BotModule> loadModules() {
List<Path> modulePaths = getModules(); List<Path> modulePaths = getModules();
List<BotModule> modules = new ArrayList<>(); List<BotModule> modules = new ArrayList<>();
@ -103,6 +100,7 @@ public class ModuleLoader {
iNewModule = (IBotModule) mainClass.getDeclaredConstructor().newInstance(); iNewModule = (IBotModule) mainClass.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
LOGGER.error("Error while creating instance of module " + moduleName); LOGGER.error("Error while creating instance of module " + moduleName);
//TODO: Check if setting Module-Type to inherit wile using extend breaks here
e.printStackTrace(); e.printStackTrace();
continue; continue;
} }
@ -112,6 +110,7 @@ public class ModuleLoader {
newModule = (BotModule) mainClass.getDeclaredConstructor(String.class).newInstance(moduleName); newModule = (BotModule) mainClass.getDeclaredConstructor(String.class).newInstance(moduleName);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
LOGGER.error("Error while creating instance of module " + moduleName); LOGGER.error("Error while creating instance of module " + moduleName);
LOGGER.error("HINT: Check the Module-Type of module " + moduleName);
e.printStackTrace(); e.printStackTrace();
continue; continue;
} }
@ -137,7 +136,7 @@ public class ModuleLoader {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < modules.size(); i++) { for (int i = 0; i < modules.size(); i++) {
sb.append(modules.get(i).getModuleName()); sb.append(modules.get(i).getModuleName());
if (i < modules.size()) { if ((i + 1) < modules.size()) {
sb.append(", "); sb.append(", ");
} }
} }

View File

@ -0,0 +1,20 @@
package tech.nevets.modbot;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import tech.nevets.modbot.api.commands.IPrefixCommand;
import tech.nevets.modbot.api.commands.ISlashCommand;
public class Registry {
public static void register(IPrefixCommand cmd) {
}
public static void register(ISlashCommand cmd) {
}
public static void register(ListenerAdapter listener) {
}
}

View File

@ -7,7 +7,7 @@ public interface IBotModule {
default void onPreEnable() {} default void onPreEnable() {}
CommandRegistry loadCommandRegistry(); void registry();
void onEnable(JDA jda); void onEnable(JDA jda);

View File

@ -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.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import tech.nevets.modbot.api.commands.ICommandContext;
import java.util.List; import java.util.List;

View File

@ -8,7 +8,6 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tech.nevets.modbot.CoreBot; import tech.nevets.modbot.CoreBot;
import tech.nevets.modbot.util.commands.CommandContext;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -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.JDA;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;

View File

@ -1,7 +1,5 @@
package tech.nevets.modbot.api.commands; package tech.nevets.modbot.api.commands;
import tech.nevets.modbot.util.commands.CommandContext;
public interface IPrefixCommand extends ICommand { public interface IPrefixCommand extends ICommand {
void handle(CommandContext ctx); void handle(CommandContext ctx);

View File

@ -1,7 +1,6 @@
package tech.nevets.modbot.api.commands; package tech.nevets.modbot.api.commands;
import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import tech.nevets.modbot.util.commands.CommandContext;
public interface ISlashCommand extends ICommand { public interface ISlashCommand extends ICommand {

View File

@ -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.entities.User;
import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.ReadyEvent;
@ -9,11 +9,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tech.nevets.modbot.CoreBot; import tech.nevets.modbot.CoreBot;
import tech.nevets.modbot.api.commands.CommandRegistry; 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; import javax.annotation.Nonnull;
public class CoreListener extends ListenerAdapter { public class InternalListener extends ListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(CoreListener.class); private static final Logger LOGGER = LoggerFactory.getLogger(InternalListener.class);
private final CommandRegistry registry = new CommandRegistry(); private final CommandRegistry registry = new CommandRegistry();
{ {

View File

@ -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.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.Command; 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.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import tech.nevets.modbot.CommandManager;
import tech.nevets.modbot.CoreBot; 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.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;

View File

@ -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.entities.IMentionable;
import net.dv8tion.jda.api.interactions.commands.OptionType; 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.CommandData;
import tech.nevets.modbot.api.commands.CommandContext;
import tech.nevets.modbot.api.commands.ISlashCommand; import tech.nevets.modbot.api.commands.ISlashCommand;
public class TestCmd implements ISlashCommand { public class TestCmd implements ISlashCommand {

View File

@ -29,6 +29,6 @@ public class Pair<L,R> {
@Override @Override
public String toString() { public String toString() {
return "Pair{" + left + ", right=" + right + "}"; return "Pair{left=" + left + ", right=" + right + "}";
} }
} }