Exemplo n.º 1
0
        public async Task Forecast([Remainder] string city)
        {
            var builder = new EmbedBuilder();

            try
            {
                builder.Color = Discord.Color.Green;
                XmlDocument doc = new XmlDocument();
                doc.Load($"http://api.openweathermap.org/data/2.5/forecast?q={city}&mode=xml&appid={appkey}");
                XmlNode fore = doc.SelectSingleNode("weatherdata/forecast");
                foreach (XmlNode node in fore)
                {
                    string[] temp; //string precipitation; //string clouds; string winddir; string windspeed;
                    temp = Weathercmd.TempAll(double.Parse(node.SelectSingleNode("temperature").Attributes[1].InnerText)).Split('_');
                    //precipitation = Weathercmd.Precipitationforecast(node.SelectSingleNode("precipitation"));
                    //builder.AddInlineField("test", precipitation);

                    builder.AddInlineField($"{node.Attributes[0].InnerText.Replace('T', ' ')} to {node.Attributes[1].InnerText.Replace('T', ' ')}", $"Temperature = {temp[1]}°C, {temp[2]}°F");
                    //+ $"\nPrecipitation = {precipitation}");
                }
                await ReplyAsync("", false, builder.Build());
            }
            catch (Exception ex)
            {
                builder.Color = Discord.Color.Red;
                builder.AddField(x =>
                {
                    x.Name     = "Error";
                    x.Value    = ex.Message.ToString();
                    x.IsInline = false;
                });
                await ReplyAsync("", false, builder.Build());
            }
        }
