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());
}
}
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());
}
}
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));
}
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));
}
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);
}
}
Aggregations