Search in sources :

Example 6 with Context

use of fredboat.messaging.internal.Context in project FredBoat by Frederikam.

the class KickCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    Guild guild = context.guild;
    // Ensure we have a search term
    if (!context.hasArguments()) {
        HelpCommand.sendFormattedCommandHelp(context);
        return;
    }
    // was there a target provided?
    Member target = ArgumentUtil.checkSingleFuzzyMemberSearchResult(context, context.args[0]);
    if (target == null)
        return;
    // are we allowed to do that?
    if (!checkKickAuthorization(context, target))
        return;
    // putting together a reason
    String plainReason = DiscordUtil.getReasonForModAction(context);
    String auditLogReason = DiscordUtil.formatReasonForAuditLog(plainReason, context.invoker);
    // putting together the action
    RestAction<Void> modAction = guild.getController().kick(target, auditLogReason);
    // on success
    String successOutput = context.i18nFormat("kickSuccess", TextUtils.escapeAndDefuse(target.getUser().getName()), target.getUser().getDiscriminator(), target.getUser().getId()) + "\n" + plainReason;
    Consumer<Void> onSuccess = aVoid -> {
        Metrics.successfulRestActions.labels("kick").inc();
        context.replyWithName(successOutput);
    };
    // on fail
    String failOutput = context.i18nFormat("kickFail", target.getUser());
    Consumer<Throwable> onFail = t -> {
        CentralMessaging.getJdaRestActionFailureHandler(String.format("Failed to kick user %s in guild %s", target.getUser().getId(), guild.getId())).accept(t);
        context.replyWithName(failOutput);
    };
    // issue the mod action
    modAction.queue(onSuccess, onFail);
}
Also used : ArgumentUtil(fredboat.util.ArgumentUtil) CentralMessaging(fredboat.messaging.CentralMessaging) Logger(org.slf4j.Logger) Member(net.dv8tion.jda.core.entities.Member) Command(fredboat.commandmeta.abs.Command) LoggerFactory(org.slf4j.LoggerFactory) CommandContext(fredboat.commandmeta.abs.CommandContext) TextUtils(fredboat.util.TextUtils) IModerationCommand(fredboat.commandmeta.abs.IModerationCommand) RestAction(net.dv8tion.jda.core.requests.RestAction) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.core.entities.Guild) Permission(net.dv8tion.jda.core.Permission) Context(fredboat.messaging.internal.Context) DiscordUtil(fredboat.util.DiscordUtil) HelpCommand(fredboat.command.info.HelpCommand) Metrics(fredboat.feature.metrics.Metrics) Nonnull(javax.annotation.Nonnull) Guild(net.dv8tion.jda.core.entities.Guild) Member(net.dv8tion.jda.core.entities.Member)

Example 7 with Context

use of fredboat.messaging.internal.Context in project FredBoat by Frederikam.

