Search in sources :

Example 56 with GuildMessageReceivedEvent

use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.

the class MiscCmds method iamFunction.

protected static void iamFunction(String autoroleName, GuildMessageReceivedEvent event) {
    DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
    Map<String, String> autoroles = dbGuild.getData().getAutoroles();
    if (autoroles.containsKey(autoroleName)) {
        Role role = event.getGuild().getRoleById(autoroles.get(autoroleName));
        if (role == null) {
            event.getChannel().sendMessage(EmoteReference.ERROR + "The role that this autorole corresponded to has been deleted").queue();
            // delete the non-existent autorole.
            dbGuild.getData().getAutoroles().remove(autoroleName);
            dbGuild.saveAsync();
        } else {
            if (event.getMember().getRoles().stream().filter(r1 -> r1.getId().equals(role.getId())).collect(Collectors.toList()).size() > 0) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You already have this role, silly!").queue();
                return;
            }
            try {
                event.getGuild().getController().addSingleRoleToMember(event.getMember(), role).reason("Auto-assignable roles assigner (~>iam)").queue(aVoid -> event.getChannel().sendMessage(String.format("%s%s, you've been given the **%s** role", EmoteReference.OK, event.getMember().getEffectiveName(), role.getName())).queue());
            } catch (PermissionException pex) {
                event.getChannel().sendMessage(String.format("%sI couldn't take from you **%s. Make sure that I have permission to add roles and that my role is above **%s**", EmoteReference.ERROR, role.getName(), role.getName())).queue();
            }
        }
    } else {
        event.getChannel().sendMessage(EmoteReference.ERROR + "There isn't an autorole with the name ``" + autoroleName + "``!").queue();
    }
}
Also used : Role(net.dv8tion.jda.core.entities.Role) IntStream(java.util.stream.IntStream) Poll(net.kodehawa.mantarobot.commands.interaction.polls.Poll) Module(net.kodehawa.mantarobot.core.modules.Module) SimpleTreeCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleTreeCommand) Utils(net.kodehawa.mantarobot.utils.Utils) Random(java.util.Random) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) Message(net.dv8tion.jda.core.entities.Message) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) ITreeCommand(net.kodehawa.mantarobot.core.modules.commands.base.ITreeCommand) JSONObject(org.json.JSONObject) Permission(net.dv8tion.jda.core.Permission) CommandRegistry(net.kodehawa.mantarobot.core.CommandRegistry) Map(java.util.Map) StringUtils(br.com.brjdevs.java.utils.texts.StringUtils) Subscribe(com.google.common.eventbus.Subscribe) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) SimpleFileDataManager(net.kodehawa.mantarobot.utils.data.SimpleFileDataManager) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) Role(net.dv8tion.jda.core.entities.Role) PollBuilder(net.kodehawa.mantarobot.commands.interaction.polls.PollBuilder) SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) Category(net.kodehawa.mantarobot.core.modules.commands.base.Category) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Slf4j(lombok.extern.slf4j.Slf4j) DataManager(net.kodehawa.mantarobot.utils.data.DataManager) URLEncoder(java.net.URLEncoder) List(java.util.List) CollectionUtils.random(br.com.brjdevs.java.utils.collections.CollectionUtils.random) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) Optional(java.util.Optional) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) DBGuild(net.kodehawa.mantarobot.db.entities.DBGuild)

Example 57 with GuildMessageReceivedEvent

use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent 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)

Example 58 with GuildMessageReceivedEvent

use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.

the class MiscCmds method eightBall.

@Subscribe
public void eightBall(CommandRegistry cr) {
    cr.register("8ball", new SimpleCommand(Category.MISC) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (content.isEmpty()) {
                onError(event);
                return;
            }
            String textEncoded;
            String answer;
            try {
                textEncoded = URLEncoder.encode(content, "UTF-8");
                String json = Utils.wgetResty(String.format("https://8ball.delegator.com/magic/JSON/%1s", textEncoded), event);
                answer = new JSONObject(json).getJSONObject("magic").getString("answer");
            } catch (Exception exception) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "I ran into an error while fetching 8ball results.").queue();
                return;
            }
            event.getChannel().sendMessage("\uD83D\uDCAC " + answer + ".").queue();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "8ball").setDescription("**Retrieves an answer from the almighty 8ball.**").addField("Usage", "`~>8ball <question>` - **Retrieves an answer from 8ball based on the question or sentence provided.**", false).build();
        }
    });
    cr.registerAlias("8ball", "8b");
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) JSONObject(org.json.JSONObject) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) Subscribe(com.google.common.eventbus.Subscribe)

Example 59 with GuildMessageReceivedEvent

use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.

the class MoneyCmds method gamble.

