Search in sources :

Example 1 with SimpleTreeCommand

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

the class GameCmds method game.

@Subscribe
public void game(CommandRegistry cr) {
    final NewRateLimiter rateLimiter = new NewRateLimiter(Executors.newSingleThreadScheduledExecutor(), 4, 6, TimeUnit.SECONDS, 450, true) {

        @Override
        protected void onSpamDetected(String key, int times) {
            log.warn("[Game] Spam detected for {} ({} times)!", key, times);
        }
    };
    SimpleTreeCommand gameCommand = (SimpleTreeCommand) cr.register("game", new SimpleTreeCommand(Category.GAMES) {

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Guessing games.").addField("Games", "`~>game character` - **Starts an instance of Guess the character (anime)**.\n" + "`~>game pokemon` - **Starts an instance of who's that pokemon?**\n" + "`~>game number` - **Starts an instance of Guess The Number**`\n" + "`~>game lobby` - **Starts a chunk of different games, for example `~>game lobby pokemon, trivia` will start pokemon and then trivia.**\n" + "`~>game multiple` - **Starts multiple instances of one game, for example `~>game multiple trivia 5` will start trivia 5 times.**\n" + "`~>game wins` - **Shows how many times you've won in games**", false).addField("Considerations", "The pokemon guessing game has around *900 different pokemon* to guess, " + "where the anime guessing game has around 60. The number in the number guessing game is a random number between 0 and 150.\n" + "To start multiple trivia sessions please use `~>game trivia multiple`, not `~>trivia multiple`", false).build();
        }
    }.addSubCommand("character", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            startGame(new Character(), event);
        }
    }).addSubCommand("pokemon", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            startGame(new Pokemon(), event);
        }
    }).addSubCommand("number", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            startGame(new GuessTheNumber(), event);
        }
    }));
    gameCommand.setPredicate(event -> Utils.handleDefaultNewRatelimit(rateLimiter, event.getAuthor(), event));
    gameCommand.createSubCommandAlias("pokemon", "pokémon");
    gameCommand.createSubCommandAlias("number", "guessthatnumber");
    gameCommand.addSubCommand("wins", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            Member member = Utils.findMember(event, event.getMember(), content);
            if (member == null)
                return;
            event.getChannel().sendMessage(EmoteReference.POPPER + member.getEffectiveName() + " has won " + MantaroData.db().getPlayer(member).getData().getGamesWon() + " games").queue();
        }
    });
    gameCommand.addSubCommand("lobby", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            if (content.isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You didn't specify anything to play!").queue();
                return;
            }
            // Stripe all mentions from this.
            String[] split = mentionPattern.matcher(content).replaceAll("").split(", ");
            if (split.length < 1 || split.length == 1) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify two games at least!").queue();
                return;
            }
            LinkedList<Game> gameList = new LinkedList<>();
            for (String s : split) {
                switch(s.replace(" ", "")) {
                    case "pokemon":
                        gameList.add(new Pokemon());
                        break;
                    case "trivia":
                        gameList.add(new Trivia(null));
                        break;
                    case "number":
                        gameList.add(new GuessTheNumber());
                        break;
                    case "character":
                        gameList.add(new Character());
                        break;
                }
            }
            if (gameList.isEmpty() || gameList.size() == 1) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify two games at least (Valid games: character, pokemon, number, trivia)!").queue();
                return;
            }
            startMultipleGames(gameList, event);
        }
    });
    gameCommand.addSubCommand("multiple", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            String[] values = SPLIT_PATTERN.split(content, 2);
            if (values.length < 2) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify the game and the number of times to run it").queue();
                return;
            }
            int number;
            try {
                number = Integer.parseInt(values[1]);
            } catch (Exception e) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid number of times!").queue();
                return;
            }
            if (number > 10) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You can only start a maximum of 10 games of the same type at a time!").queue();
                return;
            }
            LinkedList<Game> gameList = new LinkedList<>();
            for (int i = 0; i < number; i++) {
                switch(values[0].replace(" ", "")) {
                    case "pokemon":
                        gameList.add(new Pokemon());
                        break;
                    case "trivia":
                        gameList.add(new Trivia(null));
                        break;
                    case "number":
                        gameList.add(new GuessTheNumber());
                        break;
                    case "character":
                        gameList.add(new Character());
                        break;
                }
            }
            if (gameList.isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to specify a valid game! (Valid games: character, pokemon, number, trivia)").queue();
                return;
            }
            startMultipleGames(gameList, event);
        }
    });
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) Character(net.kodehawa.mantarobot.commands.game.Character) Trivia(net.kodehawa.mantarobot.commands.game.Trivia) LinkedList(java.util.LinkedList) GuessTheNumber(net.kodehawa.mantarobot.commands.game.GuessTheNumber) NewRateLimiter(net.kodehawa.mantarobot.utils.commands.NewRateLimiter) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) Pokemon(net.kodehawa.mantarobot.commands.game.Pokemon) Member(net.dv8tion.jda.core.entities.Member) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with SimpleTreeCommand

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

the class OsuStatsCmd method osustats.

