Search in sources :

Example 1 with MessageEmbed

use of net.dv8tion.jda.core.entities.MessageEmbed in project MantaroBot by Mantaro.

the class CurrencyCmds method transferItems.

@Command
public static void transferItems(CommandRegistry cr) {
    cr.register("itemtransfer", new SimpleCommand(Category.CURRENCY) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (args.length < 2) {
                onError(event);
                return;
            }
            List<User> mentionedUsers = event.getMessage().getMentionedUsers();
            if (mentionedUsers.size() == 0)
                event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention a user").queue();
            else {
                User giveTo = mentionedUsers.get(0);
                if (event.getAuthor().getId().equals(giveTo.getId())) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot transfer an item to yourself!").queue();
                    return;
                }
                Item item = Items.fromAny(args[1]).orElse(null);
                if (item == null) {
                    event.getChannel().sendMessage("There isn't an item associated with this emoji.").queue();
                } else {
                    Player player = MantaroData.db().getPlayer(event.getAuthor());
                    Player giveToPlayer = MantaroData.db().getPlayer(giveTo);
                    if (args.length == 2) {
                        if (player.getInventory().containsItem(item)) {
                            if (item.isHidden()) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot transfer this item!").queue();
                                return;
                            }
                            if (giveToPlayer.getInventory().asMap().getOrDefault(item, new ItemStack(item, 0)).getAmount() >= 5000) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "Don't do that").queue();
                                return;
                            }
                            player.getInventory().process(new ItemStack(item, -1));
                            giveToPlayer.getInventory().process(new ItemStack(item, 1));
                            event.getChannel().sendMessage(EmoteReference.OK + event.getAuthor().getAsMention() + " gave 1 " + item.getName() + " to " + giveTo.getAsMention()).queue();
                        } else {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You don't have any of these items in your inventory").queue();
                        }
                        player.saveAsync();
                        giveToPlayer.saveAsync();
                        return;
                    }
                    try {
                        int amount = Math.abs(Integer.parseInt(args[2]));
                        if (player.getInventory().containsItem(item) && player.getInventory().getAmount(item) >= amount) {
                            if (item.isHidden()) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot transfer this item!").queue();
                                return;
                            }
                            if (giveToPlayer.getInventory().asMap().getOrDefault(item, new ItemStack(item, 0)).getAmount() + amount >= 5000) {
                                event.getChannel().sendMessage(EmoteReference.ERROR + "Don't do that").queue();
                                return;
                            }
                            player.getInventory().process(new ItemStack(item, amount * -1));
                            giveToPlayer.getInventory().process(new ItemStack(item, amount));
                            event.getChannel().sendMessage(EmoteReference.OK + event.getAuthor().getAsMention() + " gave " + amount + " " + item.getName() + " to " + giveTo.getAsMention()).queue();
                        } else
                            event.getChannel().sendMessage(EmoteReference.ERROR + "You don't have enough of this item " + "to do that").queue();
                    } catch (NumberFormatException nfe) {
                        event.getChannel().sendMessage(EmoteReference.ERROR + "Invalid number provided").queue();
                    }
                    player.saveAsync();
                    giveToPlayer.saveAsync();
                }
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Transfer Items command").setDescription("**Transfers items from you to another player.**").addField("Usage", "`~>itemtransfer <@user> <item emoji> <amount (optional)>` - **Transfers the item to player x**", false).addField("Parameters", "`@user` - user to send the item to\n" + "`item emoji` - write out the emoji of the item you want to send\n" + "`amount` - optional, send a specific amount of an item to someone.", false).addField("Important", "You cannot send more items than what you already have", false).build();
        }
    });
    cr.registerAlias("itemtransfer", "transferitems");
}
Also used : Item(net.kodehawa.mantarobot.commands.currency.item.Item) Player(net.kodehawa.mantarobot.data.entities.Player) DBUser(net.kodehawa.mantarobot.data.entities.DBUser) User(net.dv8tion.jda.core.entities.User) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) ItemStack(net.kodehawa.mantarobot.commands.currency.item.ItemStack) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 2 with MessageEmbed

