Search in sources :

Example 1 with AliasCommand

use of net.kodehawa.mantarobot.core.modules.commands.AliasCommand in project MantaroBot by Mantaro.

the class CommandRegistry method process.

// BEWARE OF INSTANCEOF CALLS
// I know there are better approaches to this, THIS IS JUST A WORKAROUND, DON'T TRY TO REPLICATE THIS.
public boolean process(GuildMessageReceivedEvent event, String cmdName, String content) {
    long start = System.currentTimeMillis();
    Command command = commands.get(cmdName);
    if (command == null) {
        command = commands.get(cmdName.toLowerCase());
        if (command == null)
            return false;
    }
    // Variable used in lambda expression should be final or effectively final...
    final Command cmd = command;
    if (MantaroData.db().getMantaroData().getBlackListedUsers().contains(event.getAuthor().getId())) {
        return false;
    }
    DBGuild dbg = MantaroData.db().getGuild(event.getGuild());
    GuildData data = dbg.getData();
    if (data.getDisabledCommands().contains(cmd instanceof AliasCommand ? ((AliasCommand) cmd).getOriginalName() : cmdName)) {
        return false;
    }
    List<String> channelDisabledCommands = data.getChannelSpecificDisabledCommands().get(event.getChannel().getId());
    if (channelDisabledCommands != null && channelDisabledCommands.contains(cmd instanceof AliasCommand ? ((AliasCommand) cmd).getOriginalName() : cmdName)) {
        return false;
    }
    if (data.getDisabledUsers().contains(event.getAuthor().getId()) && !isAdmin(event.getMember())) {
        return false;
    }
    if (data.getDisabledChannels().contains(event.getChannel().getId()) && (cmd instanceof AliasCommand ? ((AliasCommand) cmd).parentCategory() != Category.MODERATION : cmd.category() != Category.MODERATION)) {
        return false;
    }
    if (conf.isPremiumBot() && (cmd instanceof AliasCommand ? ((AliasCommand) cmd).parentCategory() == Category.CURRENCY : cmd.category() == Category.CURRENCY)) {
        return false;
    }
    if (data.getDisabledCategories().contains(cmd instanceof AliasCommand ? ((AliasCommand) cmd).parentCategory() : cmd.category())) {
        return false;
    }
    if (data.getChannelSpecificDisabledCategories().computeIfAbsent(event.getChannel().getId(), c -> new ArrayList<>()).contains(cmd instanceof AliasCommand ? ((AliasCommand) cmd).parentCategory() : cmd.category())) {
        return false;
    }
    if (!data.getDisabledRoles().isEmpty() && event.getMember().getRoles().stream().anyMatch(r -> data.getDisabledRoles().contains(r.getId())) && !isAdmin(event.getMember())) {
        return false;
    }
    HashMap<String, List<String>> roleSpecificDisabledCommands = data.getRoleSpecificDisabledCommands();
    if (event.getMember().getRoles().stream().anyMatch(r -> roleSpecificDisabledCommands.computeIfAbsent(r.getId(), s -> new ArrayList<>()).contains(cmd instanceof AliasCommand ? ((AliasCommand) cmd).getOriginalName() : cmdName)) && !isAdmin(event.getMember())) {
        return false;
    }
    HashMap<String, List<Category>> roleSpecificDisabledCategories = data.getRoleSpecificDisabledCategories();
    if (event.getMember().getRoles().stream().anyMatch(r -> roleSpecificDisabledCategories.computeIfAbsent(r.getId(), s -> new ArrayList<>()).contains(cmd instanceof AliasCommand ? ((AliasCommand) cmd).parentCategory() : cmd.category())) && !isAdmin(event.getMember())) {
        return false;
    }
    // If we are in the patreon bot, deny all requests from unknown guilds.
    if (conf.isPremiumBot() && !conf.isOwner(event.getAuthor()) && !dbg.isPremium()) {
        event.getChannel().sendMessage(EmoteReference.ERROR + "Seems like you're trying to use the Patreon bot when this guild is **not** marked as premium. " + "**If you think this is an error please contact Kodehawa#3457 or poke me on #donators in the support guild**").queue();
        return false;
    }
    if (!cmd.permission().test(event.getMember())) {
        event.getChannel().sendMessage(EmoteReference.STOP + "You have no permissions to trigger this command :(").queue();
        return false;
    }
    long end = System.currentTimeMillis();
    MantaroBot.getInstance().getStatsClient().increment("commands");
    log.debug("Command invoked: {}, by {}#{} with timestamp {}", cmdName, event.getAuthor().getName(), event.getAuthor().getDiscriminator(), new Date(System.currentTimeMillis()));
    cmd.run(event, cmdName, content);
    if (cmd.category() != null && cmd.category().name() != null && !cmd.category().name().isEmpty()) {
        MantaroBot.getInstance().getStatsClient().increment("command", "name:" + cmdName);
        MantaroBot.getInstance().getStatsClient().increment("category", "name:" + cmd.category().name().toLowerCase());
        CommandStatsManager.log(cmdName);
        CategoryStatsManager.log(cmd.category().name().toLowerCase());
    }
    MantaroBot.getInstance().getStatsClient().histogram("command_process_time", (end - start));
    return true;
}
Also used : SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) java.util(java.util) Command(net.kodehawa.mantarobot.core.modules.commands.base.Command) Member(net.dv8tion.jda.core.entities.Member) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) Category(net.kodehawa.mantarobot.core.modules.commands.base.Category) CategoryStatsManager(net.kodehawa.mantarobot.commands.info.stats.manager.CategoryStatsManager) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) MantaroBot(net.kodehawa.mantarobot.MantaroBot) TreeCommand(net.kodehawa.mantarobot.core.modules.commands.TreeCommand) Slf4j(lombok.extern.slf4j.Slf4j) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) CommandPermission(net.kodehawa.mantarobot.core.modules.commands.base.CommandPermission) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Preconditions(com.google.common.base.Preconditions) CommandStatsManager(net.kodehawa.mantarobot.commands.info.stats.manager.CommandStatsManager) Config(net.kodehawa.mantarobot.data.Config) AliasCommand(net.kodehawa.mantarobot.core.modules.commands.AliasCommand) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) AliasCommand(net.kodehawa.mantarobot.core.modules.commands.AliasCommand) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) Command(net.kodehawa.mantarobot.core.modules.commands.base.Command) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) TreeCommand(net.kodehawa.mantarobot.core.modules.commands.TreeCommand) AliasCommand(net.kodehawa.mantarobot.core.modules.commands.AliasCommand)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 java.util (java.util)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Member (net.dv8tion.jda.core.entities.Member)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 MantaroBot (net.kodehawa.mantarobot.MantaroBot)1 CategoryStatsManager (net.kodehawa.mantarobot.commands.info.stats.manager.CategoryStatsManager)1 CommandStatsManager (net.kodehawa.mantarobot.commands.info.stats.manager.CommandStatsManager)1 AliasCommand (net.kodehawa.mantarobot.core.modules.commands.AliasCommand)1 SimpleTreeCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand)1 SubCommand (net.kodehawa.mantarobot.core.modules.commands.SubCommand)1 TreeCommand (net.kodehawa.mantarobot.core.modules.commands.TreeCommand)1 Category (net.kodehawa.mantarobot.core.modules.commands.base.Category)1 Command (net.kodehawa.mantarobot.core.modules.commands.base.Command)1 CommandPermission (net.kodehawa.mantarobot.core.modules.commands.base.CommandPermission)1 Config (net.kodehawa.mantarobot.data.Config)1 MantaroData (net.kodehawa.mantarobot.data.MantaroData)1 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)1 GuildData (net.kodehawa.mantarobot.db.entities.helpers.GuildData)1 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)1