Search in sources :

Example 11 with MissingArgumentException

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

the class AutoMessageSetting method channel.

private void channel(Context context, Action action) throws MissingArgumentException {
    List<IChannel> channelsMentioned = context.getMessage().getChannelMentions();
    if (channelsMentioned.size() != 1) {
        throw new MissingArgumentException();
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    IChannel channel = channelsMentioned.get(0);
    if (Action.ENABLE.equals(action)) {
        dbGuild.setSetting(SettingEnum.MESSAGE_CHANNEL_ID, channel.getLongID());
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " %s is now the default channel for join/leave messages.", channel.mention()), context.getChannel());
    } else if (Action.DISABLE.equals(action)) {
        dbGuild.removeSetting(SettingEnum.MESSAGE_CHANNEL_ID);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Auto-messages disabled. I will no longer send automatic messages " + "until a new channel is defined.", channel.mention()), context.getChannel());
    }
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException)

Example 12 with MissingArgumentException

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

the class AutoMessageSetting method updateLeaveMessage.

private void updateLeaveMessage(Context context, Action action, List<String> args) throws MissingArgumentException {
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    if (Action.ENABLE.equals(action)) {
        if (args.size() < 3) {
            throw new MissingArgumentException();
        }
        String message = args.get(2);
        dbGuild.setSetting(SettingEnum.LEAVE_MESSAGE, message);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Leave message set to `%s`", message), context.getChannel());
    } else if (Action.DISABLE.equals(action)) {
        dbGuild.removeSetting(SettingEnum.LEAVE_MESSAGE);
        BotUtils.sendMessage(Emoji.CHECK_MARK + " Leave message disabled.", context.getChannel());
    }
}
Also used : DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException)

Example 13 with MissingArgumentException

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

the class AutoRoleSetting method execute.

@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!BotUtils.hasPermissions(context.getChannel(), Permissions.MANAGE_ROLES)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.MANAGE_ROLES), context.getChannel());
        LogUtils.infof("{Guild ID: %d} Shadbot wasn't allowed to manage roles.", context.getGuild().getLongID());
        return;
    }
    if (arg == null) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(arg);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
    if (action == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
    }
    List<IRole> mentionedRoles = context.getMessage().getRoleMentions();
    if (mentionedRoles.isEmpty()) {
        throw new MissingArgumentException();
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<Long> roles = dbGuild.getAutoRoles();
    if (Action.ADD.equals(action)) {
        for (IRole role : mentionedRoles) {
            if (!PermissionUtils.hasHierarchicalPermissions(context.getGuild(), context.getOurUser(), Arrays.asList(role))) {
                throw new IllegalCmdArgumentException(String.format("%s is a higher role in the role hierarchy than mine, I can't auto-assign it.", role.mention()));
            }
        }
        roles.addAll(mentionedRoles.stream().map(IRole::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format("New comers will now have role(s): %s", FormatUtils.format(roles, role -> context.getGuild().getRoleByID(role).mention(), ", ")), context.getChannel());
    } else {
        roles.removeAll(mentionedRoles.stream().map(IRole::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format("%s removed from auto-assigned roles.", FormatUtils.format(mentionedRoles, IRole::mention, ", ")), context.getChannel());
    }
    dbGuild.setSetting(SettingEnum.AUTO_ROLE, new JSONArray(roles));
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) IRole(sx.blah.discord.handle.obj.IRole) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 14 with MissingArgumentException

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

the class BlacklistSettingCmd method execute.

@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
    if (arg == null) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(arg, 2);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
    if (action == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
    }
    List<String> commands = StringUtils.split(splitArgs.get(1).toLowerCase());
    List<String> unknownCmds = commands.stream().filter(cmd -> CommandManager.getCommand(cmd) == null).collect(Collectors.toList());
    if (!unknownCmds.isEmpty()) {
        throw new IllegalCmdArgumentException(String.format("Command %s doesn't exist.", FormatUtils.format(unknownCmds, cmd -> String.format("`%s`", cmd), ", ")));
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<String> blacklist = dbGuild.getBlacklistedCmd();
    if (Action.ADD.equals(action)) {
        blacklist.addAll(commands);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Command(s) `%s` added to the blacklist.", FormatUtils.format(commands, cmd -> String.format("`%s`", cmd), ", ")), context.getChannel());
    } else if (Action.REMOVE.equals(action)) {
        blacklist.removeAll(commands);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Command(s) `%s` removed from the blacklist.", FormatUtils.format(commands, cmd -> String.format("`%s`", cmd), ", ")), context.getChannel());
    }
    dbGuild.setSetting(this.getSetting(), new JSONArray(blacklist));
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) SettingEnum(me.shadorc.shadbot.command.admin.setting.core.SettingEnum) DBGuild(me.shadorc.shadbot.data.db.DBGuild) AbstractSetting(me.shadorc.shadbot.command.admin.setting.core.AbstractSetting) CommandManager(me.shadorc.shadbot.core.command.CommandManager) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) List(java.util.List) Context(me.shadorc.shadbot.core.command.Context) Database(me.shadorc.shadbot.data.db.Database) Setting(me.shadorc.shadbot.command.admin.setting.core.Setting) Emoji(me.shadorc.shadbot.utils.object.Emoji) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) JSONArray(org.json.JSONArray) Utils(me.shadorc.shadbot.utils.Utils) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 15 with MissingArgumentException

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

