Search in sources :

Example 11 with CommandSubscriber

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

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

the class GeneralCommands method command_Uptime.

@CommandSubscriber(command = "uptime", help = "Zeigt seit wann der Bot online ist")
public void command_Uptime(final IMessage message) {
    final DateTimeFormatter timeStampFormatter = DateTimeFormatter.ofPattern("dd.MM. | HH:mm");
    Util.sendMessage(message.getChannel(), "Online seit: " + startupTimestamp.format(timeStampFormatter));
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) CommandSubscriber(de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)

Aggregations

CommandSubscriber (de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)12 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)5 ScheduledFuture (java.util.concurrent.ScheduledFuture)3 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)3 CommandModule (de.nikos410.discordBot.util.modular.annotations.CommandModule)2 Method (java.lang.reflect.Method)2 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 IChannel (sx.blah.discord.handle.obj.IChannel)1 IUser (sx.blah.discord.handle.obj.IUser)1