Search in sources :

Example 6 with CommandSubscriber

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

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

the class UserGroups method command_groups.

@CommandSubscriber(command = "groups", help = "Alle Rollen auflisten")
public void command_groups(final IMessage message) {
    final StringBuilder stringBuilder = new StringBuilder();
    final List<String> keyList = new LinkedList<>();
    keyList.addAll(usergroupsJSON.keySet());
    Collections.sort(keyList);
    for (String key : keyList) {
        if (stringBuilder.length() != 0) {
            stringBuilder.append('\n');
        }
        stringBuilder.append(key);
    }
    if (stringBuilder.length() == 0) {
        stringBuilder.append("_Keine_");
    }
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.appendField("Verfügbare Gruppen:", stringBuilder.toString(), false);
    embedBuilder.withFooterText("Weise dir mit '" + bot.configJSON.getString("prefix") + "group <Gruppe>' selbst eine dieser Gruppen zu");
    Util.sendEmbed(message.getChannel(), embedBuilder.build());
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CommandSubscriber(de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)

Example 8 with CommandSubscriber

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

the class UserLog method command_Userlog_Test.

@CommandSubscriber(command = "userlog_test", help = "Userlog-Ausgabe testen", permissionLevel = CommandPermissions.ADMIN)
public void command_Userlog_Test(final IMessage message) {
    final IUser user = message.getAuthor();
    userJoinNotify(user);
    userLeaveNotify(user);
    userBanNotify(user);
}
Also used : IUser(sx.blah.discord.handle.obj.IUser) CommandSubscriber(de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)

Example 9 with CommandSubscriber

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

the class ModStuff method command_Mute.

@CommandSubscriber(command = "mute", help = "Einen Nutzer für eine bestimmte Zeit muten", pmAllowed = false, permissionLevel = CommandPermissions.MODERATOR)
public void command_Mute(final IMessage message, final String muteUserString, final String muteDurationInput) {
    final List<IUser> mentions = message.getMentions();
    if (mentions.size() < 1) {
        Util.sendMessage(message.getChannel(), ":x: Fehler: Kein Nutzer angegeben!");
        return;
    } else if (mentions.size() > 1) {
        Util.sendMessage(message.getChannel(), ":x: Fehler: mehrere Nutzer erwähnt");
        return;
    }
    final IUser muteUser = mentions.get(0);
    if (muteUser == null) {
        Util.sendMessage(message.getChannel(), ":x: Fehler: Nutzer nicht gefunden!");
        return;
    }
    Pattern pattern = Pattern.compile("(\\d+)\\s?([smhd])\\s?(.*)");
    Matcher matcher = pattern.matcher(muteDurationInput);
    if (!matcher.matches()) {
        Util.sendMessage(message.getChannel(), "Ungültige Eingabe! Mögliche Zeitformate sind s, m, h und d.");
        return;
    }
    IRole muteRole = message.getGuild().getRoleByID(muteRoleID);
    // Wird ausgeführt, um Nutzer wieder zu entmuten
    Runnable unmuteTask = () -> {
        mutedUsers.remove(muteUser.getStringID());
        muteUser.removeRole(muteRole);
        System.out.println("Unmuted user " + Util.makeUserString(muteUser, message.getGuild()) + ".");
    };
    final int muteDuration = Integer.parseInt(matcher.group(1));
    final String muteDurationUnitString = matcher.group(2);
    ChronoUnit muteDurationUnit = parseChronoUnit(muteDurationUnitString);
    if (mutedUsers.containsKey(muteUser.getStringID())) {
        // Überprüfen, ob angegebener Zeitpunkt nach dem bisherigen Zeitpunkt liegt
        ScheduledFuture oldFuture = mutedUsers.get(muteUser.getStringID());
        LocalDateTime oldDateTime = LocalDateTime.now().plusSeconds(oldFuture.getDelay(TimeUnit.SECONDS));
        LocalDateTime newDateTime = LocalDateTime.now().plus(muteDuration, muteDurationUnit);
        if (newDateTime.isBefore(oldDateTime)) {
            // neuer Zeitpunkt ist vor altem -> nichts tun (längerer Mute bleibt bestehen)
            Util.sendMessage(message.getChannel(), "Nutzer ist bereits für einen längeren Zeitraum gemuted!");
            return;
        } else {
            // neuer Zeitpunkt ist nach altem -> neu schedulen
            mutedUsers.remove(muteUser.getStringID(), oldFuture);
            oldFuture.cancel(false);
        }
    } else {
        muteUser.addRole(muteRole);
        System.out.println("Muted user " + Util.makeUserString(muteUser, message.getGuild()) + ".");
    }
    ScheduledFuture newFuture = scheduler.schedule(unmuteTask, muteDuration, chronoUnitToTimeUnit(muteDurationUnit));
    mutedUsers.put(muteUser.getStringID(), newFuture);
    // :mute:
    message.addReaction(ReactionEmoji.of("\uD83D\uDD07"));
    String customMessage = matcher.group(3);
    if (customMessage.isEmpty()) {
        customMessage = "kein";
    }
    final String muteMessage = "**Du wurdest für " + muteDuration + ' ' + muteDurationUnitString + " gemuted!** \nHinweis: _" + customMessage + " _";
    if (!muteUser.isBot()) {
        Util.sendPM(muteUser, muteMessage);
    }
    // Modlog
    IChannel modLogChannel = message.getGuild().getChannelByID(this.modlogChannelID);
    final String modLogMessage = String.format("**%s** hat Nutzer **%s** für %s %s **gemuted**. \nHinweis: _%s _", Util.makeUserString(message.getAuthor(), message.getGuild()), Util.makeUserString(muteUser, message.getGuild()), muteDuration, muteDurationUnitString, customMessage);
    Util.sendMessage(modLogChannel, modLogMessage);
}
Also used : LocalDateTime(java.time.LocalDateTime) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ScheduledFuture(java.util.concurrent.ScheduledFuture) ChronoUnit(java.time.temporal.ChronoUnit) CommandSubscriber(de.nikos410.discordBot.util.modular.annotations.CommandSubscriber)

Example 10 with CommandSubscriber

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

the class ModStuff method command_Selfmute.

@CommandSubscriber(command = "selfmute", help = "Schalte dich selber für die angegebene Zeit stumm", pmAllowed = false)
public void command_Selfmute(final IMessage message, final String muteDurationInput) {
    IUser muteUser = message.getAuthor();
    Pattern pattern = Pattern.compile("(\\d+)\\s?([smhd])\\s?(.*)");
    Matcher matcher = pattern.matcher(muteDurationInput);
    if (!matcher.matches()) {
        Util.sendMessage(message.getChannel(), "Ungültige Eingabe! Mögliche Zeitformate sind s, m, h und d.");
        return;
    }
    IRole muteRole = message.getGuild().getRoleByID(muteRoleID);
    // Wird ausgeführt, um Nutzer wieder zu entmuten
    Runnable unmuteTask = () -> {
        mutedUsers.remove(muteUser.getStringID());
        muteUser.removeRole(muteRole);
        System.out.println("Unmuted user " + Util.makeUserString(muteUser, message.getGuild()) + ". (was selfmuted)");
    };
    final int muteDuration = Integer.parseInt(matcher.group(1));
    final String muteDurationUnitString = matcher.group(2);
    ChronoUnit muteDurationUnit = parseChronoUnit(muteDurationUnitString);
    if (mutedUsers.containsKey(muteUser.getStringID())) {
        // Überprüfen, ob angegebener Zeitpunkt nach dem bisherigen Zeitpunkt liegt
        ScheduledFuture oldFuture = mutedUsers.get(muteUser.getStringID());
        LocalDateTime oldDateTime = LocalDateTime.now().plusSeconds(oldFuture.getDelay(TimeUnit.SECONDS));
        LocalDateTime newDateTime = LocalDateTime.now().plus(muteDuration, muteDurationUnit);
        if (newDateTime.isBefore(oldDateTime)) {
            // neuer Zeitpunkt ist vor altem -> nichts tun (längerer Mute bleibt bestehen)
            Util.sendMessage(message.getChannel(), "Nutzer ist bereits für einen längeren Zeitraum gemuted!");
            return;
        } else {
            // neuer Zeitpunkt ist nach altem -> neu schedulen
            mutedUsers.remove(muteUser.getStringID(), oldFuture);
            oldFuture.cancel(false);
        }
    } else {
        muteUser.addRole(muteRole);
        System.out.println("User " + Util.makeUserString(muteUser, message.getGuild()) + " selfmuted.");
    }
    ScheduledFuture newFuture = scheduler.schedule(unmuteTask, muteDuration, chronoUnitToTimeUnit(muteDurationUnit));
    mutedUsers.put(muteUser.getStringID(), newFuture);
    // :mute:
    message.addReaction(ReactionEmoji.of("\uD83D\uDD07"));
}
Also used : LocalDateTime(java.time.LocalDateTime) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ScheduledFuture(java.util.concurrent.ScheduledFuture) ChronoUnit(java.time.temporal.ChronoUnit) 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