use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.
the class PlayCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
if (!context.hasArg()) {
throw new MissingArgumentException();
}
IVoiceChannel botVoiceChannel = context.getClient().getOurUser().getVoiceStateForGuild(context.getGuild()).getChannel();
IVoiceChannel userVoiceChannel = context.getAuthor().getVoiceStateForGuild(context.getGuild()).getChannel();
if (botVoiceChannel != null && !botVoiceChannel.equals(userVoiceChannel)) {
throw new IllegalCmdArgumentException(String.format("I'm currently playing music in voice channel %s" + ", join me before using this command.", botVoiceChannel.mention()));
}
if (userVoiceChannel == null) {
throw new IllegalCmdArgumentException("Join a voice channel before using this command.");
}
if (botVoiceChannel == null && !BotUtils.hasPermissions(userVoiceChannel, Permissions.VOICE_CONNECT, Permissions.VOICE_SPEAK)) {
BotUtils.sendMessage(TextUtils.missingPerm(Permissions.VOICE_CONNECT, Permissions.VOICE_SPEAK), context.getChannel());
LogUtils.infof("{Guild ID: %d} Shadbot wasn't allowed to connect/speak in a voice channel.", context.getGuild().getLongID());
return;
}
String identifier;
if (context.getArg().startsWith("soundcloud ")) {
identifier = AudioLoadResultListener.SC_SEARCH + StringUtils.remove(context.getArg(), "soundcloud ");
} else if (NetUtils.isValidURL(context.getArg())) {
identifier = context.getArg();
} else {
identifier = AudioLoadResultListener.YT_SEARCH + context.getArg();
}
GuildMusic guildMusic = GuildMusicManager.GUILD_MUSIC_MAP.get(context.getGuild().getLongID());
if (guildMusic != null && guildMusic.isWaiting()) {
if (guildMusic.getDj().equals(context.getAuthor())) {
throw new IllegalCmdArgumentException(String.format("(**%s**) You're already selecting a music. " + "Enter a number or use `%scancel` to cancel the selection.", context.getAuthorName(), context.getPrefix()));
}
if (identifier.startsWith(AudioLoadResultListener.SC_SEARCH) || identifier.startsWith(AudioLoadResultListener.YT_SEARCH)) {
BotUtils.sendMessage(String.format(Emoji.HOURGLASS + " **%s** is already selecting a music, please wait for him to finish.", guildMusic.getDj().getName()), context.getChannel());
return;
}
}
if (guildMusic == null) {
guildMusic = GuildMusicManager.createGuildMusic(context.getGuild());
}
if (guildMusic.getScheduler().getPlaylist().size() >= Config.MAX_PLAYLIST_SIZE - 1 && !PremiumManager.isPremium(context.getGuild(), context.getAuthor())) {
BotUtils.sendMessage(TextUtils.PLAYLIST_LIMIT_REACHED, context.getChannel());
return;
}
guildMusic.setChannel(context.getChannel());
AudioLoadResultListener resultListener = new AudioLoadResultListener(guildMusic, context.getAuthor(), userVoiceChannel, identifier, context.getCommandName().endsWith("first"));
GuildMusicManager.PLAYER_MANAGER.loadItemOrdered(guildMusic, identifier, resultListener);
}
use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.
the class SkipCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
GuildMusic guildMusic = GuildMusicManager.GUILD_MUSIC_MAP.get(context.getGuild().getLongID());
if (guildMusic == null || guildMusic.getScheduler().isStopped()) {
BotUtils.sendMessage(TextUtils.NO_PLAYING_MUSIC, context.getChannel());
return;
}
if (context.hasArg()) {
Integer num = CastUtils.asIntBetween(context.getArg(), 1, guildMusic.getScheduler().getPlaylist().size());
if (num == null) {
throw new IllegalCmdArgumentException(String.format("Number must be between 1 and %d.", guildMusic.getScheduler().getPlaylist().size()));
}
guildMusic.getScheduler().skipTo(num);
return;
}
if (guildMusic.getScheduler().nextTrack()) {
// If the music has been started correctly, we resume it in case the previous music was on pause
guildMusic.getScheduler().getAudioPlayer().setPaused(false);
} else {
guildMusic.end();
}
}
use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.
the class ShutdownCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
if (!context.hasArg()) {
MessageManager.addListener(context.getChannel(), this);
BotUtils.sendMessage(String.format(Emoji.QUESTION + " Do you really want to shutdown %s ? Yes/No", context.getClient().getOurUser().mention()), context.getChannel());
return;
}
List<String> splitArgs = StringUtils.split(context.getArg(), 2);
if (splitArgs.size() != 2) {
throw new MissingArgumentException();
}
Integer delay = CastUtils.asPositiveInt(splitArgs.get(0));
if (delay == null) {
throw new IllegalCmdArgumentException(String.format("`%s` is not a valid time.", splitArgs.get(0)));
}
String message = splitArgs.get(1);
for (IGuild guild : context.getClient().getGuilds()) {
GuildMusic guildMusic = GuildMusicManager.GUILD_MUSIC_MAP.get(guild.getLongID());
if (guildMusic != null && guildMusic.getChannel() != null) {
BotUtils.sendMessage(Emoji.INFO + " " + message, guildMusic.getChannel());
}
}
Shadbot.getScheduler().schedule(() -> System.exit(0), delay, TimeUnit.SECONDS);
LogUtils.warnf("Shadbot will restart in %d seconds. (Message: %s)", delay, message);
}
use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.
the class WallpaperCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
LoadingMessage loadingMsg = new LoadingMessage("Loading wallpaper...", context.getChannel());
loadingMsg.send();
if (wallhaven == null) {
wallhaven = new Wallhaven(APIKeys.get(APIKey.WALLHAVEN_LOGIN), APIKeys.get(APIKey.WALLHAVEN_PASSWORD));
}
Options options = new Options();
options.addOption("p", PURITY, true, FormatUtils.format(Purity.values(), purity -> purity.toString().toLowerCase(), ", "));
options.addOption("c", CATEGORY, true, FormatUtils.format(Category.values(), cat -> cat.toString().toLowerCase(), ", "));
Option ratioOpt = new Option("rat", RATIO, true, "image ratio");
ratioOpt.setValueSeparator('x');
options.addOption(ratioOpt);
Option resOpt = new Option("res", RESOLUTION, true, "image resolution");
resOpt.setValueSeparator('x');
options.addOption(resOpt);
Option keyOpt = new Option("k", KEYWORD, true, KEYWORD);
keyOpt.setValueSeparator(',');
options.addOption(keyOpt);
CommandLine cmdLine;
try {
List<String> args = StringUtils.split(context.getArg());
cmdLine = new DefaultParser().parse(options, args.toArray(new String[args.size()]));
} catch (UnrecognizedOptionException | org.apache.commons.cli.MissingArgumentException err) {
loadingMsg.delete();
throw new IllegalCmdArgumentException(String.format("%s. Use `%shelp %s` for more information.", err.getMessage(), context.getPrefix(), this.getName()));
} catch (ParseException err) {
loadingMsg.delete();
Utils.handle("getting a wallpaper", context, err);
return;
}
Purity purity = this.parseEnum(loadingMsg, context, Purity.class, PURITY, cmdLine.getOptionValue(PURITY, Purity.SFW.toString()));
if ((purity.equals(Purity.NSFW) || purity.equals(Purity.SKETCHY)) && !context.getChannel().isNSFW()) {
loadingMsg.edit(TextUtils.mustBeNSFW(context.getPrefix()));
return;
}
SearchQueryBuilder queryBuilder = new SearchQueryBuilder();
queryBuilder.purity(purity);
if (cmdLine.hasOption(CATEGORY)) {
queryBuilder.categories(this.parseEnum(loadingMsg, context, Category.class, CATEGORY, cmdLine.getOptionValue(CATEGORY)));
}
if (cmdLine.hasOption(RATIO)) {
Dimension dim = this.parseDim(loadingMsg, context, RATIO, cmdLine.getOptionValues(RATIO));
queryBuilder.ratios(new Ratio((int) dim.getWidth(), (int) dim.getHeight()));
}
if (cmdLine.hasOption(RESOLUTION)) {
Dimension dim = this.parseDim(loadingMsg, context, RESOLUTION, cmdLine.getOptionValues(RESOLUTION));
queryBuilder.resolutions(new Resolution((int) dim.getWidth(), (int) dim.getHeight()));
}
if (cmdLine.hasOption(KEYWORD)) {
queryBuilder.keywords(cmdLine.getOptionValues(KEYWORD));
}
try {
List<Wallpaper> wallpapers = wallhaven.search(queryBuilder.pages(1).build());
if (wallpapers.isEmpty()) {
loadingMsg.edit(TextUtils.noResult(context.getMessage().getContent()));
return;
}
Wallpaper wallpaper = wallpapers.get(ThreadLocalRandom.current().nextInt(wallpapers.size()));
String tags = FormatUtils.format(wallpaper.getTags(), tag -> String.format("`%s`", StringUtils.remove(tag.toString(), "#")), " ");
EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Wallpaper").withAuthorUrl(wallpaper.getUrl()).withImage(wallpaper.getImageUrl()).appendField("Resolution", wallpaper.getResolution().toString(), false).appendField("Tags", tags, false);
loadingMsg.edit(embed.build());
} catch (ConnectionException err) {
loadingMsg.delete();
Utils.handle("getting a wallpaper", context, err.getCause());
}
}
use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.
the class BanCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
if (!context.hasArg()) {
throw new MissingArgumentException();
}
List<IUser> mentionedUsers = context.getMessage().getMentions();
if (mentionedUsers.isEmpty()) {
throw new MissingArgumentException();
}
if (!PermissionUtils.hasPermissions(context.getChannel(), context.getAuthor(), Permissions.BAN)) {
throw new IllegalArgumentException("You don't have permission to ban.");
}
if (!BotUtils.hasPermissions(context.getChannel(), Permissions.BAN)) {
BotUtils.sendMessage(TextUtils.missingPerm(Permissions.BAN), context.getChannel());
return;
}
if (mentionedUsers.contains(context.getAuthor())) {
throw new IllegalCmdArgumentException("You cannot ban yourself.");
}
for (IUser mentionedUser : mentionedUsers) {
if (!PermissionUtils.isUserHigher(context.getGuild(), context.getAuthor(), mentionedUser)) {
throw new IllegalCmdArgumentException(String.format("You can't ban **%s** because he has the same or a higher role " + "position than you in the role hierarchy.", mentionedUser.getName()));
}
if (!BotUtils.canInteract(context.getGuild(), mentionedUser)) {
throw new IllegalCmdArgumentException(String.format("I cannot ban **%s** because he has the same or a higher role " + "position than me in the role hierarchy.", mentionedUser.getName()));
}
}
StringBuilder reason = new StringBuilder();
reason.append(StringUtils.remove(context.getArg(), FormatUtils.format(mentionedUsers, user -> user.mention(false), " ")).trim());
if (reason.length() > Ban.MAX_REASON_LENGTH) {
throw new IllegalCmdArgumentException(String.format("Reason cannot exceed **%d characters**.", Ban.MAX_REASON_LENGTH));
}
if (reason.length() == 0) {
reason.append("Reason not specified.");
}
for (IUser user : mentionedUsers) {
if (!user.isBot()) {
BotUtils.sendMessage(String.format(Emoji.INFO + " You were banned from the server **%s** by **%s**. Reason: `%s`", context.getGuild().getName(), context.getAuthorName(), reason), user.getOrCreatePMChannel());
}
RequestBuffer.request(() -> {
context.getGuild().banUser(user, reason.toString(), 7);
}).get();
}
BotUtils.sendMessage(String.format(Emoji.INFO + " (Requested by **%s**) **%s** got banned. Reason: `%s`", context.getAuthorName(), FormatUtils.format(mentionedUsers, IUser::getName, ", "), reason), context.getChannel());
}
Aggregations