the class ServerInfoCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    Guild guild = context.guild;
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
    EmbedBuilder eb = CentralMessaging.getColoredEmbedBuilder();
    eb.setTitle(context.i18nFormat("serverinfoTitle", guild.getName()), null);
    eb.setThumbnail(guild.getIconUrl());
    long onlineMembers = guild.getMemberCache().stream().filter(m -> m.getOnlineStatus() != OnlineStatus.OFFLINE).count();
    eb.addField(context.i18n("serverinfoOnlineUsers"), String.valueOf(onlineMembers), true);
    eb.addField(context.i18n("serverinfoTotalUsers"), String.valueOf(guild.getMembers().size()), true);
    eb.addField(context.i18n("serverinfoRoles"), String.valueOf(guild.getRoles().size()), true);
    eb.addField(context.i18n("serverinfoText"), String.valueOf(guild.getTextChannels().size()), true);
    eb.addField(context.i18n("serverinfoVoice"), String.valueOf(guild.getVoiceChannels().size()), true);
    eb.addField(context.i18n("serverinfoCreationDate"), guild.getCreationTime().format(dtf), true);
    eb.addField(context.i18n("serverinfoGuildID"), guild.getId(), true);
    eb.addField(context.i18n("serverinfoVLv"), guild.getVerificationLevel().name(), true);
    eb.addField(context.i18n("serverinfoOwner"), guild.getOwner().getAsMention(), true);
    String prefix = TextUtils.shorten(TextUtils.escapeMarkdown(context.getPrefix()), MessageEmbed.VALUE_MAX_LENGTH);
    if (prefix.isEmpty()) {
        prefix = "No Prefix";
    }
    eb.addField(context.i18n("prefix"), prefix, true);
    context.reply(eb.build());
}
Also used : CentralMessaging(fredboat.messaging.CentralMessaging) Guild(net.dv8tion.jda.core.entities.Guild) Context(fredboat.messaging.internal.Context) DateTimeFormatter(java.time.format.DateTimeFormatter) Command(fredboat.commandmeta.abs.Command) CommandContext(fredboat.commandmeta.abs.CommandContext) TextUtils(fredboat.util.TextUtils) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) Nonnull(javax.annotation.Nonnull) IUtilCommand(fredboat.commandmeta.abs.IUtilCommand) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) OnlineStatus(net.dv8tion.jda.core.OnlineStatus) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Guild(net.dv8tion.jda.core.entities.Guild) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 8 with Context

use of fredboat.messaging.internal.Context in project FredBoat by Frederikam.

the class CommandsCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    if (!context.hasArguments()) {
        Collection<Module> enabledModules = context.getEnabledModules();
        if (!PermsUtil.checkPerms(PermissionLevel.BOT_ADMIN, context.invoker)) {
            // dont show admin commands/modules for non admins
            enabledModules.remove(Module.ADMIN);
        }
        String prefixAndCommand = "`" + context.getPrefix() + CommandInitializer.COMMANDS_COMM_NAME;
        List<String> translatedModuleNames = enabledModules.stream().map(module -> context.i18n(module.translationKey)).collect(Collectors.toList());
        context.reply(context.i18nFormat("modulesCommands", prefixAndCommand + " <module>`", prefixAndCommand + " " + ALL + "`") + "\n\n" + context.i18nFormat("musicCommandsPromotion", "`" + context.getPrefix() + CommandInitializer.MUSICHELP_COMM_NAME + "`") + "\n\n" + context.i18n("modulesEnabledInGuild") + " **" + String.join("**, **", translatedModuleNames) + "**" + "\n" + context.i18nFormat("commandsModulesHint", "`" + context.getPrefix() + CommandInitializer.MODULES_COMM_NAME + "`"));
        return;
    }
    List<Module> showHelpFor;
    if (context.rawArgs.toLowerCase().contains(ALL.toLowerCase())) {
        showHelpFor = new ArrayList<>(Arrays.asList(Module.values()));
        if (!PermsUtil.checkPerms(PermissionLevel.BOT_ADMIN, context.invoker)) {
            // dont show admin commands/modules for non admins
            showHelpFor.remove(Module.ADMIN);
        }
    } else {
        Module module = CommandRegistry.whichModule(context.rawArgs, context);
        if (module == null) {
            context.reply(context.i18nFormat("moduleCantParse", "`" + context.getPrefix() + context.command.name) + "`");
            return;
        } else {
            showHelpFor = Collections.singletonList(module);
        }
    }
    EmbedBuilder eb = CentralMessaging.getColoredEmbedBuilder();
    for (Module module : showHelpFor) {
        eb = addModuleCommands(eb, context, CommandRegistry.getCommandModule(module));
    }
    eb.addField("", context.i18nFormat("commandsMoreHelp", "`" + TextUtils.escapeMarkdown(context.getPrefix()) + CommandInitializer.HELP_COMM_NAME + " <command>`"), false);
    context.reply(eb.build());
}
Also used : CommandRegistry(fredboat.commandmeta.CommandRegistry) CentralMessaging(fredboat.messaging.CentralMessaging) java.util(java.util) IInfoCommand(fredboat.commandmeta.abs.IInfoCommand) Command(fredboat.commandmeta.abs.Command) ICommandRestricted(fredboat.commandmeta.abs.ICommandRestricted) CommandContext(fredboat.commandmeta.abs.CommandContext) PermissionLevel(fredboat.definitions.PermissionLevel) TextUtils(fredboat.util.TextUtils) Collectors(java.util.stream.Collectors) CommandInitializer(fredboat.commandmeta.CommandInitializer) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Module(fredboat.definitions.Module) Context(fredboat.messaging.internal.Context) Nonnull(javax.annotation.Nonnull) DestroyCommand(fredboat.command.music.control.DestroyCommand) PermsUtil(fredboat.perms.PermsUtil) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Module(fredboat.definitions.Module)