@Subscribe
public void gamble(CommandRegistry cr) {
    cr.register("gamble", new SimpleCommand(Category.CURRENCY) {

        final RateLimiter rateLimiter = new RateLimiter(TimeUnit.SECONDS, 20, true);

        SecureRandom r = new SecureRandom();

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            Player player = MantaroData.db().getPlayer(event.getMember());
            if (!handleDefaultRatelimit(rateLimiter, event.getAuthor(), event))
                return;
            if (player.getMoney() <= 0) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "You're broke. Search for some credits first!").queue();
                return;
            }
            if (player.getMoney() > GAMBLE_ABSOLUTE_MAX_MONEY) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "You have too much money! Maybe transfer or buy items? Now you can also use `~>slots`" + " for all your gambling needs! Thanks for not breaking the local bank. *(Gamble limit: " + GAMBLE_ABSOLUTE_MAX_MONEY + ")*").queue();
                return;
            }
            double multiplier;
            long i;
            int luck;
            try {
                switch(content) {
                    case "all":
                    case "everything":
                        i = player.getMoney();
                        multiplier = 1.3d + (r.nextInt(1450) / 1000d);
                        luck = 21 + (int) (multiplier * 13) + r.nextInt(18);
                        break;
                    case "half":
                        i = player.getMoney() == 1 ? 1 : player.getMoney() / 2;
                        multiplier = 1.2d + (r.nextInt(1350) / 1000d);
                        luck = 19 + (int) (multiplier * 13) + r.nextInt(18);
                        break;
                    case "quarter":
                        i = player.getMoney() == 1 ? 1 : player.getMoney() / 4;
                        multiplier = 1.1d + (r.nextInt(1250) / 1000d);
                        luck = 18 + (int) (multiplier * 12) + r.nextInt(18);
                        break;
                    default:
                        i = content.endsWith("%") ? Math.round(PERCENT_FORMAT.get().parse(content).doubleValue() * player.getMoney()) : Long.parseLong(content);
                        if (i > player.getMoney() || i < 0)
                            throw new UnsupportedOperationException();
                        multiplier = 1.1d + (i / player.getMoney() * r.nextInt(1300) / 1000d);
                        luck = 17 + (int) (multiplier * 13) + r.nextInt(12);
                        break;
                }
            } catch (NumberFormatException e) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a valid number less than or equal to your current balance or" + " `all` to gamble all your credits.").queue();
                return;
            } catch (UnsupportedOperationException e) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a value within your balance.").queue();
                return;
            } catch (ParseException e) {
                event.getChannel().sendMessage(EmoteReference.ERROR2 + "Please type a valid percentage value.").queue();
                return;
            }
            User user = event.getAuthor();
            long gains = (long) (i * multiplier);
            gains = Math.round(gains * 0.45);
            final int finalLuck = luck;
            final long finalGains = gains;
            if (i >= Integer.MAX_VALUE / 4) {
                player.setLocked(true);
                player.save();
                event.getChannel().sendMessage(String.format("%sYou're about to bet **%d** credits (which seems to be a lot). " + "Are you sure? Type **yes** to continue and **no** otherwise.", EmoteReference.WARNING, i)).queue();
                InteractiveOperations.create(event.getChannel(), 30, new InteractiveOperation() {

                    @Override
                    public int run(GuildMessageReceivedEvent e) {
                        if (e.getAuthor().getId().equals(user.getId())) {
                            if (e.getMessage().getContentRaw().equalsIgnoreCase("yes")) {
                                proceedGamble(event, player, finalLuck, random, i, finalGains);
                                return COMPLETED;
                            } else if (e.getMessage().getContentRaw().equalsIgnoreCase("no")) {
                                e.getChannel().sendMessage(EmoteReference.ZAP + "Cancelled bet.").queue();
                                player.setLocked(false);
                                player.saveAsync();
                                return COMPLETED;
                            }
                        }
                        return IGNORED;
                    }

                    @Override
                    public void onExpire() {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Time to complete the operation has ran out.").queue();
                        player.setLocked(false);
                        player.saveAsync();
                    }
                });
                return;
            }
            proceedGamble(event, player, luck, random, i, gains);
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Gamble command").setDescription("Gambles your money").addField("Usage", "~>gamble <all/half/quarter> or ~>gamble <amount>\n" + "You can also use percentages now, for example `~>gamble 35%`", false).build();
        }
    });
}
Also used : InteractiveOperation(net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation) Player(net.kodehawa.mantarobot.db.entities.Player) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SecureRandom(java.security.SecureRandom) RateLimiter(net.kodehawa.mantarobot.utils.commands.RateLimiter) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) ParseException(java.text.ParseException) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Subscribe(com.google.common.eventbus.Subscribe)

Example 60 with GuildMessageReceivedEvent

use of net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent in project MantaroBot by Mantaro.

the class MoneyCmds method balance.