Exemplo n.º 2
0
        public async Task Weather([Remainder] string city)
        {
            if (city.Contains("_"))
            {
                city.Replace('_', ' ');
            }
            var builder = new EmbedBuilder();

            try
            {
                string      lon; string lat; string country; string sunrise; string sunset; string sunuptime; string[] avetemp; string[] mintemp; string[] maxtemp; string humidity; string pressure; string windspeedname;
                string      windspeed; string clouds; string visibility; string precipitation; string weatherID; string weathername; string weatherEmoji; string[] lastupdate; string cityname; string cityid;
                XmlDocument doc = new XmlDocument();
                doc.Load($"http://api.openweathermap.org/data/2.5/weather?q={city}&type=accurate&mode=xml&appid={appkey}");
                XmlNode node = doc.SelectSingleNode("current");
                cityid        = node.SelectSingleNode("city").Attributes[0].InnerText;
                cityname      = node.SelectSingleNode("city").Attributes[1].InnerText;
                lon           = node.SelectSingleNode("city/coord").Attributes[0].InnerText;
                lat           = node.SelectSingleNode("city/coord").Attributes[1].InnerText;
                country       = node.SelectSingleNode("city/country").InnerText;
                sunrise       = node.SelectSingleNode("city/sun").Attributes[0].InnerText;
                sunset        = node.SelectSingleNode("city/sun").Attributes[1].InnerText;
                sunuptime     = Weathercmd.SunUptime(sunrise, sunset);
                avetemp       = Weathercmd.TempAll(double.Parse(node.SelectSingleNode("temperature").Attributes[0].InnerText)).Split('_');
                mintemp       = Weathercmd.TempAll(double.Parse(node.SelectSingleNode("temperature").Attributes[1].InnerText)).Split('_');
                maxtemp       = Weathercmd.TempAll(double.Parse(node.SelectSingleNode("temperature").Attributes[2].InnerText)).Split('_');
                humidity      = node.SelectSingleNode("humidity").Attributes[0].InnerText + "%";
                pressure      = node.SelectSingleNode("pressure").Attributes[0].InnerText + " hPa";
                windspeedname = node.SelectSingleNode("wind/speed").Attributes[1].InnerText;
                windspeed     = node.SelectSingleNode("wind/speed").Attributes[0].InnerText;
                var winddirct = new List <string>();
                for (int i = 0; i <= 2; i++)
                {
                    winddirct.Add(node.SelectSingleNode("wind/direction").Attributes[i].InnerText);
                }
                clouds        = node.SelectSingleNode("clouds").Attributes[0].InnerText + "%, " + node.SelectSingleNode("clouds").Attributes[1].InnerText;
                visibility    = node.SelectSingleNode("visibility").Attributes[0].InnerText;
                precipitation = Weathercmd.Precipitationweather(node.SelectSingleNode("precipitation"));
                Dictionary <int, string> emoji = new Dictionary <int, string>()
                {
                    { 200, ":thunder_cloud_rain:" }, { 201, ":thunder_cloud_rain:" }, { 202, ":thunder_cloud_rain:" }, { 210, ":thunder_cloud_rain:" }, { 211, ":thunder_cloud_rain:" }, { 212, ":thunder_cloud_rain:" }, { 221, ":thunder_cloud_rain:" }, { 230, ":thunder_cloud_rain:" }, { 231, ":thunder_cloud_rain:" }, { 232, ":thunder_cloud_rain:" },
                    { 300, ":cloud_rain:" }, { 301, ":cloud_rain:" }, { 302, ":cloud_rain:" }, { 310, ":cloud_rain:" }, { 311, ":cloud_rain:" }, { 312, ":cloud_rain:" }, { 313, ":cloud_rain:" }, { 314, ":cloud_rain:" }, { 321, ":cloud_rain:" },
                    { 500, ":white_sun_rain_cloud:" }, { 501, ":white_sun_rain_cloud:" }, { 502, ":white_sun_rain_cloud:" }, { 503, ":white_sun_rain_cloud:" }, { 504, ":white_sun_rain_cloud:" }, { 511, ":cloud_snow:" }, { 520, ":cloud_rain:" }, { 521, ":cloud_rain:" }, { 522, ":cloud_rain:" }, { 531, ":cloud_rain:" },
                    { 600, ":cloud_snow:" }, { 601, ":cloud_snow:" }, { 602, ":cloud_snow:" }, { 611, ":cloud_snow:" }, { 612, ":cloud_snow:" }, { 615, ":cloud_snow:" }, { 616, ":cloud_snow:" }, { 620, ":cloud_snow:" }, { 621, ":cloud_snow:" }, { 622, ":cloud_snow:" },
                    { 701, ":fog:" }, { 711, ":fog:" }, { 721, ":fog:" }, { 731, ":fog:" }, { 741, ":fog:" }, { 751, ":fog:" }, { 761, ":fog:" }, { 762, ":fog:" }, { 771, ":fog:" }, { 781, ":fog:" }, { 800, ":sunny:" }, { 900, ":cloud_tornado: Get out of there" }, { 903, ":flame:" }, { 904, ":snowflake:" }
                };
                weatherID     = node.SelectSingleNode("weather").Attributes[0].InnerText;
                weathername   = node.SelectSingleNode("weather").Attributes[1].InnerText;
                weatherEmoji  = Weathercmd.Emoji(emoji, weatherID, weathername);
                lastupdate    = node.SelectSingleNode("lastupdate").Attributes[0].InnerText.Split('T');
                builder.Color = Discord.Color.Green;
                builder.Title = $"Weather for {cityname}, {country}  :flag_{country.ToLower()}:";
                builder.AddField(x =>
                {
                    x.Name  = $"cityinfo";
                    x.Value = $"City id = {cityid}"
                              + $"\nLon = {lon}, Lat = {lat}"
                              + $"\nSunrise = {sunrise.Split('T')[1]}"
                              + $"\nSunup = {sunuptime}"
                              + $"\nSunset = {sunset.Split('T')[1]}";
                    x.IsInline = false;
                });
                builder.AddInlineField("Temperature", $"Average = {avetemp[1]}°C, {avetemp[2]}°F, {avetemp[0]}°K"
                                       + $"\nminimum = {mintemp[1]}°C, {mintemp[2]}°F, {mintemp[0]}°K"
                                       + $"\nmaximum = {maxtemp[1]}°C, {maxtemp[2]}°F, {maxtemp[0]}°K"
                                       + $"\nHumidity = {humidity}"
                                       + $"\nPressure = {pressure}");
                builder.AddField(x =>
                {
                    x.Name  = "Wind";
                    x.Value = $"speed = {windspeedname}, {windspeed}m/s, {(double.Parse((windspeed).Replace(".", ",")) * 3.6).ToString().Replace(",", ".")}km/h"
                              + $"\ndirection = {winddirct[1]}, {winddirct[2]}, {winddirct[0]}°";
                    x.IsInline = false;
                });
                builder.AddInlineField("clouds", $"Clouds = {clouds}"
                                       + $"Visibility = {visibility}m, {double.Parse(visibility) / 1000}km");
                builder.AddField(x =>
                {
                    x.Name     = "Precipitation";
                    x.Value    = $"precipitation = {precipitation}";
                    x.IsInline = false;
                });
                builder.AddInlineField("weather icon", weatherEmoji + $"\n weatherId = {weatherID}");
                builder.AddField(x =>
                {
                    x.Name     = "Last up date";
                    x.Value    = $"{lastupdate[0]} at {lastupdate[1]}";
                    x.IsInline = false;
                });

                await ReplyAsync("", false, builder.Build());
            }
            catch (Exception ex)
            {
                builder.Color = Discord.Color.Red;
                builder.AddField(x =>
                {
                    x.Name     = "Error";
                    x.Value    = ex.Message.ToString();
                    x.IsInline = false;
                });
                await ReplyAsync("", false, builder.Build());
            }
        }