Search in sources :

Example 26 with CommandSubscriber

use of de.nikos410.discordbot.framework.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 27 with CommandSubscriber

use of de.nikos410.discordbot.framework.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)

Example 28 with CommandSubscriber

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

the class BotSetup method command_help.

@CommandSubscriber(command = "help", help = "Zeigt diese Hilfe an")
public void command_help(final IMessage message) {
    final EmbedBuilder helpEmbedBuilder = new EmbedBuilder();
    final List<ModuleWrapper> loadedModules = bot.getLoadedModules();
    for (ModuleWrapper module : loadedModules) {
        final StringBuilder moduleHelpBuilder = new StringBuilder();
        for (CommandWrapper command : module.getCommands()) {
            // Only list commands that are available to that user
            if (bot.getUserPermissionLevel(message.getAuthor(), message.getGuild()).getLevel() >= command.getPermissionLevel().getLevel()) {
                moduleHelpBuilder.append(String.format("`%s` - %s%n", command.getName(), command.getHelp()));
            }
        }
        final String helpString = moduleHelpBuilder.toString();
        if (moduleHelpBuilder.length() > 0) {
            helpEmbedBuilder.appendField(module.getDisplayName(), helpString, false);
        }
    }
    final EmbedObject embedObject = helpEmbedBuilder.build();
    DiscordIO.sendEmbed(message.getAuthor().getOrCreatePMChannel(), embedObject);
    if (!message.getChannel().isPrivate()) {
        DiscordIO.sendMessage(message.getChannel(), ":mailbox_with_mail:");
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) ModuleWrapper(de.nikos410.discordbot.framework.ModuleWrapper) CommandWrapper(de.nikos410.discordbot.framework.CommandWrapper) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Example 29 with CommandSubscriber

use of de.nikos410.discordbot.framework.annotations.CommandSubscriber 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 30 with CommandSubscriber

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

the class GameStats method command_playing.

@CommandSubscriber(command = "playing", help = "Zeigt alle Nutzer die das angegebene Spiel spielen", pmAllowed = false)
public void command_playing(final IMessage message, final String game) {
    final IGuild guild = message.getGuild();
    // Similar games
    final List<String> similarGames = findSimilarKeys(game, guild);
    // Users who play the game right now
    List<IUser> playingNowUsers = new ArrayList<>();
    for (IUser user : guild.getUsers()) {
        final String currentUserGame = getCurrentGame(user);
        if (game.equalsIgnoreCase(currentUserGame)) {
            playingNowUsers.add(user);
        }
    }
    // Users who played the game at any point
    List<IUser> playingAnyUsers = new ArrayList<>();
    if (gameStatsJSON.has(guild.getStringID())) {
        final JSONObject guildJSON = gameStatsJSON.getJSONObject(guild.getStringID());
        if (guildJSON.has(game.toLowerCase())) {
            final JSONArray gameArray = guildJSON.getJSONArray(game.toLowerCase());
            for (int i = 0; i < gameArray.length(); i++) {
                final IUser user = guild.getUserByID(gameArray.getLong(i));
                // Only add if user isn't playing right now
                if (user != null && !playingNowUsers.contains(user)) {
                    playingAnyUsers.add(user);
                }
            }
        }
    }
    sendPlayingResponse(message, game, playingNowUsers, playingAnyUsers, similarGames);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) CommandSubscriber(de.nikos410.discordbot.framework.annotations.CommandSubscriber)

Aggregations

CommandSubscriber (de.nikos410.discordbot.framework.annotations.CommandSubscriber)30 JSONObject (org.json.JSONObject)23 IGuild (sx.blah.discord.handle.obj.IGuild)14 CommandSubscriber (de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)12 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)11 ChronoUnit (java.time.temporal.ChronoUnit)5 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)5 LocalDateTime (java.time.LocalDateTime)4 DateTimeFormatter (java.time.format.DateTimeFormatter)4 IChannel (sx.blah.discord.handle.obj.IChannel)4 ModuleWrapper (de.nikos410.discordbot.framework.ModuleWrapper)3 CommandUtils (de.nikos410.discordbot.util.CommandUtils)3 Method (java.lang.reflect.Method)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)3 JSONArray (org.json.JSONArray)3 IUser (sx.blah.discord.handle.obj.IUser)3 CommandModule (de.nikos410.discordBot.util.modular.annotations.CommandModule)2 CommandWrapper (de.nikos410.discordbot.framework.CommandWrapper)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2