Search in sources :

Example 1 with CommandModule

use of de.nikos410.discordBot.util.modular.annotations.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.util.modular.annotations.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 3 with CommandModule

use of de.nikos410.discordBot.util.modular.annotations.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)

Example 4 with CommandModule

use of de.nikos410.discordBot.util.modular.annotations.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method makeCommandMap.

private void makeCommandMap() {
    this.commands.clear();
    for (final String key : this.loadedModules.keySet()) {
        Object module = this.loadedModules.get(key);
        for (Method method : module.getClass().getMethods()) {
            if (method.isAnnotationPresent(CommandSubscriber.class)) {
                final CommandSubscriber[] annotations = method.getDeclaredAnnotationsByType(CommandSubscriber.class);
                final String command = annotations[0].command();
                final String help = annotations[0].help();
                final boolean pmAllowed = annotations[0].pmAllowed();
                final int permissionLevel = annotations[0].permissionLevel();
                final int parameterCount = method.getParameterCount();
                final boolean passContext = annotations[0].passContext();
                // Mindestens 1 (message), max 6 (message + 5 parameter)
                if (parameterCount > 0 && parameterCount <= 6) {
                    final Command cmd = new Command(module, method, help, pmAllowed, permissionLevel, parameterCount - 1, passContext);
                    this.commands.put(command.toLowerCase(), cmd);
                } else {
                    System.err.println("Ungültige Anzahl Parameter bei Befehl " + command);
                }
            }
        }
        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: ");
                Util.error(e);
            }
        }
    }
}
Also used : 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)

Example 5 with CommandModule

use of de.nikos410.discordBot.util.modular.annotations.CommandModule in project de-DiscordBot by DACH-Discord.

the class DiscordBot method unloadModule.

public String unloadModule(final String moduleName) throws NullPointerException {
    if (moduleName.isEmpty()) {
        return "Fehler! Kein Modul angegeben.";
    }
    if (!this.loadedModules.containsKey(moduleName)) {
        return "Fehler! Modul `" + moduleName + "` ist bereits deaktiviert oder existiert nicht.";
    }
    // Modul in andere Map übertragen und entfernen
    final Object module = this.loadedModules.get(moduleName);
    if (module.getClass().isAnnotationPresent(AlwaysLoaded.class)) {
        return "Dieses Modul kann nicht deaktiviert werden.";
    }
    this.unloadedModules.put(moduleName, module);
    this.loadedModules.remove(moduleName);
    this.makeCommandMap();
    // Modul in JSON-Array speichern
    final JSONArray jsonUnloadedModules = this.configJSON.getJSONArray("unloadedModules");
    jsonUnloadedModules.put(moduleName);
    this.saveJSON();
    // EventListener deaktivieren
    final CommandModule moduleAnnotation = module.getClass().getDeclaredAnnotationsByType(CommandModule.class)[0];
    if (!moduleAnnotation.commandOnly()) {
        try {
            this.client.getDispatcher().unregisterListener(module);
        } catch (NullPointerException e) {
            System.err.println("[Error] Could not get EventDispatcher!");
            throw e;
        }
    }
    return ":x: Modul `" + moduleName + "` deaktiviert.";
}
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)

Aggregations

CommandModule (de.nikos410.discordBot.util.modular.annotations.CommandModule)5 JSONObject (org.json.JSONObject)4 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)4 JSONArray (org.json.JSONArray)3 CommandSubscriber (de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)2 Method (java.lang.reflect.Method)2 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)1