Search in sources :

Example 6 with ModuleWrapper

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());
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Example 7 with ModuleWrapper

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;
}
Also used : CommandModule(de.nikos410.discordbot.framework.CommandModule) EventDispatcher(sx.blah.discord.api.events.EventDispatcher) JSONArray(org.json.JSONArray)

Example 8 with ModuleWrapper

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);
        }
    }
}
Also used : ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper) CommandWrapper(de.nikos410.discordbot.framework.CommandWrapper)

Example 9 with ModuleWrapper

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;
}
Also used : CommandModule(de.nikos410.discordbot.framework.CommandModule) ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper)

Aggregations

ModuleWrapper (de.nikos410.discordbot.framework.ModuleWrapper)8 CommandModule (de.nikos410.discordbot.framework.CommandModule)5 CommandWrapper (de.nikos410.discordbot.framework.CommandWrapper)3 CommandSubscriber (de.nikos410.discordbot.framework.annotations.CommandSubscriber)3 InitializationException (de.nikos410.discordbot.exception.InitializationException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JSONArray (org.json.JSONArray)2 Reflections (org.reflections.Reflections)2 EventDispatcher (sx.blah.discord.api.events.EventDispatcher)2 DiscordException (sx.blah.discord.util.DiscordException)2 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)2 ModuleStatus (de.nikos410.discordbot.framework.ModuleWrapper.ModuleStatus)1 PermissionLevel (de.nikos410.discordbot.framework.PermissionLevel)1 BotSetup (de.nikos410.discordbot.modules.BotSetup)1 Authorization (de.nikos410.discordbot.util.discord.Authorization)1 DiscordIO (de.nikos410.discordbot.util.discord.DiscordIO)1 UserUtils (de.nikos410.discordbot.util.discord.UserUtils)1 IOUtil (de.nikos410.discordbot.util.io.IOUtil)1 Method (java.lang.reflect.Method)1 Files (java.nio.file.Files)1