Upload contents

This commit is contained in:
2022-08-10 15:55:16 -04:00
commit caa1c93027
22 changed files with 850 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package tech.nevets.modbot;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
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.CommandListener;
import javax.security.auth.login.LoginException;
import java.util.*;
public class CoreBot {
public static JDA jda;
public static final Config CORE_CONFIG = new Config("core");
public static final List<ListenerAdapter> PLUGIN_LISTENERS = new ArrayList<>();
private static List<BotModule> modules;
private static final Logger LOGGER = LoggerFactory.getLogger(CoreBot.class);
public static void main(String[] args) {
CORE_CONFIG.addDefaults(getDefaults());
CORE_CONFIG.loadConfig();
modules = ModuleLoader.loadModules();
JDABuilder builder = JDABuilder.createDefault(CORE_CONFIG.getConfig().getString("bot.token"))
.enableCache(CacheFlag.VOICE_STATE)
.addEventListeners(new CommandListener());
for (ListenerAdapter listener : PLUGIN_LISTENERS) {
builder.addEventListeners(listener);
}
try {
jda = builder.build();
} catch (LoginException e) {
e.printStackTrace();
}
//CommandManager.registerSlashCommands();
for (BotModule module : modules) {
module.onEnable(jda);
}
}
public static void addListener(ListenerAdapter listener) {
PLUGIN_LISTENERS.add(listener);
}
private static Map<String, Object> getDefaults() {
Map<String, Object> defaults = new HashMap<>();
defaults.put("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
defaults.put("bot.prefix", "!");
return defaults;
}
}

View File

@@ -0,0 +1,114 @@
package tech.nevets.modbot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.modbot.api.BotModule;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
public class ModuleLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(ModuleLoader.class);
public static List<Path> getModules() {
File modulesPath = new File("./modules/");
if (!modulesPath.exists()) {
modulesPath.mkdirs();
}
List<Path> modules = new ArrayList<>();
try {
Files.walk(Paths.get("./modules/"))
.filter(Files::isRegularFile)
.forEach(modules::add);
} catch (IOException e) {
LOGGER.error("Unable to get module list");
e.printStackTrace();
}
return modules;
}
public static List<BotModule> loadModules() {
List<Path> modulePaths = getModules();
List<BotModule> modules = new ArrayList<>();
for (Path module : modulePaths) {
JarInputStream jarStream;
Manifest mf;
Attributes attributes;
String mainClassName;
try {
jarStream = new JarInputStream(new FileInputStream(String.valueOf(module)));
mf = jarStream.getManifest();
attributes = mf.getMainAttributes();
mainClassName = attributes.getValue("Main-Class");
} catch (final Exception e) {
LOGGER.error("Error while reading module attributes from module: " + module.toString());
e.printStackTrace();
continue;
}
if (mainClassName == null) {
LOGGER.error("Unable to get mainClassName from module: " + module.toString());
continue;
}
File file = module.toFile();
URL[] url;
try {
url = new URL[]{file.toURI().toURL()};
} catch (MalformedURLException e) {
LOGGER.error("Error occurred while getting file URL");
e.printStackTrace();
continue;
}
URLClassLoader urlClassLoader = URLClassLoader.newInstance(url);
Class<?> mainClass;
try {
mainClass = Class.forName(mainClassName, true, urlClassLoader);
} catch (ClassNotFoundException e) {
LOGGER.error("Error getting class from classLoader, Class Not Found!");
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();
continue;
}
newModule.onPreEnable();
modules.add(newModule);
try {
jarStream.close();
if (urlClassLoader != null) {
urlClassLoader.close();
}
} catch (IOException e) {
LOGGER.error("Error closing jarStream or classLoader");
e.printStackTrace();
}
}
return modules;
}
}

View File

@@ -0,0 +1,12 @@
package tech.nevets.modbot.api;
import net.dv8tion.jda.api.JDA;
public interface BotModule {
default void onPreEnable() {}
void onEnable(JDA jda);
void onDisable();
}

View File

@@ -0,0 +1,71 @@
package tech.nevets.modbot.api;
import org.simpleyaml.configuration.file.YamlFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Config {
private final YamlFile CONFIG_FILE;
private final String FILE_NAME;
private Map<String, Object> defaults;
private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);
public Config(String configName) {
CONFIG_FILE = new YamlFile("./" + configName + "-config.yml");
this.FILE_NAME = configName;
this.defaults = new HashMap<>();
}
public Config(String configName, Map<String, Object> defaults) {
CONFIG_FILE = new YamlFile("./" + configName + "-config.yml");
this.FILE_NAME = configName;
this.defaults = defaults;
}
public void addDefault(String path, Object value) {
if (path != null && value != null) {
defaults.put(path, value);
}
}
public void addDefaults(Map<String, Object> defaults) {
if (defaults != null) {
this.defaults.putAll(defaults);
}
}
public void loadConfig() {
try {
if (!CONFIG_FILE.exists()) {
LOGGER.info("Config file not found, creating new one...");
CONFIG_FILE.createNewFile(true);
LOGGER.info("Config file created at: " + CONFIG_FILE.getFilePath());
} else {
LOGGER.info("Loading Config: " + CONFIG_FILE.getName() + "...");
CONFIG_FILE.loadWithComments();
LOGGER.info("Config file loaded!");
}
} catch (final Exception e) {
LOGGER.error("Error while loading config:");
e.printStackTrace();
}
CONFIG_FILE.addDefaults(defaults);
try {
CONFIG_FILE.save();
} catch (IOException e) {
LOGGER.error("Error saving config: ");
e.printStackTrace();
}
}
public YamlFile getConfig() {
return CONFIG_FILE;
}
}

View File

@@ -0,0 +1,4 @@
package tech.nevets.modbot.api.commands;
public interface ICommand {
}

View File

@@ -0,0 +1,4 @@
package tech.nevets.modbot.api.commands;
public interface IPrefixCommand extends ICommand {
}

View File

@@ -0,0 +1,4 @@
package tech.nevets.modbot.api.commands;
public interface ISlashCommand extends ICommand {
}

View File

@@ -0,0 +1,34 @@
package tech.nevets.modbot.util;
public class Pair<L,R> {
private L left;
private R right;
public Pair() {}
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return left;
}
public void setLeft(L left) {
this.left = left;
}
public R getRight() {
return right;
}
public void setRight(R right) {
this.right = right;
}
@Override
public String toString() {
return "Pair{" + left + ", right=" + right + "}";
}
}

View File

@@ -0,0 +1,10 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommandListener extends ListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandListener.class);
private final CommandManager commandManager = new CommandManager();
}

View File

@@ -0,0 +1,9 @@
package tech.nevets.modbot.util.commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommandManager {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandManager.class);
}

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-34.-34thread %10.10X{jda.shard} %-15.-15logger{0} %-6level %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>