Search in sources :

Example 1 with WeatherData

use of net.kodehawa.mantarobot.commands.utils.WeatherData in project MantaroBot by Mantaro.

the class UtilsCmds method weather.

@Subscribe
public void weather(CommandRegistry registry) {
    registry.register("weather", new SimpleCommand(Category.UTILS) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (content.isEmpty()) {
                onError(event);
                return;
            }
            EmbedBuilder embed = new EmbedBuilder();
            try {
                long start = System.currentTimeMillis();
                WeatherData data = GsonDataManager.GSON_PRETTY.fromJson(Utils.wgetResty(String.format("http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s", URLEncoder.encode(content, "UTF-8"), MantaroData.config().get().weatherAppId), event), WeatherData.class);
                String countryCode = data.getSys().country;
                String status = data.getWeather().get(0).main;
                Double temp = data.getMain().getTemp();
                double pressure = data.getMain().getPressure();
                int humidity = data.getMain().getHumidity();
                Double ws = data.getWind().speed;
                int cloudiness = data.getClouds().all;
                Double finalTemperatureCelsius = temp - 273.15;
                Double finalTemperatureFahrenheit = temp * 9 / 5 - 459.67;
                Double finalWindSpeedMetric = ws * 3.6;
                Double finalWindSpeedImperial = ws / 0.447046;
                long end = System.currentTimeMillis() - start;
                embed.setColor(Color.CYAN).setTitle(":flag_" + countryCode.toLowerCase() + ":" + " Forecast information for " + content, null).setDescription(status + " (" + cloudiness + "% clouds)").addField(":thermometer: Temperature", String.format("%d°C | %d°F", finalTemperatureCelsius.intValue(), finalTemperatureFahrenheit.intValue()), true).addField(":droplet: Humidity", humidity + "%", true).addBlankField(true).addField(":wind_blowing_face: Wind Speed", String.format("%dkm/h | %dmph", finalWindSpeedMetric.intValue(), finalWindSpeedImperial.intValue()), true).addField("Pressure", pressure + "hPA", true).addBlankField(true).setFooter("Information provided by OpenWeatherMap (Process time: " + end + "ms)", null);
                event.getChannel().sendMessage(embed.build()).queue();
            } catch (NullPointerException npe) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "Error while fetching results. (Not found?)").queue();
            } catch (Exception e) {
                event.getChannel().sendMessage(EmoteReference.ERROR + "Error while fetching results. (Not found?)").queue();
                log.warn("Exception caught while trying to fetch weather data, maybe the API changed something?", e);
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Weather command").setDescription("This command retrieves information from OpenWeatherMap. Used to check **forecast information.**").addField("Usage", "`~>weather <city>,<countrycode>` - **Retrieves the forecast information for the given location.**", false).addField("Parameters", "`city` - **Your city name, e.g. New York, **\n" + "`countrycode` - **(OPTIONAL) The abbreviation for your country, for example US (USA) or MX (Mexico).**", false).addField("Example", "`~>weather New York, US`", false).build();
        }
    });
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) SimpleCommand(net.kodehawa.mantarobot.core.modules.commands.SimpleCommand) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) UnsupportedEncodingException(java.io.UnsupportedEncodingException) WeatherData(net.kodehawa.mantarobot.commands.utils.WeatherData) Subscribe(com.google.common.eventbus.Subscribe)

Aggregations

Subscribe (com.google.common.eventbus.Subscribe)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)1 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 WeatherData (net.kodehawa.mantarobot.commands.utils.WeatherData)1 SimpleCommand (net.kodehawa.mantarobot.core.modules.commands.SimpleCommand)1