Slight bug fixes with module loading
This commit is contained in:
parent
41c1d28bcc
commit
07db7b7abd
@ -8,7 +8,7 @@ plugins {
|
|||||||
mainClassName = 'net.nevet5gi.buzzbot.Bot'
|
mainClassName = 'net.nevet5gi.buzzbot.Bot'
|
||||||
|
|
||||||
group 'net.nevet5gi'
|
group 'net.nevet5gi'
|
||||||
version '0.5.0'
|
version '0.5.1'
|
||||||
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
@ -17,11 +17,11 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
|
|
||||||
public class Bot {
|
public class BuzzBot {
|
||||||
//TODO Make shutdown sequence
|
//TODO Make shutdown sequence
|
||||||
public static JDA jda;
|
public static JDA jda;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(BuzzBot.class);
|
||||||
public static final ArrayList<ListenerAdapter> PLUGINS = new ArrayList<>();
|
public static final ArrayList<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
|
||||||
private static ArrayList<BuzzyModule> modules;
|
private static ArrayList<BuzzyModule> modules;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -43,7 +43,7 @@ public class Bot {
|
|||||||
))
|
))
|
||||||
.enableCache(CacheFlag.VOICE_STATE);
|
.enableCache(CacheFlag.VOICE_STATE);
|
||||||
|
|
||||||
for (ListenerAdapter listener : PLUGINS) {
|
for (ListenerAdapter listener : PLUGIN_LISTENERS) {
|
||||||
builder.addEventListeners(listener);
|
builder.addEventListeners(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,5 +77,9 @@ public class Bot {
|
|||||||
jda.getPresence().setActivity(Activity.playing("with myself!"));
|
jda.getPresence().setActivity(Activity.playing("with myself!"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void addListener(ListenerAdapter listener) {
|
||||||
|
PLUGIN_LISTENERS.add(listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
package net.nevet5gi.buzzbot.commands;
|
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.CommandContext;
|
||||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||||
import net.nevet5gi.buzzbot.database.SqlDB;
|
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();
|
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.setModId(ctx.getMessage().getAuthor().getIdLong());
|
||||||
ban.setModName(ctx.getMessage().getAuthor().getName());
|
ban.setModName(ctx.getMessage().getAuthor().getName());
|
||||||
ban.setServerId(ctx.getGuild().getIdLong());
|
ban.setServerId(ctx.getGuild().getIdLong());
|
||||||
|
@ -2,7 +2,7 @@ package net.nevet5gi.buzzbot.commands;
|
|||||||
|
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
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.Config;
|
||||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||||
@ -28,7 +28,7 @@ public class PingCmd implements ICommand, ISlashCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleSlash(CommandContext ctx) {
|
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());
|
jda.getRestPing().queue(ping -> ctx.getHook().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package net.nevet5gi.buzzbot.commands;
|
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.CommandContext;
|
||||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||||
import net.nevet5gi.buzzbot.database.SqlDB;
|
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();
|
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.setModId(ctx.getMessage().getAuthor().getIdLong());
|
||||||
warn.setModName(ctx.getMessage().getAuthor().getName());
|
warn.setModName(ctx.getMessage().getAuthor().getName());
|
||||||
warn.setServerId(ctx.getGuild().getIdLong());
|
warn.setServerId(ctx.getGuild().getIdLong());
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
package net.nevet5gi.buzzbot.commands.utils;
|
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.Member;
|
||||||
import net.dv8tion.jda.api.entities.Role;
|
import net.dv8tion.jda.api.entities.Role;
|
||||||
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 net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
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.Config;
|
||||||
import net.nevet5gi.buzzbot.commands.*;
|
import net.nevet5gi.buzzbot.commands.*;
|
||||||
import net.nevet5gi.buzzbot.database.SqlDB;
|
import net.nevet5gi.buzzbot.database.SqlDB;
|
||||||
@ -62,7 +61,7 @@ public class CommandManager {
|
|||||||
for (ISlashCommand slashCmd : slashList) {
|
for (ISlashCommand slashCmd : slashList) {
|
||||||
cmdDataList.add(slashCmd.getCommandData());
|
cmdDataList.add(slashCmd.getCommandData());
|
||||||
}
|
}
|
||||||
Bot.jda.updateCommands().addCommands(cmdDataList).queue();
|
BuzzBot.jda.updateCommands().addCommands(cmdDataList).queue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,10 +29,6 @@ public class ModuleLoader {
|
|||||||
Files.walk(Paths.get("./modules/"))
|
Files.walk(Paths.get("./modules/"))
|
||||||
.filter(Files::isRegularFile)
|
.filter(Files::isRegularFile)
|
||||||
.forEach(modules::add);
|
.forEach(modules::add);
|
||||||
|
|
||||||
for (Path path : modules) {
|
|
||||||
System.out.println("Module path: " + path.toString());
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Unable to get module list");
|
System.out.println("Unable to get module list");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -49,22 +45,30 @@ public class ModuleLoader {
|
|||||||
JarInputStream jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
|
JarInputStream jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
|
||||||
Manifest mf = jarStream.getManifest();
|
Manifest mf = jarStream.getManifest();
|
||||||
Attributes attributes = mf.getMainAttributes();
|
Attributes attributes = mf.getMainAttributes();
|
||||||
String mainClassString = attributes.getValue("Main-Class");
|
String className = attributes.getValue("Main-Class");
|
||||||
if (mainClassString == null) {
|
if (className == null) {
|
||||||
LOGGER.error("Unable to get Main Class from module " + module.toString());
|
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();
|
File file = module.toFile();
|
||||||
URL[] urls = {file.toURI().toURL()};
|
URL[] urls = {file.toURI().toURL()};
|
||||||
|
|
||||||
URLClassLoader urlClassLoader = URLClassLoader.newInstance(urls);
|
URLClassLoader urlClassLoader = URLClassLoader.newInstance(urls);
|
||||||
|
|
||||||
Class<? extends BuzzyModule> mainClass = (Class<? extends BuzzyModule>) Class.forName(className, true, urlClassLoader);
|
if (urlClassLoader == null) {
|
||||||
BuzzyModule newModule = mainClass.getDeclaredConstructor().newInstance();
|
System.out.println("Classloader is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> mainClass = Class.forName(className, true, urlClassLoader);
|
||||||
|
BuzzyModule newModule = (BuzzyModule) mainClass.getDeclaredConstructor().newInstance();
|
||||||
newModule.onPreEnable();
|
newModule.onPreEnable();
|
||||||
modules.add(newModule);
|
modules.add(newModule);
|
||||||
|
if (jarStream != null ) {
|
||||||
|
jarStream.close();
|
||||||
|
}
|
||||||
|
if (urlClassLoader != null) {
|
||||||
|
urlClassLoader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return modules;
|
return modules;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user