use of net.dv8tion.jda.core.entities.MessageEmbed in project MantaroBot by Mantaro.

the class CustomCmds method custom.

@Command
public static void custom(CommandRegistry cr) {
    String any = "[\\d\\D]*?";
    cr.register("custom", new SimpleCommand(Category.UTILS) {

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (args.length < 1) {
                onHelp(event);
                return;
            }
            String action = args[0];
            if (action.equals("list") || action.equals("ls")) {
                String filter = event.getGuild().getId() + ":";
                List<String> commands = customCommands.keySet().stream().filter(s -> s.startsWith(filter)).map(s -> s.substring(filter.length())).collect(Collectors.toList());
                EmbedBuilder builder = new EmbedBuilder().setAuthor("Commands for this guild", null, event.getGuild().getIconUrl()).setColor(event.getMember().getColor());
                builder.setDescription(commands.isEmpty() ? "There is nothing here, just dust." : forType(commands));
                event.getChannel().sendMessage(builder.build()).queue();
                return;
            }
            if (db().getGuild(event.getGuild()).getData().isCustomAdminLock() && !CommandPermission.ADMIN.test(event.getMember())) {
                event.getChannel().sendMessage("This guild only accepts custom commands from administrators.").queue();
                return;
            }
            if (action.equals("clear")) {
                if (CommandPermission.ADMIN.test(event.getMember())) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot do that, silly.").queue();
                    return;
                }
                List<CustomCommand> customCommands = db().getCustomCommands(event.getGuild());
                if (customCommands.isEmpty()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "There's no Custom Commands registered in this Guild.").queue();
                }
                int size = customCommands.size();
                customCommands.forEach(CustomCommand::deleteAsync);
                customCommands.forEach(c -> CustomCmds.customCommands.remove(c.getId()));
                event.getChannel().sendMessage(EmoteReference.PENCIL + "Cleared **" + size + " Custom Commands**!").queue();
                return;
            }
            if (args.length < 2) {
                onHelp(event);
                return;
            }
            String cmd = args[1];
            if (action.equals("make")) {
                if (!NAME_PATTERN.matcher(cmd).matches()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                    return;
                }
                List<String> responses = new ArrayList<>();
                boolean created = InteractiveOperations.create(event.getChannel(), "Custom Command Creation", 60000, OptionalInt.of(60000), e -> {
                    if (!e.getAuthor().equals(event.getAuthor()))
                        return false;
                    String c = e.getMessage().getRawContent();
                    if (!c.startsWith("&"))
                        return false;
                    c = c.substring(1);
                    if (c.startsWith("~>cancel") || c.startsWith("~>stop")) {
                        event.getChannel().sendMessage(EmoteReference.CORRECT + "Command Creation canceled.").queue();
                        return true;
                    }
                    if (c.startsWith("~>save")) {
                        String arg = c.substring(6).trim();
                        String saveTo = !arg.isEmpty() ? arg : cmd;
                        if (!NAME_PATTERN.matcher(cmd).matches()) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                            return false;
                        }
                        if (CommandProcessor.REGISTRY.commands().containsKey(saveTo) && !CommandProcessor.REGISTRY.commands().get(saveTo).equals(customCommand)) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "A command already exists with this name!").queue();
                            return false;
                        }
                        if (responses.isEmpty()) {
                            event.getChannel().sendMessage(EmoteReference.ERROR + "No responses were added. Stopping creation without saving...").queue();
                        } else {
                            CustomCommand custom = CustomCommand.of(event.getGuild().getId(), cmd, responses);
                            custom.saveAsync();
                            customCommands.put(custom.getId(), custom.getValues());
                            CommandProcessor.REGISTRY.commands().put(cmd, customCommand);
                            event.getChannel().sendMessage(EmoteReference.CORRECT + "Saved to command ``" + cmd + "``!").queue();
                            TextChannelGround.of(event).dropItemWithChance(8, 2);
                        }
                        return true;
                    }
                    responses.add(c);
                    e.getMessage().addReaction(EmoteReference.CORRECT.getUnicode()).queue();
                    return false;
                });
                if (created) {
                    event.getChannel().sendMessage(EmoteReference.PENCIL + "Started **\"Creation of Custom Command ``" + cmd + "``\"**!\nSend ``&~>stop`` to stop creation **without saving**.\nSend ``&~>save`` to stop creation an **save the new Command**. Send any text beginning with ``&`` to be added to the Command Responses.\nThis Interactive Operation ends without saving after 60 seconds of inactivity.").queue();
                } else {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "There's already an Interactive Operation happening on this channel.").queue();
                }
                return;
            }
            if (action.equals("remove") || action.equals("rm")) {
                if (!NAME_PATTERN.matcher(cmd).matches()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                    return;
                }
                CustomCommand custom = db().getCustomCommand(event.getGuild(), cmd);
                if (custom == null) {
                    event.getChannel().sendMessage(EmoteReference.ERROR2 + "There's no Custom Command ``" + cmd + "`` in this Guild.").queue();
                    return;
                }
                //delete at DB
                custom.deleteAsync();
                //reflect at local
                customCommands.remove(custom.getId());
                //clear commands if none
                if (customCommands.keySet().stream().noneMatch(s -> s.endsWith(":" + cmd)))
                    CommandProcessor.REGISTRY.commands().remove(cmd);
                event.getChannel().sendMessage(EmoteReference.PENCIL + "Removed Custom Command ``" + cmd + "``!").queue();
                return;
            }
            if (action.equals("raw")) {
                if (!NAME_PATTERN.matcher(cmd).matches()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                    return;
                }
                CustomCommand custom = db().getCustomCommand(event.getGuild(), cmd);
                if (custom == null) {
                    event.getChannel().sendMessage(EmoteReference.ERROR2 + "There's no Custom Command ``" + cmd + "`` in this Guild.").queue();
                    return;
                }
                Pair<String, Integer> pair = DiscordUtils.embedList(custom.getValues(), Object::toString);
                event.getChannel().sendMessage(baseEmbed(event, "Command ``" + cmd + "``:").setDescription(pair.getLeft()).setFooter("(Showing " + pair.getRight() + " responses of " + custom.getValues().size() + ")", null).build()).queue();
                return;
            }
            if (action.equals("import")) {
                if (!NAME_WILDCARD_PATTERN.matcher(cmd).matches()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                    return;
                }
                Map<String, Guild> mapped = MantaroBot.getInstance().getMutualGuilds(event.getAuthor()).stream().collect(Collectors.toMap(ISnowflake::getId, g -> g));
                List<Pair<Guild, CustomCommand>> filtered = MantaroData.db().getCustomCommandsByName(("*" + cmd + "*").replace("*", any)).stream().map(customCommand -> {
                    Guild guild = mapped.get(customCommand.getGuildId());
                    return guild == null ? null : Pair.of(guild, customCommand);
                }).filter(Objects::nonNull).collect(Collectors.toList());
                if (filtered.size() == 0) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "There are no custom commands matching your search query.").queue();
                    return;
                }
                DiscordUtils.selectList(event, filtered, pair -> "``" + pair.getValue().getName() + "`` - Guild: ``" + pair.getKey() + "``", s -> baseEmbed(event, "Select the Command:").setDescription(s).setFooter("(You can only select custom commands from guilds that you are a member of)", null).build(), pair -> {
                    String cmdName = pair.getValue().getName();
                    List<String> responses = pair.getValue().getValues();
                    CustomCommand custom = CustomCommand.of(event.getGuild().getId(), cmdName, responses);
                    custom.saveAsync();
                    customCommands.put(custom.getId(), custom.getValues());
                    event.getChannel().sendMessage(String.format("Imported custom command ``%s`` from guild `%s` with responses ``%s``", cmdName, pair.getKey().getName(), String.join("``, ``", responses))).queue();
                    TextChannelGround.of(event).dropItemWithChance(8, 2);
                });
                return;
            }
            if (args.length < 3) {
                onHelp(event);
                return;
            }
            String value = args[2];
            if (action.equals("rename")) {
                if (!NAME_PATTERN.matcher(cmd).matches() || !NAME_PATTERN.matcher(value).matches()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                    return;
                }
                if (CommandProcessor.REGISTRY.commands().containsKey(value) && !CommandProcessor.REGISTRY.commands().get(value).equals(customCommand)) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "A command already exists with this name!").queue();
                    return;
                }
                CustomCommand oldCustom = db().getCustomCommand(event.getGuild(), cmd);
                if (oldCustom == null) {
                    event.getChannel().sendMessage(EmoteReference.ERROR2 + "There's no Custom Command ``" + cmd + "`` in this Guild.").queue();
                    return;
                }
                CustomCommand newCustom = CustomCommand.of(event.getGuild().getId(), value, oldCustom.getValues());
                //change at DB
                oldCustom.deleteAsync();
                newCustom.saveAsync();
                //reflect at local
                customCommands.remove(oldCustom.getId());
                customCommands.put(newCustom.getId(), newCustom.getValues());
                //add mini-hack
                CommandProcessor.REGISTRY.commands().put(cmd, customCommand);
                //clear commands if none
                if (customCommands.keySet().stream().noneMatch(s -> s.endsWith(":" + cmd)))
                    CommandProcessor.REGISTRY.commands().remove(cmd);
                event.getChannel().sendMessage(EmoteReference.CORRECT + "Renamed command ``" + cmd + "`` to ``" + value + "``!").queue();
                //easter egg :D
                TextChannelGround.of(event).dropItemWithChance(8, 2);
                return;
            }
            if (action.equals("add") || action.equals("new")) {
                if (!NAME_PATTERN.matcher(cmd).matches()) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "Not allowed character.").queue();
                    return;
                }
                if (CommandProcessor.REGISTRY.commands().containsKey(cmd) && !CommandProcessor.REGISTRY.commands().get(cmd).equals(customCommand)) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "A command already exists with this name!").queue();
                    return;
                }
                CustomCommand custom = CustomCommand.of(event.getGuild().getId(), cmd, Collections.singletonList(value));
                if (action.equals("add")) {
                    CustomCommand c = db().getCustomCommand(event, cmd);
                    if (c != null)
                        custom.getValues().addAll(c.getValues());
                }
                //save at DB
                custom.saveAsync();
                //reflect at local
                customCommands.put(custom.getId(), custom.getValues());
                //add mini-hack
                CommandProcessor.REGISTRY.commands().put(cmd, customCommand);
                event.getChannel().sendMessage(EmoteReference.CORRECT + "Saved to command ``" + cmd + "``!").queue();
                //easter egg :D
                TextChannelGround.of(event).dropItemWithChance(8, 2);
                return;
            }
            onHelp(event);
        }

        @Override
        public String[] splitArgs(String content) {
            return SPLIT_PATTERN.split(content, 3);
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "CustomCommand Manager").setDescription("**Manages the Custom Commands of the Guild.**").addField("Guide", "https://github.com/Mantaro/MantaroBot/wiki/Custom-Commands", false).addField("Usage:", "`~>custom` - Shows this help\n" + "`~>custom <list|ls> [detailed]` - **List all commands. If detailed is supplied, it prints the responses of each command.**\n" + "`~>custom debug` - **Gives a Hastebin of the Raw Custom Commands Data. (OWNER-ONLY)**\n" + "`~>custom clear` - **Remove all Custom Commands from this Guild. (OWNER-ONLY)**\n" + "`~>custom add <name> <responses>` - **Add a new Command with the response provided.**\n" + "`~>custom make <name>` - **Starts a Interactive Operation to create a command with the specified name.**\n" + "`~>custom <remove|rm> <name>` - **Removes a command with an specific name.**\n" + "`~>custom import <search>` - **Imports a command from another guild you're in.**", false).build();
        }
    });
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) CommandProcessor(net.kodehawa.mantarobot.core.CommandProcessor) java.util(java.util) URL(java.net.URL) Module(net.kodehawa.mantarobot.modules.Module) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) CommandStatsManager.log(net.kodehawa.mantarobot.commands.info.CommandStatsManager.log) MantaroBot(net.kodehawa.mantarobot.MantaroBot) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Pair(org.apache.commons.lang3.tuple.Pair) ISnowflake(net.dv8tion.jda.core.entities.ISnowflake) CustomCommand(net.kodehawa.mantarobot.data.entities.CustomCommand) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) TextChannelGround(net.kodehawa.mantarobot.commands.currency.TextChannelGround) MantaroData.db(net.kodehawa.mantarobot.data.MantaroData.db) ConditionalCustoms(net.kodehawa.mantarobot.commands.custom.ConditionalCustoms) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) SPLIT_PATTERN(net.kodehawa.mantarobot.utils.StringUtils.SPLIT_PATTERN) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Mapifier.map(net.kodehawa.mantarobot.commands.custom.Mapifier.map) AbstractCommand(net.kodehawa.mantarobot.modules.commands.base.AbstractCommand) Category(net.kodehawa.mantarobot.modules.commands.base.Category) GsonDataManager(net.kodehawa.mantarobot.utils.data.GsonDataManager) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Guild(net.dv8tion.jda.core.entities.Guild) Slf4j(lombok.extern.slf4j.Slf4j) CollectionUtils.random(br.com.brjdevs.java.utils.collections.CollectionUtils.random) Mapifier.dynamicResolve(net.kodehawa.mantarobot.commands.custom.Mapifier.dynamicResolve) GuildData(net.kodehawa.mantarobot.data.entities.helpers.GuildData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) CommandPermission(net.kodehawa.mantarobot.modules.commands.CommandPermission) EmbedJSON(net.kodehawa.mantarobot.commands.custom.EmbedJSON) MantaroData(net.kodehawa.mantarobot.data.MantaroData) HelpUtils.forType(net.kodehawa.mantarobot.commands.info.HelpUtils.forType) Pattern(java.util.regex.Pattern) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) DBGuild(net.kodehawa.mantarobot.data.entities.DBGuild) Guild(net.dv8tion.jda.core.entities.Guild) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) CustomCommand(net.kodehawa.mantarobot.data.entities.CustomCommand) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) Pair(org.apache.commons.lang3.tuple.Pair) ISnowflake(net.dv8tion.jda.core.entities.ISnowflake) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) CustomCommand(net.kodehawa.mantarobot.data.entities.CustomCommand) Command(net.kodehawa.mantarobot.modules.Command) AbstractCommand(net.kodehawa.mantarobot.modules.commands.base.AbstractCommand)

