Slight bug fixes with module loading

This commit is contained in:
Steven Tracey 2022-06-22 00:56:00 -04:00
parent 41c1d28bcc
commit 07db7b7abd
7 changed files with 31 additions and 24 deletions

View File

@ -8,7 +8,7 @@ plugins {
mainClassName = 'net.nevet5gi.buzzbot.Bot'
group 'net.nevet5gi'
version '0.5.0'
version '0.5.1'
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

View File

@ -17,11 +17,11 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.EnumSet;
public class Bot {
public class BuzzBot {
//TODO Make shutdown sequence
public static JDA jda;
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
public static final ArrayList<ListenerAdapter> PLUGINS = new ArrayList<>();
private static final Logger LOGGER = LoggerFactory.getLogger(BuzzBot.class);
public static final ArrayList<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
private static ArrayList<BuzzyModule> modules;
public static void main(String[] args) {
@ -43,7 +43,7 @@ public class Bot {
))
.enableCache(CacheFlag.VOICE_STATE);
for (ListenerAdapter listener : PLUGINS) {
for (ListenerAdapter listener : PLUGIN_LISTENERS) {
builder.addEventListeners(listener);
}
@ -77,5 +77,9 @@ public class Bot {
jda.getPresence().setActivity(Activity.playing("with myself!"));
}
}
public static void addListener(ListenerAdapter listener) {
PLUGIN_LISTENERS.add(listener);
}
}

View File

@ -1,6 +1,6 @@
package net.nevet5gi.buzzbot.commands;
import net.nevet5gi.buzzbot.Bot;
import net.nevet5gi.buzzbot.BuzzBot;
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand;
import net.nevet5gi.buzzbot.database.SqlDB;
@ -69,7 +69,7 @@ public class BanCmd implements ICommand {
ctx.getChannel().sendMessage("<@" + ctx.getMessage().getAuthor().getId() + "> banned <@" + ban.getUserId() + "> for " + ban.getBanLength() + " hours. Reason: " + ban.getReason()).queue();
}
Bot.jda.retrieveUserById(ban.getUserId()).queue(user -> { ban.setUserName(user.getName()); });
BuzzBot.jda.retrieveUserById(ban.getUserId()).queue(user -> { ban.setUserName(user.getName()); });
ban.setModId(ctx.getMessage().getAuthor().getIdLong());
ban.setModName(ctx.getMessage().getAuthor().getName());
ban.setServerId(ctx.getGuild().getIdLong());

View File

@ -2,7 +2,7 @@ package net.nevet5gi.buzzbot.commands;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.nevet5gi.buzzbot.Bot;
import net.nevet5gi.buzzbot.BuzzBot;
import net.nevet5gi.buzzbot.Config;
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand;
@ -28,7 +28,7 @@ public class PingCmd implements ICommand, ISlashCommand {
@Override
public void handleSlash(CommandContext ctx) {
JDA jda = Bot.jda;
JDA jda = BuzzBot.jda;
jda.getRestPing().queue(ping -> ctx.getHook().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue());
}

View File

@ -1,6 +1,6 @@
package net.nevet5gi.buzzbot.commands;
import net.nevet5gi.buzzbot.Bot;
import net.nevet5gi.buzzbot.BuzzBot;
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand;
import net.nevet5gi.buzzbot.database.SqlDB;
@ -37,7 +37,7 @@ public class WarnCmd implements ICommand {
ctx.getChannel().sendMessage("<@" + ctx.getMessage().getAuthor().getId() + "> warned <@" + warn.getUserId() + "> for reason: " + warn.getReason()).queue();
Bot.jda.retrieveUserById(warn.getUserId()).queue(user -> { warn.setUserName(user.getName()); });
BuzzBot.jda.retrieveUserById(warn.getUserId()).queue(user -> { warn.setUserName(user.getName()); });
warn.setModId(ctx.getMessage().getAuthor().getIdLong());
warn.setModName(ctx.getMessage().getAuthor().getName());
warn.setServerId(ctx.getGuild().getIdLong());

View File

@ -1,12 +1,11 @@
package net.nevet5gi.buzzbot.commands.utils;
import net.dv8tion.jda.api.entities.Guild;
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.interactions.commands.build.CommandData;
import net.nevet5gi.buzzbot.Bot;
import net.nevet5gi.buzzbot.BuzzBot;
import net.nevet5gi.buzzbot.Config;
import net.nevet5gi.buzzbot.commands.*;
import net.nevet5gi.buzzbot.database.SqlDB;
@ -62,7 +61,7 @@ public class CommandManager {
for (ISlashCommand slashCmd : slashList) {
cmdDataList.add(slashCmd.getCommandData());
}
Bot.jda.updateCommands().addCommands(cmdDataList).queue();
BuzzBot.jda.updateCommands().addCommands(cmdDataList).queue();
}
}

View File

@ -29,10 +29,6 @@ public class ModuleLoader {
Files.walk(Paths.get("./modules/"))
.filter(Files::isRegularFile)
.forEach(modules::add);
for (Path path : modules) {
System.out.println("Module path: " + path.toString());
}
} catch (IOException e) {
System.out.println("Unable to get module list");
e.printStackTrace();
@ -49,22 +45,30 @@ public class ModuleLoader {
JarInputStream jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
Manifest mf = jarStream.getManifest();
Attributes attributes = mf.getMainAttributes();
String mainClassString = attributes.getValue("Main-Class");
if (mainClassString == null) {
String className = attributes.getValue("Main-Class");
if (className == null) {
LOGGER.error("Unable to get Main Class from module " + module.toString());
}
String[] packages = mainClassString.split("\\.");
String className = packages[packages.length - 1];
File file = module.toFile();
URL[] urls = {file.toURI().toURL()};
URLClassLoader urlClassLoader = URLClassLoader.newInstance(urls);
Class<? extends BuzzyModule> mainClass = (Class<? extends BuzzyModule>) Class.forName(className, true, urlClassLoader);
BuzzyModule newModule = mainClass.getDeclaredConstructor().newInstance();
if (urlClassLoader == null) {
System.out.println("Classloader is null");
}
Class<?> mainClass = Class.forName(className, true, urlClassLoader);
BuzzyModule newModule = (BuzzyModule) mainClass.getDeclaredConstructor().newInstance();
newModule.onPreEnable();
modules.add(newModule);
if (jarStream != null ) {
jarStream.close();
}
if (urlClassLoader != null) {
urlClassLoader.close();
}
}
return modules;
}