Search in sources :

Example 6 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class OptsCmd method register.

@Subscribe
public void register(CommandRegistry registry) {
    registry.register("opts", optsCmd = new SimpleCommand(Category.MODERATION, CommandPermission.ADMIN) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (args.length == 0) {
                OptsCmd.onHelp(event);
                return;
            }
            if (args.length == 1 && args[0].equalsIgnoreCase("list") || args[0].equalsIgnoreCase("ls")) {
                StringBuilder builder = new StringBuilder();
                for (String s : Option.getAvaliableOptions()) {
                    builder.append(s).append("\n");
                }
                List<String> m = DiscordUtils.divideString(builder);
                List<String> messages = new LinkedList<>();
                boolean hasReactionPerms = event.getGuild().getSelfMember().hasPermission(event.getChannel(), Permission.MESSAGE_ADD_REACTION);
                for (String s1 : m) {
                    messages.add("**Mantaro's Options List**\n" + (hasReactionPerms ? "Use the arrow reactions to change pages. " : "Use &page >> and &page << to change pages and &cancel to end") + "*All options must be prefixed with `~>opts` when running them*\n" + String.format("```prolog\n%s```", s1));
                }
                if (hasReactionPerms) {
                    DiscordUtils.list(event, 45, false, messages);
                } else {
                    DiscordUtils.listText(event, 45, false, messages);
                }
                return;
            }
            if (args.length < 2) {
                event.getChannel().sendMessage(help(event)).queue();
                return;
            }
            StringBuilder name = new StringBuilder();
            if (args[0].equalsIgnoreCase("help")) {
                for (int i = 1; i < args.length; i++) {
                    String s = args[i];
                    if (name.length() > 0)
                        name.append(":");
                    name.append(s);
                    Option option = Option.getOptionMap().get(name.toString());
                    if (option != null) {
                        try {
                            EmbedBuilder builder = new EmbedBuilder().setAuthor(option.getOptionName(), null, event.getAuthor().getEffectiveAvatarUrl()).setDescription(option.getDescription()).setThumbnail("https://cdn.pixabay.com/photo/2012/04/14/16/26/question-34499_960_720.png").addField("Type", option.getType().toString(), false);
                            event.getChannel().sendMessage(builder.build()).queue();
                        } catch (IndexOutOfBoundsException ignored) {
                        }
                        return;
                    }
                }
                event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid option help name.").queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
                return;
            }
            for (int i = 0; i < args.length; i++) {
                String s = args[i];
                if (name.length() > 0)
                    name.append(":");
                name.append(s);
                Option option = Option.getOptionMap().get(name.toString());
                if (option != null) {
                    BiConsumer<GuildMessageReceivedEvent, String[]> callable = Option.getOptionMap().get(name.toString()).getEventConsumer();
                    try {
                        String[] a;
                        if (++i < args.length)
                            a = Arrays.copyOfRange(args, i, args.length);
                        else
                            a = new String[0];
                        callable.accept(event, a);
                        Player p = MantaroData.db().getPlayer(event.getAuthor());
                        if (p.getData().addBadgeIfAbsent(Badge.DID_THIS_WORK)) {
                            p.saveAsync();
                        }
                    } catch (IndexOutOfBoundsException ignored) {
                    }
                    return;
                }
            }
            event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid option or arguments.").queue(message -> message.delete().queueAfter(10, TimeUnit.SECONDS));
            event.getChannel().sendMessage(help(event)).queue();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Options and Configurations Command").setDescription("**This command allows you to change Mantaro settings for this server.**\n" + "All values set are local rather than global, meaning that they will only effect this server.").addField("Usage", "The command is so big that we moved the description to the wiki. [Click here](https://github.com/Mantaro/MantaroBot/wiki/Configuration) to go to the Wiki Article.", false).build();
        }
    }).addOption("check:data", new Option("Data check.", "Checks the data values you have set on this server. **THIS IS NOT USER-FRIENDLY**", OptionType.GENERAL).setAction(event -> {
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData guildData = dbGuild.getData();
        // Map as follows: name, value
        Map<String, Object> fieldMap = mapObjects(guildData);
        if (fieldMap == null) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "Cannot retrieve values. Weird thing...").queue();
            return;
        }
        StringBuilder show = new StringBuilder();
        show.append("Options set for server **").append(event.getGuild().getName()).append("**\n\n");
        AtomicInteger ai = new AtomicInteger();
        for (Entry e : fieldMap.entrySet()) {
            if (e.getKey().equals("localPlayerExperience")) {
                continue;
            }
            show.append(ai.incrementAndGet()).append(".- `").append(e.getKey()).append("`");
            if (e.getValue() == null) {
                show.append(" **is not set to anything.").append("**\n");
            } else {
                show.append(" is set to: **").append(e.getValue()).append("**\n");
            }
        }
        List<String> toSend = DiscordUtils.divideString(1600, show);
        toSend.forEach(message -> event.getChannel().sendMessage(message).queue());
    }).setShortDescription("Checks the data values you have set on this server.")).addOption("reset:all", new Option("Options reset.", "Resets all options set on this server.", OptionType.GENERAL).setAction(event -> {
        // Temporary stuff.
        DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
        GuildData temp = MantaroData.db().getGuild(event.getGuild()).getData();
        // The persistent data we wish to maintain.
        String premiumKey = temp.getPremiumKey();
        long quoteLastId = temp.getQuoteLastId();
        long ranPolls = temp.getQuoteLastId();
        String gameTimeoutExpectedAt = temp.getGameTimeoutExpectedAt();
        long cases = temp.getCases();
        // Assign everything all over again
        DBGuild newDbGuild = DBGuild.of(dbGuild.getId(), dbGuild.getPremiumUntil());
        GuildData newTmp = newDbGuild.getData();
        newTmp.setGameTimeoutExpectedAt(gameTimeoutExpectedAt);
        newTmp.setRanPolls(ranPolls);
        newTmp.setCases(cases);
        newTmp.setPremiumKey(premiumKey);
        newTmp.setQuoteLastId(quoteLastId);
        // weee
        newDbGuild.saveAsync();
        event.getChannel().sendMessage(EmoteReference.CORRECT + "Correctly reset your options!").queue();
    }));
}
Also used : Badge(net.kodehawa.mantarobot.commands.currency.profile.Badge) Module(net.kodehawa.mantarobot.core.modules.Module) Arrays(java.util.Arrays) Command(net.kodehawa.mantarobot.core.modules.commands.base.Command) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Permission(net.dv8tion.jda.core.Permission) CommandRegistry(net.kodehawa.mantarobot.core.CommandRegistry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Subscribe(com.google.common.eventbus.Subscribe) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) OptionType(net.kodehawa.mantarobot.options.core.OptionType) LinkedList(java.util.LinkedList) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) Player(net.kodehawa.mantarobot.db.entities.Player) Utils.mapObjects(net.kodehawa.mantarobot.utils.Utils.mapObjects) Category(net.kodehawa.mantarobot.core.modules.commands.base.Category) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) Option(net.kodehawa.mantarobot.options.core.Option) List(java.util.List) CommandPermission(net.kodehawa.mantarobot.core.modules.commands.base.CommandPermission) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) Entry(java.util.Map.Entry) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Player(net.kodehawa.mantarobot.db.entities.Player) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) LinkedList(java.util.LinkedList) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Entry(java.util.Map.Entry) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Option(net.kodehawa.mantarobot.options.core.Option) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Subscribe(com.google.common.eventbus.Subscribe)

