Search in sources :

Example 11 with LoadingMessage

use of me.shadorc.shadbot.utils.object.LoadingMessage in project Shadbot by Shadorc.

the class SuicideGirlsCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    if (!context.getChannel().isNSFW()) {
        BotUtils.sendMessage(TextUtils.mustBeNSFW(context.getPrefix()), context.getChannel());
        return;
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading image...", context.getChannel());
    loadingMsg.send();
    try {
        Document doc = NetUtils.getDoc("https://www.suicidegirls.com/photos/sg/recent/all/");
        Elements girls = doc.getElementsByTag("article");
        Element girl = girls.get(ThreadLocalRandom.current().nextInt(girls.size()));
        String name = girl.getElementsByTag("a").attr("href").split("/")[2].trim();
        String imageUrl = girl.select("noscript").attr("data-retina");
        String url = girl.getElementsByClass("facebook-share").attr("href");
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("SuicideGirls Image").withAuthorIcon("https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/SuicideGirls_logo.svg/1280px-SuicideGirls_logo.svg.png").withAuthorUrl(url).appendDescription(String.format("Name: **%s**", StringUtils.capitalize(name))).withImage(imageUrl);
        loadingMsg.edit(embed.build());
    } catch (IOException err) {
        loadingMsg.delete();
        Utils.handle("getting SuicideGirls image", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Element(org.jsoup.nodes.Element) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 12 with LoadingMessage

use of me.shadorc.shadbot.utils.object.LoadingMessage in project Shadbot by Shadorc.

the class DtcCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    LoadingMessage loadingMsg = new LoadingMessage("Loading quote...", context.getChannel());
    loadingMsg.send();
    try {
        String url = String.format("http://api.danstonchat.com/0.3/view/random?key=%s&format=json", APIKeys.get(APIKey.DTC_API_KEY));
        JSONObject quoteObj = Utils.toList(new JSONArray(NetUtils.getBody(url)), JSONObject.class).stream().filter(obj -> obj.getString("content").length() < 1000).findAny().get();
        String content = quoteObj.getString("content").replace("*", "\\*");
        StringBuilder strBuilder = new StringBuilder();
        for (String line : content.split("\n")) {
            strBuilder.append('\n');
            if (line.contains(" ")) {
                strBuilder.append("**" + line.substring(0, line.indexOf(' ')) + "** " + line.substring(line.indexOf(' ') + 1));
            } else {
                strBuilder.append(line);
            }
        }
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Quote DansTonChat").withAuthorUrl(String.format("https://danstonchat.com/%s.html", quoteObj.getString("id"))).withThumbnail("https://danstonchat.com/themes/danstonchat/images/logo2.png").appendDescription(strBuilder.toString());
        loadingMsg.edit(embed.build());
    } catch (JSONException | IOException err) {
        loadingMsg.delete();
        Utils.handle("getting a quote from DansTonChat.com", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException)

Example 13 with LoadingMessage

use of me.shadorc.shadbot.utils.object.LoadingMessage in project Shadbot by Shadorc.

the class HolidaysCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    Zone zone = Utils.getValueOrNull(Zone.class, context.getArg());
    if (zone == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid zone. %s", context.getArg(), FormatUtils.formatOptions(Zone.class)));
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading holiday information...", context.getChannel());
    loadingMsg.send();
    try {
        String holidays = StringUtils.remove(TwitterUtils.getLastTweet("Vacances_Zone" + zone), "#");
        loadingMsg.edit(Emoji.BEACH + " " + holidays);
    } catch (TwitterException err) {
        loadingMsg.delete();
        Utils.handle("getting holidays information", context, err.getCause());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) TwitterException(twitter4j.TwitterException)

Example 14 with LoadingMessage

use of me.shadorc.shadbot.utils.object.LoadingMessage in project Shadbot by Shadorc.

the class JokeCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    LoadingMessage loadingMsg = new LoadingMessage("Loading joke...", context.getChannel());
    loadingMsg.send();
    try {
        String url = String.format("http://www.une-blague.com/blagues-courtes.html?&p=%d", ThreadLocalRandom.current().nextInt(1, 6));
        Document doc = NetUtils.getDoc(url);
        List<String> jokes = doc.getElementsByClass("texte ").stream().map(elmt -> elmt.html()).filter(elmt -> elmt.length() < 1000).collect(Collectors.toList());
        String jokeHtml = jokes.get(ThreadLocalRandom.current().nextInt(jokes.size()));
        String joke = FormatUtils.format(jokeHtml.split("<br>"), line -> Jsoup.parse(line).text().trim(), "\n");
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Blague").withAuthorUrl("http://www.une-blague.com/").appendDescription(joke);
        loadingMsg.edit(embed.build());
    } catch (IOException err) {
        loadingMsg.delete();
        Utils.handle("getting a joke", context, err);
    }
}
Also used : RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) IOException(java.io.IOException) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) List(java.util.List) NetUtils(me.shadorc.shadbot.utils.NetUtils) Context(me.shadorc.shadbot.core.command.Context) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Document(org.jsoup.nodes.Document) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Jsoup(org.jsoup.Jsoup) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) Utils(me.shadorc.shadbot.utils.Utils) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 15 with LoadingMessage

use of me.shadorc.shadbot.utils.object.LoadingMessage in project Shadbot by Shadorc.

the class HangmanCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    Difficulty difficulty = Utils.getValueOrNull(Difficulty.class, context.getArg());
    if (context.hasArg() && difficulty == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid difficulty. %s", context.getArg(), FormatUtils.formatOptions(Difficulty.class)));
    }
    if (difficulty == null) {
        difficulty = Difficulty.EASY;
    }
    if (HARD_WORDS.isEmpty() || EASY_WORDS.isEmpty()) {
        LoadingMessage loadingMsg = new LoadingMessage("Loading word...", context.getChannel());
        loadingMsg.send();
        try {
            this.load();
        } catch (JSONException | IOException err) {
            Utils.handle("getting words list", context, err);
        }
        loadingMsg.delete();
    }
    HangmanManager hangmanManager = MANAGERS.get(context.getChannel().getLongID());
    if (hangmanManager == null) {
        hangmanManager = new HangmanManager(this, context.getPrefix(), context.getChannel(), context.getAuthor(), difficulty);
        if (MANAGERS.putIfAbsent(context.getChannel().getLongID(), hangmanManager) == null) {
            hangmanManager.start();
        }
    } else {
        BotUtils.sendMessage(String.format(Emoji.INFO + " A Hangman game has already been started by **%s**. Please, wait for him to finish.", hangmanManager.getAuthor().getName()), context.getChannel());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) JSONException(org.json.JSONException) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException)

Aggregations

LoadingMessage (me.shadorc.shadbot.utils.object.LoadingMessage)15 IOException (java.io.IOException)13 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)13 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)10 JSONException (org.json.JSONException)7 JSONObject (org.json.JSONObject)6 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)5 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)5 Context (me.shadorc.shadbot.core.command.Context)5 Command (me.shadorc.shadbot.core.command.annotation.Command)5 RateLimited (me.shadorc.shadbot.core.command.annotation.RateLimited)5 Utils (me.shadorc.shadbot.utils.Utils)5 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)5 HelpBuilder (me.shadorc.shadbot.utils.embed.HelpBuilder)5 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)5 List (java.util.List)4 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)4 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)4 NetUtils (me.shadorc.shadbot.utils.NetUtils)4 StringUtils (me.shadorc.shadbot.utils.StringUtils)4