Search in sources :

Example 1 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException 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);
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) AudioLoadResultListener(me.shadorc.shadbot.listener.music.AudioLoadResultListener) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) GuildMusic(me.shadorc.shadbot.music.GuildMusic) IVoiceChannel(sx.blah.discord.handle.obj.IVoiceChannel)

Example 2 with MissingArgumentException

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

the class RepeatCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    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;
    }
    RepeatMode mode = Utils.getValueOrNull(RepeatMode.class, context.getArg());
    if (context.hasArg() && !RepeatMode.PLAYLIST.equals(mode)) {
        throw new MissingArgumentException();
    }
    // By default, modification are made on song repeat mode
    if (mode == null) {
        mode = RepeatMode.SONG;
    }
    TrackScheduler scheduler = guildMusic.getScheduler();
    scheduler.setRepeatMode(scheduler.getRepeatMode().equals(mode) ? RepeatMode.NONE : mode);
    BotUtils.sendMessage(String.format("%s %sRepetition %s", scheduler.getRepeatMode().equals(RepeatMode.NONE) ? Emoji.PLAY : Emoji.REPEAT, RepeatMode.PLAYLIST.equals(mode) ? "Playlist " : "", scheduler.getRepeatMode().equals(RepeatMode.NONE) ? "disabled" : "enabled"), context.getChannel());
}
Also used : RepeatMode(me.shadorc.shadbot.music.RepeatMode) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) GuildMusic(me.shadorc.shadbot.music.GuildMusic) TrackScheduler(me.shadorc.shadbot.music.TrackScheduler)

Example 3 with MissingArgumentException

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

the class WeatherCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading weather information...", context.getChannel());
    loadingMsg.send();
    try {
        OpenWeatherMap owm = new OpenWeatherMap(Units.METRIC, APIKeys.get(APIKey.OPENWEATHERMAP_API_KEY));
        CurrentWeather weather = owm.currentWeatherByCityName(context.getArg());
        if (!weather.isValid()) {
            loadingMsg.edit(TextUtils.noResult(context.getArg()));
            return;
        }
        String clouds = StringUtils.capitalize(weather.getWeatherInstance(0).getWeatherDescription());
        float windSpeed = weather.getWindInstance().getWindSpeed() * 3.6f;
        String windDesc = this.getWindDesc(windSpeed);
        String rain = weather.hasRainInstance() ? String.format("%.1f mm/h", weather.getRainInstance().getRain3h()) : "None";
        float humidity = weather.getMainInstance().getHumidity();
        float temperature = weather.getMainInstance().getTemperature();
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Weather for: " + weather.getCityName()).withThumbnail("https://image.flaticon.com/icons/svg/494/494472.svg").withAuthorUrl("http://openweathermap.org/city/" + weather.getCityCode()).appendDescription("Last updated " + dateFormatter.format(weather.getDateTime())).appendField(Emoji.CLOUD + " Clouds", clouds, true).appendField(Emoji.WIND + " Wind", String.format("%s%n%.1f km/h", windDesc, windSpeed), true).appendField(Emoji.RAIN + " Rain", rain, true).appendField(Emoji.DROPLET + " Humidity", String.format("%.1f%%", humidity), true).appendField(Emoji.THERMOMETER + " Temperature", String.format("%.1f°C", temperature), true);
        loadingMsg.edit(embed.build());
    } catch (IOException err) {
        loadingMsg.delete();
        Utils.handle("getting weather information", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CurrentWeather(net.aksingh.owmjapis.CurrentWeather) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) OpenWeatherMap(net.aksingh.owmjapis.OpenWeatherMap) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException)

Example 4 with MissingArgumentException

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

the class WikiCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    try {
        // Wiki api doc https://en.wikipedia.org/w/api.php?action=help&modules=query%2Bextracts
        JSONObject mainObj = new JSONObject(NetUtils.getBody("https://en.wikipedia.org/w/api.php?" + "format=json" + "&action=query" + "&titles=" + NetUtils.encode(context.getArg()) + "&redirects=true" + "&prop=extracts" + "&explaintext=true" + "&exintro=true" + "&exsentences=5"));
        JSONObject pagesObj = mainObj.getJSONObject("query").getJSONObject("pages");
        String pageId = pagesObj.names().getString(0);
        JSONObject resultObj = pagesObj.getJSONObject(pageId);
        if ("-1".equals(pageId) || resultObj.getString("extract").isEmpty()) {
            BotUtils.sendMessage(TextUtils.noResult(context.getArg()), context.getChannel());
            return;
        }
        String extract = StringUtils.truncate(resultObj.getString("extract"), EmbedBuilder.DESCRIPTION_CONTENT_LIMIT);
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName(String.format("Wikipedia: %s", resultObj.getString("title"))).withAuthorIcon("https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Mohapedia.png/842px-Mohapedia.png").withAuthorUrl(String.format("https://en.wikipedia.org/wiki/%s", resultObj.getString("title").replace(" ", "_"))).appendDescription(extract);
        BotUtils.sendMessage(embed.build(), context.getChannel());
    } catch (JSONException | IOException err) {
        Utils.handle("getting Wikipedia information", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONObject(org.json.JSONObject) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 5 with MissingArgumentException

use of me.shadorc.shadbot.exception.MissingArgumentException 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);
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) GuildMusic(me.shadorc.shadbot.music.GuildMusic) IGuild(sx.blah.discord.handle.obj.IGuild)

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