Example 7 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class UtilsCmds method dateGMT.

protected static String dateGMT(Guild guild, String tz) {
    DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    Date date = new Date();
    DBGuild dbGuild = MantaroData.db().getGuild(guild.getId());
    GuildData guildData = dbGuild.getData();
    if (guildData.getTimeDisplay() == 1) {
        format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
    }
    format.setTimeZone(TimeZone.getTimeZone(tz));
    return format.format(date);
}
Also used : GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 8 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class Poll method startPoll.

public void startPoll() {
    try {
        if (!isCompliant) {
            getChannel().sendMessage(EmoteReference.WARNING + "This poll cannot build. " + "**Remember that the options must be a maximum of 9 and a minimum of 2 and the timeout must be a maximum of 45m and a minimum of 30s.**\n" + "Options are separated with a comma, for example `1,2,3`. For spaced stuff use quotation marks at the start and end of the sentence.").queue();
            getRunningPolls().remove(getChannel().getId());
            return;
        }
        if (isPollAlreadyRunning(getChannel())) {
            getChannel().sendMessage(EmoteReference.WARNING + "There seems to be another poll running here...").queue();
            return;
        }
        if (!getGuild().getSelfMember().hasPermission(getChannel(), Permission.MESSAGE_ADD_REACTION)) {
            getChannel().sendMessage(EmoteReference.ERROR + "Seems like I cannot add reactions here...").queue();
            getRunningPolls().remove(getChannel().getId());
            return;
        }
        DBGuild dbGuild = MantaroData.db().getGuild(getGuild());
        GuildData data = dbGuild.getData();
        AtomicInteger at = new AtomicInteger();
        data.setRanPolls(data.getRanPolls() + 1L);
        dbGuild.saveAsync();
        String toShow = Stream.of(options).map(opt -> String.format("#%01d.- %s", at.incrementAndGet(), opt)).collect(Collectors.joining("\n"));
        if (toShow.length() > 1014) {
            toShow = "This was too long to show, so I pasted it: " + Utils.paste(toShow);
        }
        User author = MantaroBot.getInstance().getUserById(owner);
        EmbedBuilder builder = new EmbedBuilder().setAuthor(String.format("Poll #%1d created by %s", data.getRanPolls(), author.getName()), null, author.getAvatarUrl()).setDescription("**Poll started. React to the number to vote.**\n*" + name + "*\n" + "Type &cancelpoll to cancel a running poll.").addField("Options", "```md\n" + toShow + "```", false).setColor(Color.CYAN).setThumbnail("https://cdn.pixabay.com/photo/2012/04/14/16/26/question-34499_960_720.png").setFooter("You have " + Utils.getHumanizedTime(timeout) + " to vote.", author.getAvatarUrl());
        getChannel().sendMessage(builder.build()).queue(this::createPoll);
        InteractiveOperations.createOverriding(getChannel(), timeout, e -> {
            if (e.getAuthor().getId().equals(owner)) {
                if (e.getMessage().getContentRaw().equalsIgnoreCase("&cancelpoll")) {
                    runningPoll.cancel(true);
                    return Operation.COMPLETED;
                }
            }
            return Operation.IGNORED;
        });
        runningPolls.put(getChannel().getId(), this);
    } catch (Exception e) {
        getChannel().sendMessage(EmoteReference.ERROR + "An unknown error has occurred while setting up a poll. Maybe try again?").queue();
    }
}
Also used : MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Utils(net.kodehawa.mantarobot.utils.Utils) HashMap(java.util.HashMap) Message(net.dv8tion.jda.core.entities.Message) Lobby(net.kodehawa.mantarobot.commands.interaction.Lobby) ArrayList(java.util.ArrayList) MantaroBot(net.kodehawa.mantarobot.MantaroBot) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Future(java.util.concurrent.Future) Permission(net.dv8tion.jda.core.Permission) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Collectors(java.util.stream.Collectors) ReactionOperations(net.kodehawa.mantarobot.core.listeners.operations.ReactionOperations) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) Stream(java.util.stream.Stream) User(net.dv8tion.jda.core.entities.User) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) ReactionOperation(net.kodehawa.mantarobot.core.listeners.operations.core.ReactionOperation) Operation(net.kodehawa.mantarobot.core.listeners.operations.core.Operation) GuildData(net.kodehawa.mantarobot.db.entities.helpers.GuildData) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) User(net.dv8tion.jda.core.entities.User) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 9 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class ModLog method logUnban.