Example 9 with Context

use of fredboat.messaging.internal.Context in project FredBoat by Frederikam.

the class CommandsCommand method addModuleCommands.

private EmbedBuilder addModuleCommands(@Nonnull EmbedBuilder embedBuilder, @Nonnull CommandContext context, @Nonnull CommandRegistry module) {
    List<Command> commands = module.getDeduplicatedCommands().stream().filter(command -> {
        if (command instanceof ICommandRestricted) {
            if (((ICommandRestricted) command).getMinimumPerms().getLevel() >= PermissionLevel.BOT_ADMIN.getLevel()) {
                return PermsUtil.checkPerms(PermissionLevel.BOT_ADMIN, context.invoker);
            }
        }
        return true;
    }).collect(Collectors.toList());
    String prefix = context.getPrefix();
    if (commands.size() >= 6) {
        // split the commands into three even columns
        StringBuilder[] sbs = new StringBuilder[3];
        sbs[0] = new StringBuilder();
        sbs[1] = new StringBuilder();
        sbs[2] = new StringBuilder();
        int i = 0;
        for (Command c : commands) {
            if (c instanceof DestroyCommand) {
                // dont want to publicly show this one
                continue;
            }
            sbs[i++ % 3].append(prefix).append(c.name).append("\n");
        }
        return embedBuilder.addField(context.i18n(module.module.translationKey), sbs[0].toString(), true).addField("", sbs[1].toString(), true).addField("", sbs[2].toString(), true);
    } else {
        StringBuilder sb = new StringBuilder();
        for (Command c : commands) {
            sb.append(prefix).append(c.name).append("\n");
        }
        return embedBuilder.addField(context.i18n(module.module.translationKey), sb.toString(), true).addBlankField(true).addBlankField(true);
    }
}
Also used : CommandRegistry(fredboat.commandmeta.CommandRegistry) CentralMessaging(fredboat.messaging.CentralMessaging) java.util(java.util) IInfoCommand(fredboat.commandmeta.abs.IInfoCommand) Command(fredboat.commandmeta.abs.Command) ICommandRestricted(fredboat.commandmeta.abs.ICommandRestricted) CommandContext(fredboat.commandmeta.abs.CommandContext) PermissionLevel(fredboat.definitions.PermissionLevel) TextUtils(fredboat.util.TextUtils) Collectors(java.util.stream.Collectors) CommandInitializer(fredboat.commandmeta.CommandInitializer) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Module(fredboat.definitions.Module) Context(fredboat.messaging.internal.Context) Nonnull(javax.annotation.Nonnull) DestroyCommand(fredboat.command.music.control.DestroyCommand) PermsUtil(fredboat.perms.PermsUtil) IInfoCommand(fredboat.commandmeta.abs.IInfoCommand) Command(fredboat.commandmeta.abs.Command) DestroyCommand(fredboat.command.music.control.DestroyCommand) ICommandRestricted(fredboat.commandmeta.abs.ICommandRestricted) DestroyCommand(fredboat.command.music.control.DestroyCommand)

Example 10 with Context

use of fredboat.messaging.internal.Context in project FredBoat by Frederikam.