@Subscribe
public void balance(CommandRegistry cr) {
    cr.register("balance", new SimpleCommand(Category.CURRENCY) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            User user = event.getAuthor();
            boolean isExternal = false;
            List<Member> found = FinderUtil.findMembers(content, event.getGuild());
            if (found.isEmpty() && !content.isEmpty()) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "Your search yielded no results :(").queue();
                return;
            }
            if (found.size() > 1 && !content.isEmpty()) {
                event.getChannel().sendMessage(String.format("%sToo many users found, maybe refine your search? (ex. use name#discriminator)\n" + "**Users found:** %s", EmoteReference.THINKING, found.stream().map(m -> m.getUser().getName() + "#" + m.getUser().getDiscriminator()).collect(Collectors.joining(", ")))).queue();
                return;
            }
            if (found.size() == 1) {
                user = found.get(0).getUser();
                isExternal = true;
            }
            long balance = MantaroData.db().getPlayer(user).getMoney();
            event.getChannel().sendMessage(EmoteReference.DIAMOND + (isExternal ? user.getName() + "'s balance is: **$" : "Your balance is: **$") + balance + "**").queue();
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return baseEmbed(event, "Balance command").setDescription("**Shows your current balance or another person's balance.**").build();
        }
    });
    cr.registerAlias("balance", "credits");
    cr.registerAlias("balance", "bal");
}
Also used : Items(net.kodehawa.mantarobot.commands.currency.item.Items) Badge(net.kodehawa.mantarobot.commands.currency.profile.Badge) Module(net.kodehawa.mantarobot.core.modules.Module) java.util(java.util) Command(net.kodehawa.mantarobot.core.modules.commands.base.Command) Member(net.dv8tion.jda.core.entities.Member) PlayerData(net.kodehawa.mantarobot.db.entities.helpers.PlayerData) Utils(net.kodehawa.mantarobot.utils.Utils) RateLimiter(net.kodehawa.mantarobot.utils.commands.RateLimiter) NumberFormat(java.text.NumberFormat) InteractiveOperation(net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation) MantaroBot(net.kodehawa.mantarobot.MantaroBot) SecureRandom(java.security.SecureRandom) RethinkDB.r(com.rethinkdb.RethinkDB.r) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Cursor(com.rethinkdb.net.Cursor) ITreeCommand(net.kodehawa.mantarobot.core.modules.commands.base.ITreeCommand) Pair(org.apache.commons.lang3.tuple.Pair) CommandRegistry(net.kodehawa.mantarobot.core.CommandRegistry) Utils.handleDefaultRatelimit(net.kodehawa.mantarobot.utils.Utils.handleDefaultRatelimit) OptArgs(com.rethinkdb.model.OptArgs) StringUtils(br.com.brjdevs.java.utils.texts.StringUtils) Subscribe(com.google.common.eventbus.Subscribe) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) ParseException(java.text.ParseException) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) ItemStack(net.kodehawa.mantarobot.commands.currency.item.ItemStack) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) Connection(com.rethinkdb.net.Connection) Player(net.kodehawa.mantarobot.db.entities.Player) SubCommand(net.kodehawa.mantarobot.core.modules.commands.SubCommand) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) Month(java.time.Month) Category(net.kodehawa.mantarobot.core.modules.commands.base.Category) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) TreeCommand(net.kodehawa.mantarobot.core.modules.commands.TreeCommand) TimeUnit(java.util.concurrent.TimeUnit) User(net.dv8tion.jda.core.entities.User) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) LocalDate(java.time.LocalDate) MantaroData(net.kodehawa.mantarobot.data.MantaroData) FinderUtil(com.jagrosh.jdautilities.utils.FinderUtil) OrderBy(com.rethinkdb.gen.ast.OrderBy) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Subscribe(com.google.common.eventbus.Subscribe)

Aggregations

GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)68 Subscribe (com.google.common.eventbus.Subscribe)50 SimpleCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleCommand)48 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)42 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)36 MantaroData (net.kodehawa.mantarobot.data.MantaroData)34 List (java.util.List)28 Category (net.kodehawa.mantarobot.core.modules.commands.base.Category)27 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)26 Utils (net.kodehawa.mantarobot.utils.Utils)26 TimeUnit (java.util.concurrent.TimeUnit)25 Collectors (java.util.stream.Collectors)25 CommandRegistry (net.kodehawa.mantarobot.core.CommandRegistry)25 Module (net.kodehawa.mantarobot.core.modules.Module)25 MantaroBot (net.kodehawa.mantarobot.MantaroBot)19 SubCommand (net.kodehawa.mantarobot.core.modules.commands.SubCommand)18 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)18 RateLimiter (net.kodehawa.mantarobot.utils.commands.RateLimiter)18 Slf4j (lombok.extern.slf4j.Slf4j)17 Player (net.kodehawa.mantarobot.db.entities.Player)17