Example 3 with MessageEmbed

use of net.dv8tion.jda.core.entities.MessageEmbed in project MantaroBot by Mantaro.

the class ActionCmds method meow.

@Command
public static void meow(CommandRegistry registry) {
    registry.register("meow", new SimpleCommand(Category.ACTION) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            Message receivedMessage = event.getMessage();
            if (!receivedMessage.getMentionedUsers().isEmpty()) {
                String mew = event.getMessage().getMentionedUsers().stream().map(IMentionable::getAsMention).collect(Collectors.joining(" "));
                event.getChannel().sendFile(ImageActionCmd.CACHE.getInput("http://imgur.com/yFGHvVR.gif"), "mew.gif", new MessageBuilder().append(EmoteReference.TALKING).append(String.format("%s *is meowing at %s.*", event.getAuthor().getAsMention(), mew)).build()).queue();
            } else {
                event.getChannel().sendFile(ImageActionCmd.CACHE.getInput("http://imgur.com/yFGHvVR.gif"), "mew.gif", new MessageBuilder().append(":speech_balloon: Meow.").build()).queue();
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Meow command").setDescription("**Meow either to a person or the sky**.").setColor(Color.cyan).build();
        }
    });
    registry.registerAlias("meow", "mew");
}
Also used : MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Message(net.dv8tion.jda.core.entities.Message) IMentionable(net.dv8tion.jda.core.entities.IMentionable) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 4 with MessageEmbed

