Search in sources :

Example 1 with Module

use of fredboat.definitions.Module in project FredBoat by Frederikam.

the class ModulesCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    if (!context.hasArguments()) {
        displayModuleStatus(context);
        return;
    }
    if (!PermsUtil.checkPermsWithFeedback(PermissionLevel.ADMIN, context)) {
        return;
    }
    // editing module status happens here
    String args = context.rawArgs.toLowerCase();
    boolean enable;
    if (args.contains("enable")) {
        enable = true;
    } else if (args.contains("disable")) {
        enable = false;
    } else {
        HelpCommand.sendFormattedCommandHelp(context);
        return;
    }
    Module module = CommandRegistry.whichModule(args, context);
    if (module == null) {
        context.reply(context.i18nFormat("moduleCantParse", context.getPrefix() + context.command.name));
        return;
    } else if (module.lockedModule) {
        context.reply(context.i18nFormat("moduleLocked", context.i18n(module.translationKey)) + "\n" + MAGICAL_LENNY);
        return;
    }
    Function<GuildModules, GuildModules> transform;
    String output;
    if (enable) {
        transform = gm -> gm.enableModule(module);
        output = context.i18nFormat("moduleEnable", "**" + context.i18n(module.translationKey) + "**") + "\n" + context.i18nFormat("moduleShowCommands", "`" + context.getPrefix() + CommandInitializer.COMMANDS_COMM_NAME + " " + context.i18n(module.translationKey) + "`");
    } else {
        transform = gm -> gm.disableModule(module);
        output = context.i18nFormat("moduleDisable", "**" + context.i18n(module.translationKey) + "**");
    }
    Launcher.getBotController().getGuildModulesService().transformGuildModules(context.guild, transform);
    // if the transaction right above this line fails, it won't be reached, which is intended
    context.reply(output);
}
Also used : GuildModules(fredboat.db.transfer.GuildModules) Module(fredboat.definitions.Module)

Example 2 with Module

use of fredboat.definitions.Module in project FredBoat by Frederikam.

the class EventListenerBoat method doOnMessageReceived.

private void doOnMessageReceived(MessageReceivedEvent event) {
    if (ratelimiter.isBlacklisted(event.getAuthor().getIdLong())) {
        Metrics.blacklistedMessagesReceived.inc();
        return;
    }
    if (event.getPrivateChannel() != null) {
        log.info("PRIVATE" + " \t " + event.getAuthor().getName() + " \t " + event.getMessage().getContentRaw());
        return;
    }
    if (event.getAuthor().equals(event.getJDA().getSelfUser())) {
        log.info(event.getMessage().getContentRaw());
        return;
    }
    if (event.getAuthor().isBot()) {
        return;
    }
    // never null since we are filtering private messages out above
    TextChannel channel = event.getTextChannel();
    // where we can't talk in
    if (!channel.canTalk() && !event.getMessage().getContentRaw().toLowerCase().contains(CommandInitializer.HELP_COMM_NAME)) {
        return;
    }
    CommandContext context = commandContextParser.parse(event);
    if (context == null) {
        return;
    }
    log.info(event.getMessage().getContentRaw());
    // ignore all commands in channels where we can't write, except for the help command
    if (!channel.canTalk() && !(context.command instanceof HelpCommand)) {
        log.info("Ignoring command {} because this bot cannot write in that channel", context.command.name);
        return;
    }
    Metrics.commandsReceived.labels(context.command.getClass().getSimpleName()).inc();
    // BOT_ADMINs can always use all commands everywhere
    if (!PermsUtil.checkPerms(PermissionLevel.BOT_ADMIN, event.getMember())) {
        // ignore commands of disabled modules for plebs
        Module module = context.command.getModule();
        if (module != null && !context.getEnabledModules().contains(module)) {
            log.debug("Ignoring command {} because its module {} is disabled in guild {}", context.command.name, module.name(), event.getGuild().getIdLong());
            return;
        }
    }
    limitOrExecuteCommand(context);
}
Also used : CommandContext(fredboat.commandmeta.abs.CommandContext) HelpCommand(fredboat.command.info.HelpCommand) Module(fredboat.definitions.Module)

Example 3 with Module

use of fredboat.definitions.Module 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 4 with Module

use of fredboat.definitions.Module 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 5 with Module

use of fredboat.definitions.Module in project FredBoat by Frederikam.

the class ModulesCommand method displayModuleStatus.

private static void displayModuleStatus(@Nonnull CommandContext context) {
    GuildModules gm = Launcher.getBotController().getGuildModulesService().fetchGuildModules(context.guild);
    Function<Module, String> moduleStatusFormatter = moduleStatusLine(gm, context);
    String moduleStatus = "";
    if (PermsUtil.checkPerms(PermissionLevel.BOT_ADMIN, context.invoker)) {
        moduleStatus = moduleStatusFormatter.apply(Module.ADMIN) + " " + Emojis.LOCK + "\n" + moduleStatusFormatter.apply(Module.INFO) + " " + Emojis.LOCK + "\n" + moduleStatusFormatter.apply(Module.CONFIG) + " " + Emojis.LOCK + "\n";
    }
    moduleStatus += moduleStatusFormatter.apply(Module.MUSIC) + " " + Emojis.LOCK + "\n" + moduleStatusFormatter.apply(Module.MOD) + "\n" + moduleStatusFormatter.apply(Module.UTIL) + "\n" + moduleStatusFormatter.apply(Module.FUN) + "\n";
    String howto = "`" + context.getPrefix() + CommandInitializer.MODULES_COMM_NAME + " " + ENABLE + "/" + DISABLE + " <module>`";
    context.reply(CentralMessaging.getColoredEmbedBuilder().addField(context.i18n("moduleStatus"), moduleStatus, false).addField("", context.i18nFormat("modulesHowTo", howto), false).build());
}
Also used : GuildModules(fredboat.db.transfer.GuildModules) Module(fredboat.definitions.Module)

Aggregations

Module (fredboat.definitions.Module)5 CommandContext (fredboat.commandmeta.abs.CommandContext)3 DestroyCommand (fredboat.command.music.control.DestroyCommand)2 CommandInitializer (fredboat.commandmeta.CommandInitializer)2 CommandRegistry (fredboat.commandmeta.CommandRegistry)2 Command (fredboat.commandmeta.abs.Command)2 ICommandRestricted (fredboat.commandmeta.abs.ICommandRestricted)2 IInfoCommand (fredboat.commandmeta.abs.IInfoCommand)2 GuildModules (fredboat.db.transfer.GuildModules)2 PermissionLevel (fredboat.definitions.PermissionLevel)2 CentralMessaging (fredboat.messaging.CentralMessaging)2 Context (fredboat.messaging.internal.Context)2 PermsUtil (fredboat.perms.PermsUtil)2 TextUtils (fredboat.util.TextUtils)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 HelpCommand (fredboat.command.info.HelpCommand)1