Search in sources :

Example 1 with CommandModule

use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method command_help.

private void command_help(final IMessage message) {
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    for (final String key : this.loadedModules.keySet()) {
        Object module = this.loadedModules.get(key);
        String moduleHelp = "";
        for (Method method : module.getClass().getMethods()) {
            if (method.isAnnotationPresent(CommandSubscriber.class)) {
                final CommandSubscriber annotation = method.getDeclaredAnnotationsByType(CommandSubscriber.class)[0];
                if (this.getUserPermissionLevel(message.getAuthor(), message.getGuild()) >= annotation.permissionLevel()) {
                    final String command = annotation.command();
                    final String help = annotation.help();
                    moduleHelp = moduleHelp + "`" + command + "` " + help + '\n';
                }
            }
        }
        final CommandModule[] annotations = module.getClass().getDeclaredAnnotationsByType(CommandModule.class);
        final String moduleName = annotations[0].moduleName();
        if (!moduleHelp.isEmpty()) {
            embedBuilder.appendField(moduleName, moduleHelp, false);
        }
    }
    final EmbedObject embedObject = embedBuilder.build();
    Util.sendEmbed(message.getAuthor().getOrCreatePMChannel(), embedObject);
    if (!message.getChannel().isPrivate()) {
        Util.sendMessage(message.getChannel(), ":mailbox_with_mail:");
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CommandModule(de.nikos410.discordBot.util.modular.annotations.CommandModule) JSONObject(org.json.JSONObject) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) Method(java.lang.reflect.Method) CommandSubscriber(de.nikos410.discordBot.util.modular.annotations.CommandSubscriber) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject)

Example 2 with CommandModule

use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method discoverModules.

/**
 * Finds all classes in the package {@link de.nikos410.discordbot.modules} that extend {@link CommandModule}
 * and populates the module map using the module names as keys.
 */
private void discoverModules() {
    // Search in package 'de.nikos410.discordbot.modules'
    final Reflections reflections = new Reflections("de.nikos410.discordbot.modules");
    // Find classes that are subtypes of CommandModule
    final Set<Class<? extends CommandModule>> foundModuleClasses = reflections.getSubTypesOf(CommandModule.class);
    for (Class<? extends CommandModule> currentModuleClass : foundModuleClasses) {
        final ModuleWrapper module = new ModuleWrapper(currentModuleClass);
        modules.put(module.getName(), module);
    }
}
Also used : CommandModule(de.nikos410.discordbot.framework.CommandModule) ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper) Reflections(org.reflections.Reflections)

Example 3 with CommandModule

use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method loadModules.

/**
 * Loads modules from classes that are located in the package 'de.nikos410.discordbot.modules' and are annotated
 * with @CommandModule.
 */
private void loadModules() {
    LOG.debug("Loading modules.");
    LOG.info("Found {} total module(s).", this.modules.size());
    // Load modules from all found classes
    for (ModuleWrapper wrapper : this.modules.values()) {
        try {
            loadModule(wrapper);
        } catch (Exception e) {
            LOG.error("Failed to load module " + wrapper.getName() + ".", e);
            wrapper.setStatus(ModuleStatus.FAILED);
        }
    }
    // Wait until the bot is ready to run initializations
    LOG.debug("Waiting until the bot is ready.");
    while (!client.isReady()) {
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            LOG.warn("Sleep was interrupted");
            Thread.currentThread().interrupt();
        }
    }
    LOG.debug("Bot is ready. Running Inits.");
    for (ModuleWrapper wrapper : this.modules.values()) {
        final CommandModule module = wrapper.getInstance();
        if (wrapper.getStatus().equals(ModuleStatus.ACTIVE)) {
            module.initWhenReady();
        }
    }
    // Create command map
    this.makeCommandMap();
    LOG.info("{} module(s) with {} command(s) active.", this.modules.size(), this.commands.size());
}
Also used : CommandModule(de.nikos410.discordbot.framework.CommandModule) ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper) InitializationException(de.nikos410.discordbot.exception.InitializationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DiscordException(sx.blah.discord.util.DiscordException)

Example 4 with CommandModule

use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method loadModule.

public String loadModule(final String moduleName) throws NullPointerException {
    if (moduleName.isEmpty()) {
        return "Fehler! Kein Modul angegeben.";
    }
    if (!this.unloadedModules.containsKey(moduleName)) {
        return "Fehler! Modul `" + moduleName + "` ist bereits aktiviert oder existiert nicht.";
    }
    // Modul in andere Map übertragen und entfernen
    final Object module = this.unloadedModules.get(moduleName);
    this.loadedModules.put(moduleName, module);
    this.unloadedModules.remove(moduleName);
    this.makeCommandMap();
    // Modul aus JSON-Array entfernen
    final JSONArray jsonUnloadedModules = this.configJSON.getJSONArray("unloadedModules");
    for (int i = 0; i < jsonUnloadedModules.length(); i++) {
        final String unloadedModuleName = jsonUnloadedModules.getString(i);
        if (unloadedModuleName.equals(moduleName)) {
            jsonUnloadedModules.remove(i);
        }
    }
    this.saveJSON();
    // EventListener aktivieren
    final CommandModule moduleAnnotation = module.getClass().getDeclaredAnnotationsByType(CommandModule.class)[0];
    if (!moduleAnnotation.commandOnly()) {
        try {
            this.client.getDispatcher().registerListener(module);
        } catch (NullPointerException e) {
            System.err.println("[Error] Could not get EventDispatcher!");
            throw e;
        }
    }
    return ":white_check_mark: Modul `" + moduleName + "` aktiviert.";
}
Also used : CommandModule(de.nikos410.discordBot.util.modular.annotations.CommandModule) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject)

Example 5 with CommandModule

use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method addModule.

/**
 * Ein Modul zum Bot hinzufügen
 *
 * @param module Das Modul
 */
private void addModule(final Object module) {
    if (!module.getClass().isAnnotationPresent(CommandModule.class)) {
        System.err.println("Error: Class \"" + module.getClass().getName() + "\" is not a command module!");
        return;
    }
    final CommandModule moduleAnnotation = module.getClass().getDeclaredAnnotationsByType(CommandModule.class)[0];
    final String moduleName = moduleAnnotation.moduleName();
    final JSONArray jsonUnloadedModules = this.configJSON.getJSONArray("unloadedModules");
    for (int i = 0; i < jsonUnloadedModules.length(); i++) {
        final String unloadedModuleName = jsonUnloadedModules.getString(i);
        if (moduleName.equals(unloadedModuleName)) {
            // Modul ist in der Liste der deaktivierten Module enthalten -> ist deaktiviert
            this.unloadedModules.put(moduleName, module);
            return;
        }
    }
    // Modul ist nicht deaktiviert
    this.loadedModules.put(moduleName, module);
}
Also used : CommandModule(de.nikos410.discordBot.util.modular.annotations.CommandModule) JSONArray(org.json.JSONArray)

Aggregations

CommandModule (de.nikos410.discordBot.util.modular.annotations.CommandModule)5 CommandModule (de.nikos410.discordbot.framework.CommandModule)5 JSONArray (org.json.JSONArray)4 JSONObject (org.json.JSONObject)4 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)4 ModuleWrapper (de.nikos410.discordbot.framework.ModuleWrapper)3 Method (java.lang.reflect.Method)3 CommandSubscriber (de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InitializationException (de.nikos410.discordbot.exception.InitializationException)1 Reflections (org.reflections.Reflections)1 EventDispatcher (sx.blah.discord.api.events.EventDispatcher)1 DiscordException (sx.blah.discord.util.DiscordException)1 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)1