Search in sources :

Example 1 with NewRateLimiter

use of net.kodehawa.mantarobot.utils.commands.NewRateLimiter 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)

Aggregations

Subscribe (com.google.common.eventbus.Subscribe)1 LinkedList (java.util.LinkedList)1 Member (net.dv8tion.jda.core.entities.Member)1 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 Character (net.kodehawa.mantarobot.commands.game.Character)1 GuessTheNumber (net.kodehawa.mantarobot.commands.game.GuessTheNumber)1 Pokemon (net.kodehawa.mantarobot.commands.game.Pokemon)1 Trivia (net.kodehawa.mantarobot.commands.game.Trivia)1 SimpleTreeCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand)1 SubCommand (net.kodehawa.mantarobot.core.modules.commands.SubCommand)1 NewRateLimiter (net.kodehawa.mantarobot.utils.commands.NewRateLimiter)1