the class SoftbanCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    Guild guild = context.guild;
    // Ensure we have a search term
    if (!context.hasArguments()) {
        HelpCommand.sendFormattedCommandHelp(context);
        return;
    }
    // was there a target provided?
    Member target = ArgumentUtil.checkSingleFuzzyMemberSearchResult(context, context.args[0]);
    if (target == null)
        return;
    // are we allowed to do that?
    if (!checkAuthorization(context, target))
        return;
    // putting together a reason
    String plainReason = DiscordUtil.getReasonForModAction(context);
    String auditLogReason = DiscordUtil.formatReasonForAuditLog(plainReason, context.invoker);
    // putting together the action
    RestAction<Void> modAction = guild.getController().ban(target, 7, auditLogReason);
    // on success
    String successOutput = context.i18nFormat("softbanSuccess", TextUtils.escapeAndDefuse(target.getUser().getName()), target.getUser().getDiscriminator(), target.getUser().getId()) + "\n" + plainReason;
    Consumer<Void> onSuccess = aVoid -> {
        Metrics.successfulRestActions.labels("ban").inc();
        guild.getController().unban(target.getUser()).queue(__ -> Metrics.successfulRestActions.labels("unban").inc(), CentralMessaging.getJdaRestActionFailureHandler(String.format("Failed to unban user %s in guild %s", target.getUser().getId(), guild.getId())));
        context.replyWithName(successOutput);
    };
    // on fail
    String failOutput = context.i18nFormat("modBanFail", target.getUser());
    Consumer<Throwable> onFail = t -> {
        CentralMessaging.getJdaRestActionFailureHandler(String.format("Failed to ban user %s in guild %s", target.getUser().getId(), guild.getId())).accept(t);
        context.replyWithName(failOutput);
    };
    // issue the mod action
    modAction.queue(onSuccess, onFail);
}
Also used : ArgumentUtil(fredboat.util.ArgumentUtil) CentralMessaging(fredboat.messaging.CentralMessaging) Logger(org.slf4j.Logger) Member(net.dv8tion.jda.core.entities.Member) Command(fredboat.commandmeta.abs.Command) LoggerFactory(org.slf4j.LoggerFactory) CommandContext(fredboat.commandmeta.abs.CommandContext) TextUtils(fredboat.util.TextUtils) IModerationCommand(fredboat.commandmeta.abs.IModerationCommand) RestAction(net.dv8tion.jda.core.requests.RestAction) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.core.entities.Guild) Permission(net.dv8tion.jda.core.Permission) Context(fredboat.messaging.internal.Context) DiscordUtil(fredboat.util.DiscordUtil) HelpCommand(fredboat.command.info.HelpCommand) Metrics(fredboat.feature.metrics.Metrics) Nonnull(javax.annotation.Nonnull) Guild(net.dv8tion.jda.core.entities.Guild) Member(net.dv8tion.jda.core.entities.Member)

Aggregations

Command (fredboat.commandmeta.abs.Command)11 CommandContext (fredboat.commandmeta.abs.CommandContext)11 Context (fredboat.messaging.internal.Context)11 TextUtils (fredboat.util.TextUtils)11 Nonnull (javax.annotation.Nonnull)11 CentralMessaging (fredboat.messaging.CentralMessaging)8 PermissionLevel (fredboat.definitions.PermissionLevel)6 HelpCommand (fredboat.command.info.HelpCommand)5 ICommandRestricted (fredboat.commandmeta.abs.ICommandRestricted)5 Launcher (fredboat.main.Launcher)5 Collectors (java.util.stream.Collectors)5 PermsUtil (fredboat.perms.PermsUtil)4 Permission (net.dv8tion.jda.core.Permission)4 Guild (net.dv8tion.jda.core.entities.Guild)4 GuildPlayer (fredboat.audio.player.GuildPlayer)3 CommandInitializer (fredboat.commandmeta.CommandInitializer)3 CommandRegistry (fredboat.commandmeta.CommandRegistry)3 IInfoCommand (fredboat.commandmeta.abs.IInfoCommand)3 IModerationCommand (fredboat.commandmeta.abs.IModerationCommand)3 Module (fredboat.definitions.Module)3