public static void logUnban(Member author, String target, String reason) {
    DBGuild guildDB = MantaroData.db().getGuild(author.getGuild());
    EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.addField("Responsible Moderator", author.getEffectiveName(), true);
    embedBuilder.addField("Member ID", target, true);
    embedBuilder.addField("Reason", reason, false);
    embedBuilder.setAuthor("Unban", null, author.getUser().getEffectiveAvatarUrl());
    if (guildDB.getData().getGuildLogChannel() != null) {
        MantaroBot.getInstance().getTextChannelById(guildDB.getData().getGuildLogChannel()).sendMessage(embedBuilder.build()).queue();
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild)

Example 10 with DBGuild

use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.

the class ModLog method log.

public static void log(Member author, User target, String reason, ModAction action, long caseN, String... time) {
    DBGuild guildDB = db.getGuild(author.getGuild());
    Player player = db.getPlayer(author);
    PlayerData playerData = player.getData();
    EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.addField("Responsible Moderator", author.getEffectiveName(), true);
    if (target != null)
        embedBuilder.addField("Member", target.getName(), true);
    embedBuilder.addField("Reason", reason, false);
    if (target != null) {
        embedBuilder.setThumbnail(target.getEffectiveAvatarUrl());
    } else {
        embedBuilder.setThumbnail(author.getUser().getEffectiveAvatarUrl());
    }
    switch(action) {
        case BAN:
            embedBuilder.setAuthor("Ban | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl());
            break;
        case TEMP_BAN:
            embedBuilder.setAuthor("Temp Ban | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl()).addField("Time", time[0], true);
            break;
        case KICK:
            embedBuilder.setAuthor("Kick | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl());
            break;
        case MUTE:
            embedBuilder.setAuthor("Mute | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl());
            break;
        case UNMUTE:
            embedBuilder.setAuthor("Un-mute | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl());
            break;
        case PRUNE:
            embedBuilder.setAuthor("Prune | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl());
            break;
        case WARN:
            embedBuilder.setAuthor("Warn | Case #" + caseN, null, author.getUser().getEffectiveAvatarUrl());
            break;
    }
    if (!playerData.hasBadge(Badge.POWER_USER)) {
        playerData.addBadgeIfAbsent(Badge.POWER_USER);
        player.saveAsync();
    }
    if (guildDB.getData().getGuildLogChannel() != null) {
        if (MantaroBot.getInstance().getTextChannelById(guildDB.getData().getGuildLogChannel()) != null) {
            MantaroBot.getInstance().getTextChannelById(guildDB.getData().getGuildLogChannel()).sendMessage(embedBuilder.build()).queue();
        }
    }
}
Also used : Player(net.kodehawa.mantarobot.db.entities.Player) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData)

Aggregations

DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)30 MantaroData (net.kodehawa.mantarobot.data.MantaroData)18 GuildData (net.kodehawa.mantarobot.db.entities.helpers.GuildData)18 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)18 Utils (net.kodehawa.mantarobot.utils.Utils)15 Subscribe (com.google.common.eventbus.Subscribe)14 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)14 List (java.util.List)12 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)11 SimpleCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleCommand)11 Slf4j (lombok.extern.slf4j.Slf4j)10 Category (net.kodehawa.mantarobot.core.modules.commands.base.Category)10 Permission (net.dv8tion.jda.core.Permission)9 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)9 DiscordUtils (net.kodehawa.mantarobot.utils.DiscordUtils)9 Collectors (java.util.stream.Collectors)8 MantaroBot (net.kodehawa.mantarobot.MantaroBot)8 CommandRegistry (net.kodehawa.mantarobot.core.CommandRegistry)8 Module (net.kodehawa.mantarobot.core.modules.Module)8 TimeUnit (java.util.concurrent.TimeUnit)7