Search in sources :

Example 21 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException 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 22 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException 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 23 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException in project Shadbot by Shadorc.

the class DatabaseCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(context.getArg());
    if (splitArgs.size() > 2) {
        throw new MissingArgumentException();
    }
    Long guildID = CastUtils.asPositiveLong(splitArgs.get(0));
    if (guildID == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid guild ID.", splitArgs.get(0)));
    }
    IGuild guild = context.getClient().getGuildByID(guildID);
    if (guild == null) {
        throw new IllegalCmdArgumentException("Guild not found.");
    }
    String json = null;
    if (splitArgs.size() == 1) {
        DBGuild dbGuild = Database.getDBGuild(guild);
        json = dbGuild.toJSON().toString(Config.JSON_INDENT_FACTOR);
    } else if (splitArgs.size() == 2) {
        Long userID = CastUtils.asPositiveLong(splitArgs.get(1));
        if (userID == null) {
            throw new IllegalCmdArgumentException(String.format("`%s` is not a valid user ID.", splitArgs.get(0)));
        }
        DBUser dbUser = new DBUser(guild, userID);
        json = dbUser.toJSON().toString(Config.JSON_INDENT_FACTOR);
    }
    if (json == null || json.length() == 2) {
        BotUtils.sendMessage(Emoji.MAGNIFYING_GLASS + " Nothing found.", context.getChannel());
    } else {
        BotUtils.sendMessage(json, context.getChannel());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) DBUser(me.shadorc.shadbot.data.db.DBUser) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 24 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException in project Shadbot by Shadorc.

the class GenerateRelicCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    RelicType type = Utils.getValueOrNull(RelicType.class, context.getArg());
    if (type == null) {
        throw new IllegalCmdArgumentException(String.format("`%s`in not a valid type. %s", context.getArg(), FormatUtils.formatOptions(RelicType.class)));
    }
    Relic relic = PremiumManager.generateRelic(type);
    BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " %s relic generated: **%s**", StringUtils.capitalize(type.toString()), relic.getRelicID()), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) Relic(me.shadorc.shadbot.data.premium.Relic) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) RelicType(me.shadorc.shadbot.data.premium.RelicType)

Example 25 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException in project Shadbot by Shadorc.

the class SendMessageCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(context.getArg(), 2);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Long userID = CastUtils.asPositiveLong(splitArgs.get(0));
    if (userID == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid user ID.", splitArgs.get(0)));
    }
    IUser user = Shadbot.getClient().getUserByID(userID);
    if (user == null) {
        BotUtils.sendMessage(Emoji.GREY_EXCLAMATION + " User not found.", context.getChannel());
        return;
    }
    if (user.equals(context.getOurUser())) {
        throw new IllegalCmdArgumentException("I can't send a private message to myself.");
    }
    if (user.isBot()) {
        throw new IllegalCmdArgumentException("I can't send private message to other bots.");
    }
    context.getClient().getOrCreatePMChannel(user).sendMessage(splitArgs.get(1));
    BotUtils.sendMessage(Emoji.CHECK_MARK + " Message sent.", context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IUser(sx.blah.discord.handle.obj.IUser)

Aggregations

MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)36 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)22 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)15 IOException (java.io.IOException)11 Context (me.shadorc.shadbot.core.command.Context)10 LoadingMessage (me.shadorc.shadbot.utils.object.LoadingMessage)10 List (java.util.List)9 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)9 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)8 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)8 Command (me.shadorc.shadbot.core.command.annotation.Command)8 BotUtils (me.shadorc.shadbot.utils.BotUtils)8 StringUtils (me.shadorc.shadbot.utils.StringUtils)8 HelpBuilder (me.shadorc.shadbot.utils.embed.HelpBuilder)8 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)8 DBGuild (me.shadorc.shadbot.data.db.DBGuild)7 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)7 Emoji (me.shadorc.shadbot.utils.object.Emoji)7 JSONArray (org.json.JSONArray)7 JSONException (org.json.JSONException)7