use of net.dv8tion.jda.core.entities.MessageEmbed in project MantaroBot by Mantaro.

the class AnimeCmds method anime.

@Command
public static void anime(CommandRegistry cr) {
    cr.register("anime", new SimpleCommand(Category.FUN) {

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            try {
                String connection = String.format("https://anilist.co/api/anime/search/%1s?access_token=%2s", URLEncoder.encode(content, "UTF-8"), authToken);
                String json = Utils.wget(connection, event);
                AnimeData[] type = GsonDataManager.GSON_PRETTY.fromJson(json, AnimeData[].class);
                if (type.length == 1) {
                    animeData(event, type[0]);
                    return;
                }
                DiscordUtils.selectList(event, type, anime -> String.format("**[%s (%s)](%s)**", anime.getTitle_english(), anime.getTitle_japanese(), "http://anilist.co/anime/" + anime.getId()), s -> baseEmbed(event, "Type the number of the anime you want to select.").setDescription(s).setThumbnail("https://anilist.co/img/logo_al.png").setFooter("Information provided by Anilist.", event.getAuthor().getAvatarUrl()).build(), anime -> animeData(event, anime));
            } catch (Exception e) {
                if (e instanceof JsonSyntaxException) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "No results or the API query was unsuccessful").queue();
                    return;
                }
                if (e instanceof NullPointerException) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "We got a wrong API result for this specific search. Maybe try another one?").queue();
                    return;
                }
                event.getChannel().sendMessage(EmoteReference.ERROR + "**Houston, we have a problem!**\n\n > We received a ``" + e.getClass().getSimpleName() + "`` while trying to process the command. \nError: ``" + e.getMessage() + "``").queue();
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Anime command").setDescription("**Get anime info from AniList (For anime characters use ~>character).**").addField("Usage", "`~>anime <animename>` - **Retrieve information of an anime based on the name.**", false).addField("Parameters", "`animename` - **The name of the anime you are looking for. Keep queries similar to their english names!**", false).setColor(Color.PINK).build();
        }
    });
    cr.registerAlias("anime", "animu");
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) Async(br.com.brjdevs.java.utils.async.Async) CharacterData(net.kodehawa.mantarobot.commands.anime.CharacterData) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) StringUtils(org.apache.commons.lang3.StringUtils) Unirest(com.mashape.unirest.http.Unirest) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) JsonSyntaxException(com.google.gson.JsonSyntaxException) Category(net.kodehawa.mantarobot.modules.commands.base.Category) GsonDataManager(net.kodehawa.mantarobot.utils.data.GsonDataManager) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) AnimeData(net.kodehawa.mantarobot.commands.anime.AnimeData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) JsonSyntaxException(com.google.gson.JsonSyntaxException) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JsonSyntaxException(com.google.gson.JsonSyntaxException) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 5 with MessageEmbed

