use of de.nikos410.discordbot.framework.ModuleWrapper in project de-DiscordBot by DACH-Discord.
the class BotSetup method command_listModules.
@CommandSubscriber(command = "modules", help = "Alle Module anzeigen")
public void command_listModules(final IMessage message) {
final EmbedBuilder embedBuilder = new EmbedBuilder();
// List loaded modules
final StringBuilder loadedBuilder = new StringBuilder();
for (ModuleWrapper module : bot.getLoadedModules()) {
loadedBuilder.append(module.getName());
loadedBuilder.append('\n');
}
final String loadedModulesString = loadedBuilder.toString().isEmpty() ? "_keine_" : loadedBuilder.toString();
embedBuilder.appendField("Aktivierte Module", loadedModulesString, true);
// List unloaded modules
final StringBuilder unloadedBuilder = new StringBuilder();
for (ModuleWrapper module : bot.getUnloadedModules()) {
unloadedBuilder.append(module.getName());
unloadedBuilder.append('\n');
}
final String unloadedModulesString = unloadedBuilder.toString().isEmpty() ? "_keine_" : unloadedBuilder.toString();
embedBuilder.appendField("Deaktivierte Module", unloadedModulesString, true);
// Add failed modules, if present
final List<ModuleWrapper> failedModules = bot.getFailedModules();
if (!failedModules.isEmpty()) {
final StringBuilder failedBuilder = new StringBuilder();
for (ModuleWrapper module : failedModules) {
failedBuilder.append(module.getName());
failedBuilder.append('\n');
}
embedBuilder.appendField("Folgende Module konnten nicht geladen werden:", failedBuilder.toString(), true);
}
DiscordIO.sendEmbed(message.getChannel(), embedBuilder.build());
}
use of de.nikos410.discordbot.framework.ModuleWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method loadModule.
/**
* Load a module from a class specified in a {@link ModuleWrapper}
*
* @param wrapper The
*/
private CommandModule loadModule(final ModuleWrapper wrapper) {
LOG.debug("Loading module {}.", wrapper.getName());
// Check if module is deactivated in config
final JSONArray jsonUnloadedModules = this.configJSON.getJSONArray("unloadedModules");
if (!wrapper.getModuleClass().equals(BotSetup.class) && jsonUnloadedModules.toList().contains(wrapper.getName())) {
LOG.info("Module '{}' is deactivated. Skipping.", wrapper.getName());
wrapper.setStatus(ModuleStatus.INACTIVE);
return null;
}
LOG.debug("Loading module '{}'.", wrapper.getName());
final CommandModule moduleInstance = instantiateModule(wrapper.getModuleClass());
wrapper.setInstance(moduleInstance);
if (moduleInstance == null) {
// Module could not be created -> Add to failed modules
wrapper.setStatus(ModuleStatus.FAILED);
return null;
}
// Fill wrapper fields
wrapper.setDisplayName(moduleInstance.getDisplayName());
// Set bot field and run initialization for module
moduleInstance.setBot(this);
moduleInstance.init();
// Register EventListener if needed
if (moduleInstance.hasEvents()) {
final EventDispatcher dispatcher = this.client.getDispatcher();
dispatcher.registerListener(moduleInstance);
}
// Register all commands
wrapper.setCommands(discoverCommands(wrapper));
wrapper.setStatus(ModuleStatus.ACTIVE);
LOG.info("Successfully loaded module '{}'.", wrapper.getName());
return moduleInstance;
}
use of de.nikos410.discordbot.framework.ModuleWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method makeCommandMap.
/**
* Populates global command map, maps a 'CommandWrapper' instance, containing a commands attributes, to each command.
*/
private void makeCommandMap() {
LOG.debug("Creating command map.");
LOG.debug("Clearing old commands.");
this.commands.clear();
final List<ModuleWrapper> loadedModules = getLoadedModules();
for (final ModuleWrapper moduleWrapper : loadedModules) {
for (final CommandWrapper commandWrapper : moduleWrapper.getCommands()) {
this.commands.put(commandWrapper.getName().toLowerCase(), commandWrapper);
}
}
}
use of de.nikos410.discordbot.framework.ModuleWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method activateModule.
/**
* Activate a module so the bot loads it
*
* @param moduleName The name of the module
* @return true if everything went fine, false if the module does not exist or is already actived
*/
public ModuleWrapper activateModule(final String moduleName) {
if (!modules.containsKey(moduleName)) {
// Module does not exist
return null;
}
final ModuleWrapper module = modules.get(moduleName);
if (module.getStatus().equals(ModuleStatus.ACTIVE)) {
// Module is already active
return null;
}
LOG.debug("Removing module from unloaded JSON array");
removeFromJSONArray(configJSON.getJSONArray("unloadedModules"), moduleName);
this.saveConfig();
LOG.info("Activating module '{}'.", moduleName);
final CommandModule moduleInstance = loadModule(module);
if (moduleInstance == null) {
LOG.error("Module {} could not be loaded.", moduleName);
return module;
}
// Run init tasks
LOG.debug("Running init tasks.");
moduleInstance.init();
// This method can only be executed by a command, so we don't have to check if the bot is ready
moduleInstance.initWhenReady();
LOG.info("Rebuilding command map to include commands from module '{}'.", moduleName);
makeCommandMap();
// Everything went fine
return module;
}
Aggregations