2017-12-06 20:34:40 +00:00
using System.Threading.Tasks ;
2017-12-06 20:35:21 +00:00
using Discord ;
2017-12-06 20:34:40 +00:00
using Discord.Commands ;
using Microsoft.Extensions.Configuration ;
2017-12-09 04:17:45 +00:00
using System ;
using dotbot.Core ;
using System.Linq ;
using System.Web ;
2017-12-06 05:28:58 +00:00
namespace dotbot.Commands
{
2017-12-06 20:34:40 +00:00
[Group("weather")]
2017-12-06 05:28:58 +00:00
public class Weather : ModuleBase < SocketCommandContext >
{
2017-12-09 05:59:43 +00:00
public DotbotDb db { get ; set ; }
2017-12-06 20:34:40 +00:00
private IConfigurationRoot _config ;
2017-12-09 04:39:19 +00:00
private string OwmUrl ;
2017-12-06 20:34:40 +00:00
public Weather ( IConfigurationRoot config )
{
_config = config ;
2017-12-09 04:39:19 +00:00
OwmUrl = $"{_config[" endpoints : owm "]}?APPID={_config[" tokens : owm "]}&units=metric" ;
2017-12-06 20:34:40 +00:00
}
[Command]
[Summary("gets the weather for your location")]
public async Task GetWeather ( )
{
2017-12-09 05:59:43 +00:00
await Context . Channel . TriggerTypingAsync ( ) ;
if ( db . UserLocations . Any ( u = > u . Id = = Context . User . Id ) )
2017-12-09 04:17:45 +00:00
{
2017-12-09 05:59:43 +00:00
var url = $"{OwmUrl}&id={db.UserLocations.Find(Context.User.Id).CityId}" ;
await ReplyAsync ( "" , embed : WeatherEmbed ( Utils . GetJson < OwmApiResult > ( url ) ) ) ;
2017-12-09 04:17:45 +00:00
}
2017-12-09 05:59:43 +00:00
else await ReplyAsync ( $"you don't have a location saved. look one up with `{_config[" prefix "]}weather <search term>` or save a location for yourself with `{_config[" prefix "]}savelocation <city>`" ) ;
2017-12-06 20:34:40 +00:00
}
[Command]
[Summary("look up the weather at a mentioned user's saved location")]
public async Task LookupWeatherForUser ( [ Summary ( "user to check time for" ) ] IUser user )
{
2017-12-09 05:59:43 +00:00
await Context . Channel . TriggerTypingAsync ( ) ;
if ( db . UserLocations . Any ( u = > u . Id = = user . Id ) )
2017-12-09 04:17:45 +00:00
{
2017-12-09 05:59:43 +00:00
var url = $"{OwmUrl}&id={db.UserLocations.Find(user.Id).CityId}" ;
await ReplyAsync ( "" , embed : WeatherEmbed ( Utils . GetJson < OwmApiResult > ( url ) ) ) ;
2017-12-09 04:17:45 +00:00
}
2017-12-09 05:59:43 +00:00
else await ReplyAsync ( $"{user.Mention} doesn't have a location saved. look one up with `{_config[" prefix "]}weather <search term>` or save a location for yourself with `{_config[" prefix "]}savelocation <city>`" ) ;
2017-12-06 20:34:40 +00:00
}
[Command]
[Summary("look up the weather at a specified location")]
public async Task LookupWeather ( [ Remainder ] [ Summary ( "location" ) ] string location )
{
2017-12-09 05:59:43 +00:00
await Context . Channel . TriggerTypingAsync ( ) ;
2017-12-09 04:39:19 +00:00
var url = $"{OwmUrl}&q={HttpUtility.UrlEncode(location)}" ;
2017-12-09 04:17:45 +00:00
await ReplyAsync ( "" , embed : WeatherEmbed ( Utils . GetJson < OwmApiResult > ( url ) ) ) ;
2017-12-06 20:34:40 +00:00
}
2017-12-09 04:17:45 +00:00
private Embed WeatherEmbed ( OwmApiResult result )
{
var embed = new EmbedBuilder
{
Title = $"Weather in {result.name}, {result.sys.country}" ,
ThumbnailUrl = $"http://openweathermap.org/img/w/{result.weather[0].icon}.png" ,
} ;
embed . AddField ( "Current Temp" , $"{result.main.temp}°C ({Utils.ConvertCToF(result.main.temp)}°F)" ) ;
embed . AddInlineField ( "Low" , $"{result.main.temp_min}°C ({Utils.ConvertCToF(result.main.temp_min)}°F)" ) ;
embed . AddInlineField ( "High" , $"{result.main.temp_max}°C ({Utils.ConvertCToF(result.main.temp_max)}°F)" ) ;
embed . AddField ( "Current Conditions" , result . weather [ 0 ] . description ) ;
embed . AddInlineField ( "Atmospheric Pressure" , $"{result.main.pressure} hPa" ) ;
embed . AddInlineField ( "Humidity" , $"{result.main.humidity}%" ) ;
embed . AddInlineField ( "Wind" , $"{result.wind.speed} meters/second, {result.wind.deg}°" ) ;
var UnixEpoch = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , DateTimeKind . Utc ) . ToLocalTime ( ) ;
embed . AddInlineField ( "Sunrise" , $"{UnixEpoch.AddSeconds(result.sys.sunrise):t}" ) ;
embed . AddInlineField ( "Sunset" , $"{UnixEpoch.AddSeconds(result.sys.sunset):t}" ) ;
return embed ;
2017-12-06 20:34:40 +00:00
}
2017-12-06 05:28:58 +00:00
}
2017-12-09 04:39:19 +00:00
public class OwmApiResult
{
public int id { get ; set ; }
public string name { get ; set ; }
public class Coord
{
public double lon { get ; set ; }
public double lat { get ; set ; }
}
public Coord coord { get ; set ; }
public class Weather
{
public int id { get ; set ; }
public string main { get ; set ; }
public string description { get ; set ; }
public string icon { get ; set ; }
}
public Weather [ ] weather { get ; set ; }
public class WeatherMain
{
public double temp { get ; set ; }
public double pressure { get ; set ; }
public double humidity { get ; set ; }
public double temp_min { get ; set ; }
public double temp_max { get ; set ; }
}
public WeatherMain main { get ; set ; }
public class Wind
{
public double speed { get ; set ; }
public int deg { get ; set ; }
}
public Wind wind { get ; set ; }
public class Sys
{
public string country { get ; set ; }
public double sunrise { get ; set ; }
public double sunset { get ; set ; }
}
public Sys sys { get ; set ; }
}
2017-12-06 05:28:58 +00:00
}