Search in sources :

Example 1 with CharacterData

use of net.kodehawa.mantarobot.commands.anime.CharacterData in project MantaroBot by Mantaro.

the class AnimeCmds method character.

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

        @Override
        public void call(GuildMessageReceivedEvent event, String content, String[] args) {
            try {
                if (content.isEmpty()) {
                    onHelp(event);
                    return;
                }
                String url = String.format("https://anilist.co/api/character/search/%1s?access_token=%2s", URLEncoder.encode(content, "UTF-8"), authToken);
                String json = Utils.wgetResty(url, event);
                CharacterData[] character = CharacterData.fromJson(json);
                if (character.length == 1) {
                    characterData(event, character[0]);
                    return;
                }
                DiscordUtils.selectList(event, character, character1 -> String.format("**[%s %s](%s)**", character1.getLastName() == null ? "" : character1.getLastName(), character1.getFirstName(), "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. Type &cancel to cancel.", event.getAuthor().getAvatarUrl()).build(), character1 -> characterData(event, character1));
            } catch (JsonSyntaxException jsonException) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "No results found...").queue();
            } catch (NullPointerException nullException) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "We got a wrong API result for this specific search. Maybe try another one?").queue();
            } catch (Exception exception) {
                event.getChannel().sendMessage(String.format("%s**I swear I didn't d-drop your waifu, please forgive me!**\nI got ``%s`` while trying to process this command.", EmoteReference.ERROR, exception.getClass().getSimpleName())).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 : Module(net.kodehawa.mantarobot.core.modules.Module) Async(br.com.brjdevs.java.utils.async.Async) CharacterData(net.kodehawa.mantarobot.commands.anime.CharacterData) Utils(net.kodehawa.mantarobot.utils.Utils) DiscordUtils(net.kodehawa.mantarobot.utils.DiscordUtils) StringUtils(org.apache.commons.lang3.StringUtils) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JSONObject(org.json.JSONObject) CommandRegistry(net.kodehawa.mantarobot.core.CommandRegistry) okhttp3(okhttp3) Subscribe(com.google.common.eventbus.Subscribe) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) JsonSyntaxException(com.google.gson.JsonSyntaxException) Category(net.kodehawa.mantarobot.core.modules.commands.base.Category) SentryHelper(net.kodehawa.mantarobot.utils.SentryHelper) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) List(java.util.List) AnimeData(net.kodehawa.mantarobot.commands.anime.AnimeData) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) LogUtils(net.kodehawa.mantarobot.log.LogUtils) PreLoadEvent(net.kodehawa.mantarobot.core.listeners.events.PreLoadEvent) Collections(java.util.Collections) JsonSyntaxException(com.google.gson.JsonSyntaxException) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) JsonSyntaxException(com.google.gson.JsonSyntaxException) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with CharacterData

use of net.kodehawa.mantarobot.commands.anime.CharacterData in project MantaroBot by Mantaro.

the class Character method onStart.

@Override
public boolean onStart(GameLobby lobby) {
    try {
        GameStatsManager.log(name());
        characterNameL = new ArrayList<>();
        characterName = CollectionUtils.random(NAMES.get());
        String url = String.format("https://anilist.co/api/character/search/%1s?access_token=%2s", URLEncoder.encode(characterName, "UTF-8"), authToken);
        String json = Utils.wget(url, null);
        CharacterData character = CharacterData.fromJsonFirst(json);
        String imageUrl = character.getMedImageUrl();
        // Allow for replying with only the first name.
        if (characterName.contains(" ") && !characterName.contains("Sailor")) {
            characterNameL.add(characterName.split(" ")[0]);
        }
        characterNameL.add(characterName);
        sendEmbedImage(lobby.getChannel(), imageUrl, eb -> eb.setTitle("Guess the character", null).setFooter("You have 10 attempts and 60 seconds. (Type end to end the game)", null)).queue();
        return true;
    } catch (Exception e) {
        if (e instanceof JsonSyntaxException) {
            lobby.getChannel().sendMessage(EmoteReference.WARNING + "Report this in the official server please. Failed to setup game for pre-saved character: " + characterName).queue();
            return false;
        }
        lobby.getChannel().sendMessage(EmoteReference.ERROR + "Error while setting up a game.").queue();
        log.warn("Exception while setting up a game", e);
        return false;
    }
}
Also used : ImageGame(net.kodehawa.mantarobot.commands.game.core.ImageGame) Getter(lombok.Getter) GameLobby(net.kodehawa.mantarobot.commands.game.core.GameLobby) JsonSyntaxException(com.google.gson.JsonSyntaxException) InteractiveOperations(net.kodehawa.mantarobot.core.listeners.operations.InteractiveOperations) AnimeCmds(net.kodehawa.mantarobot.commands.AnimeCmds) CharacterData(net.kodehawa.mantarobot.commands.anime.CharacterData) Utils(net.kodehawa.mantarobot.utils.Utils) InteractiveOperation(net.kodehawa.mantarobot.core.listeners.operations.core.InteractiveOperation) ArrayList(java.util.ArrayList) Slf4j(lombok.extern.slf4j.Slf4j) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) DataManager(net.kodehawa.mantarobot.utils.data.DataManager) URLEncoder(java.net.URLEncoder) List(java.util.List) CollectionUtils(br.com.brjdevs.java.utils.collections.CollectionUtils) GameStatsManager(net.kodehawa.mantarobot.commands.info.stats.manager.GameStatsManager) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) SimpleFileDataManager(net.kodehawa.mantarobot.utils.data.SimpleFileDataManager) JsonSyntaxException(com.google.gson.JsonSyntaxException) CharacterData(net.kodehawa.mantarobot.commands.anime.CharacterData) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)2 URLEncoder (java.net.URLEncoder)2 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)2 CharacterData (net.kodehawa.mantarobot.commands.anime.CharacterData)2 Utils (net.kodehawa.mantarobot.utils.Utils)2 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)2 Async (br.com.brjdevs.java.utils.async.Async)1 CollectionUtils (br.com.brjdevs.java.utils.collections.CollectionUtils)1 Subscribe (com.google.common.eventbus.Subscribe)1 java.awt (java.awt)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 TimeUnit (java.util.concurrent.TimeUnit)1 Getter (lombok.Getter)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)1 AnimeCmds (net.kodehawa.mantarobot.commands.AnimeCmds)1 AnimeData (net.kodehawa.mantarobot.commands.anime.AnimeData)1