the class CounterStrikeCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading CS:GO stats...", context.getChannel());
    loadingMsg.send();
    try {
        String arg = context.getArg();
        String steamid = null;
        // The user provided an URL that can contains a pseudo or an ID
        if (arg.contains("/")) {
            List<String> splittedURl = StringUtils.split(arg, "/");
            arg = splittedURl.get(splittedURl.size() - 1);
        }
        // The user directly provided the ID
        if (CastUtils.isPositiveLong(arg)) {
            steamid = arg;
        } else // The user provided a pseudo
        {
            String url = String.format("http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s", APIKeys.get(APIKey.STEAM_API_KEY), NetUtils.encode(arg));
            JSONObject mainObj = new JSONObject(NetUtils.getBody(url));
            JSONObject responseObj = mainObj.getJSONObject("response");
            steamid = responseObj.optString("steamid");
        }
        String url = String.format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", APIKeys.get(APIKey.STEAM_API_KEY), steamid);
        JSONObject mainUserObj = new JSONObject(NetUtils.getBody(url));
        // Search users matching the steamID
        JSONArray players = mainUserObj.getJSONObject("response").getJSONArray("players");
        if (players.length() == 0) {
            loadingMsg.edit(Emoji.MAGNIFYING_GLASS + " User not found.");
            return;
        }
        JSONObject userObj = players.getJSONObject(0);
        /*
			 * CommunityVisibilityState
			 * 1: Private
			 * 2: FriendsOnly
			 * 3: Public
			 */
        if (userObj.getInt("communityvisibilitystate") != 3) {
            loadingMsg.edit(Emoji.ACCESS_DENIED + " This profile is private.");
            return;
        }
        url = String.format("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=%s&steamid=%s", APIKeys.get(APIKey.STEAM_API_KEY), steamid);
        JSONObject mainStatsObj = new JSONObject(NetUtils.getBody(url));
        if (!mainStatsObj.has("playerstats") || !mainStatsObj.getJSONObject("playerstats").has("stats")) {
            loadingMsg.edit(Emoji.MAGNIFYING_GLASS + " This user doesn't play Counter-Strike: Global Offensive.");
            return;
        }
        JSONArray statsArray = mainStatsObj.getJSONObject("playerstats").getJSONArray("stats");
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Counter-Strike: Global Offensive Stats").withAuthorIcon("http://www.icon100.com/up/2841/256/csgo.png").withAuthorUrl("http://steamcommunity.com/profiles/" + steamid).withThumbnail(userObj.getString("avatarfull")).appendDescription(String.format("Stats for **%s**", userObj.getString("personaname"))).appendField("Kills", Integer.toString(this.getValue(statsArray, "total_kills")), true).appendField("Deaths", Integer.toString(this.getValue(statsArray, "total_deaths")), true).appendField("Ratio", String.format("%.2f", (float) this.getValue(statsArray, "total_kills") / this.getValue(statsArray, "total_deaths")), true).appendField("Total wins", Integer.toString(this.getValue(statsArray, "total_wins")), true).appendField("Total MVP", Integer.toString(this.getValue(statsArray, "total_mvps")), true);
        loadingMsg.edit(embed.build());
    } catch (JSONException | IOException err) {
        loadingMsg.delete();
        Utils.handle("getting Counter-Strike: Global Offensive stats", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONObject(org.json.JSONObject) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException)

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