use of net.dv8tion.jda.core.entities.MessageEmbed in project MantaroBot by Mantaro.

the class AnimeCmds method character.

@Command
public static void character(CommandRegistry cr) {
    cr.register("character", new SimpleCommand(Category.FUN) {

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            try {
                String url = String.format("https://anilist.co/api/character/search/%1s?access_token=%2s", URLEncoder.encode(content, "UTF-8"), authToken);
                String json = Utils.wget(url, event);
                CharacterData[] character = GsonDataManager.GSON_PRETTY.fromJson(json, CharacterData[].class);
                if (character.length == 1) {
                    characterData(event, character[0]);
                    return;
                }
                DiscordUtils.selectList(event, character, character1 -> String.format("**[%s %s](%s)**", character1.name_last == null ? "" : character1.name_last, character1.name_first, "http://anilist.co/character/" + character1.getId()), s -> baseEmbed(event, "Type the number of the character you want to select.").setDescription(s).setThumbnail("https://anilist.co/img/logo_al.png").setFooter("Information provided by Anilist.", event.getAuthor().getAvatarUrl()).build(), character1 -> characterData(event, character1));
            } catch (Exception e) {
                if (e instanceof JsonSyntaxException) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "No results!").queue();
                    return;
                }
                log.warn("Problem processing data.", e);
                event.getChannel().sendMessage(EmoteReference.ERROR + "**We have a problem!**\n\n > I got ``" + e.getClass().getSimpleName() + "`` while trying to process this command. \nError: ``" + e.getMessage() + "``").queue();
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Character command").setDescription("**Get character info from AniList (For anime use `~>anime`).**").addField("Usage", "`~>character <name>` - **Retrieve information of a charactrer based on the name.**", false).addField("Parameters", "`name` - **The name of the character you are looking for. Keep queries similar to their romanji names!**", false).setColor(Color.PINK).build();
        }
    });
    cr.registerAlias("character", "char");
}
Also used : SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) Async(br.com.brjdevs.java.utils.async.Async) CharacterData(net.kodehawa.mantarobot.commands.anime.CharacterData) Utils(net.kodehawa.mantarobot.utils.Utils) Module(net.kodehawa.mantarobot.modules.Module) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) StringUtils(org.apache.commons.lang3.StringUtils) Unirest(com.mashape.unirest.http.Unirest) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) PostLoadEvent(net.kodehawa.mantarobot.modules.events.PostLoadEvent) JsonSyntaxException(com.google.gson.JsonSyntaxException) Category(net.kodehawa.mantarobot.modules.commands.base.Category) GsonDataManager(net.kodehawa.mantarobot.utils.data.GsonDataManager) Collectors(java.util.stream.Collectors) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) AnimeData(net.kodehawa.mantarobot.commands.anime.AnimeData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) JsonSyntaxException(com.google.gson.JsonSyntaxException) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JsonSyntaxException(com.google.gson.JsonSyntaxException) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Aggregations

MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)26 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)22 Command (net.kodehawa.mantarobot.modules.Command)22 SimpleCommand (net.kodehawa.mantarobot.modules.commands.SimpleCommand)22 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)12 MantaroData (net.kodehawa.mantarobot.data.MantaroData)8 CommandRegistry (net.kodehawa.mantarobot.modules.CommandRegistry)8 Module (net.kodehawa.mantarobot.modules.Module)8 Category (net.kodehawa.mantarobot.modules.commands.base.Category)8 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)8 Player (net.kodehawa.mantarobot.data.entities.Player)7 Color (java.awt.Color)6 TimeUnit (java.util.concurrent.TimeUnit)5 Slf4j (lombok.extern.slf4j.Slf4j)5 Utils (net.kodehawa.mantarobot.utils.Utils)5 Collectors (java.util.stream.Collectors)4 User (net.dv8tion.jda.core.entities.User)4 RateLimiter (net.kodehawa.mantarobot.commands.currency.RateLimiter)4 TextChannelGround (net.kodehawa.mantarobot.commands.currency.TextChannelGround)4 DBGuild (net.kodehawa.mantarobot.data.entities.DBGuild)4