@Subscribe
public void osustats(CommandRegistry cr) {
    ITreeCommand osuCommand = (SimpleTreeCommand) cr.register("osustats", new SimpleTreeCommand(Category.GAMES) {

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "osu! command").setDescription("**Retrieves information from osu! (Players and scores)**.").addField("Usage", "`~>osu best <player>` - **Retrieves best scores of the user specified in the specified game mode**.\n" + "`~>osu recent <player>` - **Retrieves recent scores of the user specified in the specified game mode.**\n" + "`~>osu user <player>` - **Retrieves information about a osu! player**.\n", false).addField("Parameters", "`player` - **The osu! player to look info for.**", false).build();
        }
    });
    osuCommand.addSubCommand("best", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Retrieving information from osu! server...").queue(sentMessage -> {
                Future<String> task = pool.submit(() -> best(content));
                try {
                    sentMessage.editMessage(task.get(16, TimeUnit.SECONDS)).queue();
                } catch (Exception e) {
                    if (e instanceof TimeoutException) {
                        task.cancel(true);
                        sentMessage.editMessage(EmoteReference.ERROR + "The osu! api seems to be taking a nap. Maybe try again later?").queue();
                    } else {
                        SentryHelper.captureException("Error retrieving results from osu!API", e, OsuStatsCmd.class);
                    }
                }
            });
        }
    });
    osuCommand.addSubCommand("recent", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            event.getChannel().sendMessage(EmoteReference.STOPWATCH + "Retrieving information from server...").queue(sentMessage -> {
                Future<String> task = pool.submit(() -> recent(content));
                try {
                    sentMessage.editMessage(task.get(16, TimeUnit.SECONDS)).queue();
                } catch (Exception e) {
                    if (e instanceof TimeoutException) {
                        task.cancel(true);
                        sentMessage.editMessage(EmoteReference.ERROR + "The osu! api seems to be taking a nap. Maybe try again later?").queue();
                    } else
                        log.warn("Exception thrown while fetching data", e);
                }
            });
        }
    });
    osuCommand.addSubCommand("user", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            event.getChannel().sendMessage(user(content)).queue();
        }
    });
    cr.registerAlias("osustats", "osu");
}
Also used : Module(net.kodehawa.mantarobot.core.modules.Module) SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) java.util.concurrent(java.util.concurrent) Category(net.kodehawa.mantarobot.core.modules.commands.base.Category) DecimalFormat(java.text.DecimalFormat) SentryHelper(net.kodehawa.mantarobot.utils.SentryHelper) HashMap(java.util.HashMap) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MantaroBot(net.kodehawa.mantarobot.MantaroBot) java.awt(java.awt) Slf4j(lombok.extern.slf4j.Slf4j) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONException(org.json.JSONException) List(java.util.List) ITreeCommand(net.kodehawa.mantarobot.core.modules.commands.base.ITreeCommand) CommandRegistry(net.kodehawa.mantarobot.core.CommandRegistry) com.osu.api.ciyfhx(com.osu.api.ciyfhx) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) Map(java.util.Map) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Subscribe(com.google.common.eventbus.Subscribe) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) OsuMod(net.kodehawa.mantarobot.commands.osu.OsuMod) SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) ITreeCommand(net.kodehawa.mantarobot.core.modules.commands.base.ITreeCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONException(org.json.JSONException) Subscribe(com.google.common.eventbus.Subscribe)

Example 3 with SimpleTreeCommand

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

the class MiscCmds method misc.

@Subscribe
public void misc(CommandRegistry cr) {
    ITreeCommand miscCommand = (ITreeCommand) cr.register("misc", new SimpleTreeCommand(Category.MISC) {

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Misc Commands").setDescription("**Miscellaneous funny/useful commands.**").addField("Usage", "`~>misc reverse <sentence>` - **Reverses any given sentence.**\n" + "`~>misc rndcolor` - **Gives you a random hex color.**\n", false).addField("Parameter Explanation", "`sentence` - **A sentence to reverse.**\n" + "`@user` - **A user to mention.**", false).build();
        }
    }.addSubCommand("reverse", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            if (content.isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You didn't provide any message to reverse!").queue();
                return;
            }
            new MessageBuilder().append(new StringBuilder(content).reverse().toString()).stripMentions(event.getGuild(), Message.MentionType.EVERYONE, Message.MentionType.HERE).sendTo(event.getChannel()).queue();
        }
    }).addSubCommand("rndcolor", new SubCommand() {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content) {
            event.getChannel().sendMessage(String.format(EmoteReference.TALKING + "The random color is %s", randomColor())).queue();
        }
    }));
    miscCommand.createSubCommandAlias("rndcolor", "randomcolor");
}
Also used : SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) ITreeCommand(net.kodehawa.mantarobot.core.modules.commands.base.ITreeCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Subscribe(com.google.common.eventbus.Subscribe)

Aggregations

Subscribe (com.google.common.eventbus.Subscribe)3 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)3 SimpleTreeCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand)3 SubCommand (net.kodehawa.mantarobot.core.modules.commands.SubCommand)3 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)2 ITreeCommand (net.kodehawa.mantarobot.core.modules.commands.base.ITreeCommand)2 com.osu.api.ciyfhx (com.osu.api.ciyfhx)1 java.awt (java.awt)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 java.util.concurrent (java.util.concurrent)1 Slf4j (lombok.extern.slf4j.Slf4j)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)1 Member (net.dv8tion.jda.core.entities.Member)1 MantaroBot (net.kodehawa.mantarobot.MantaroBot)1 Character (net.kodehawa.mantarobot.commands.game.Character)1