diff --git a/Commands/Images.cs b/Commands/Images.cs new file mode 100644 index 0000000..f3c3134 --- /dev/null +++ b/Commands/Images.cs @@ -0,0 +1,20 @@ +using Discord.Commands; +using dotbot.Core; +using Microsoft.Extensions.Configuration; +using System.Linq; +using System.Threading.Tasks; + +namespace dotbot.Commands +{ + [Group("img")] + public class Images : ModuleBase + { + [Command] + [Alias("image", "pic")] + [Summary("get a saved image")] + public async Task GetImage([Summary("image name")] string name) + { + + } + } +} diff --git a/Commands/Location.cs b/Commands/Location.cs index 3faca88..56d97c1 100644 --- a/Commands/Location.cs +++ b/Commands/Location.cs @@ -9,15 +9,13 @@ namespace dotbot.Commands { public class Location : ModuleBase { - private readonly IConfigurationRoot _config; - private string OwmApiUrl = "http://api.openweathermap.org/data/2.5/weather"; - private string GeoNamesUrl = "http://api.geonames.org/timezoneJSON"; + private string OwmUrl; + private string GeoNamesUrl; public Location(IConfigurationRoot config) { - _config = config; - OwmApiUrl += $"?APPID={_config["tokens:owm"]}&units=metric"; - GeoNamesUrl += $"?username={_config["tokens:geonames"]}"; + OwmUrl = $"{config["endpoints:owm"]}?APPID={config["tokens:owm"]}&units=metric"; + GeoNamesUrl = $"{config["endpoints:geonames"]}?username={config["tokens:geonames"]}"; } @@ -25,8 +23,8 @@ namespace dotbot.Commands [Summary("save your location so benbot can look up your weather and timezone")] public async Task SaveUserLocation([Remainder] [Summary("location")] string location) { - var owm = Utils.GetJson($"{OwmApiUrl}&q={HttpUtility.UrlEncode(location)}"); - var geo = Utils.GetJson($"{GeoNamesUrl}&lat={owm.coord.lat}&lng={owm.coord.lon}"); + var owm = Utils.GetJson($"{OwmUrl}&q={HttpUtility.UrlEncode(location)}"); + var geo = Utils.GetJson($"{GeoNamesUrl}&lat={owm.coord.lat}&lng={owm.coord.lon}"); using (var db = new DotbotDbContext()) { if (db.UserLocations.Any(u => u.Id == Context.User.Id)) diff --git a/Commands/Time.cs b/Commands/Time.cs index 5141a6e..283d4f1 100644 --- a/Commands/Time.cs +++ b/Commands/Time.cs @@ -14,16 +14,17 @@ namespace dotbot.Commands public class Time : ModuleBase { private readonly IConfigurationRoot _config; - private string OwmApiUrl = "http://api.openweathermap.org/data/2.5/weather"; - private string GeoNamesUrl = "http://api.geonames.org/timezoneJSON"; + private string OwmUrl; + private string GeoNamesUrl; public Time(IConfigurationRoot config) { _config = config; - OwmApiUrl += $"?APPID={_config["tokens:owm"]}&units=metric"; - GeoNamesUrl += $"?username={_config["tokens:geonames"]}"; + OwmUrl = $"{_config["endpoints:owm"]}?APPID={_config["tokens:owm"]}&units=metric"; + GeoNamesUrl = $"{_config["endpoints:geonames"]}?username={_config["tokens:geonames"]}"; } + [Command] [Summary("get the time at your location (or bot's location if you don't have one saved)")] public async Task GetTime() @@ -33,7 +34,7 @@ namespace dotbot.Commands if (db.UserLocations.Any(u => u.Id == Context.User.Id)) { var ul = db.UserLocations.Find(Context.User.Id); - await ReplyAsync($"it's {GetTimeFromIanaId(ul.TimeZone):g} in {ul.City}"); + await ReplyAsync($"it's {Utils.IanaIdToDateTime(ul.TimeZone):g} in {ul.City}"); } else await ReplyAsync($"it's {DateTime.Now:g} Eastern Time (where the bot is hosted)\n\nyou can save your location/timezone with `{_config["prefix"]}savelocation `"); } @@ -49,12 +50,9 @@ namespace dotbot.Commands if (db.UserLocations.Any(u => u.Id == user.Id)) { var ul = db.UserLocations.Find(user.Id); - await ReplyAsync($"the time for {user.Mention} in {ul.City} is {GetTimeFromIanaId(ul.TimeZone):g}"); - } - else - { - await ReplyAsync($"{user.Mention} does not have a saved location\nit's {DateTime.Now:g} Eastern Time (US)"); + await ReplyAsync($"the time for {user.Mention} in {ul.City} is {Utils.IanaIdToDateTime(ul.TimeZone):g}"); } + else await ReplyAsync($"{user.Mention} does not have a saved location\nit's {DateTime.Now:g} Eastern Time (US)"); } } @@ -64,17 +62,17 @@ namespace dotbot.Commands public async Task LookupTime([Remainder] [Summary("location")] string location) { await Context.Channel.TriggerTypingAsync(); - var owm = Utils.GetJson($"{OwmApiUrl}&q={HttpUtility.UrlEncode(location)}"); + var owm = Utils.GetJson($"{OwmUrl}&q={HttpUtility.UrlEncode(location)}"); var geo = Utils.GetJson($"{GeoNamesUrl}&lat={owm.coord.lat}&lng={owm.coord.lon}"); - await ReplyAsync($"it is {GetTimeFromIanaId(geo.timezoneId)} in {owm.name}"); + await ReplyAsync($"it is {Utils.IanaIdToDateTime(geo.timezoneId)} in {owm.name}"); } - - public class GeonamesApiResult - { - public string timezoneId { get; set; } - } - - public static DateTime GetTimeFromIanaId(string tzId) => SystemClock.Instance.GetCurrentInstant().InZone(DateTimeZoneProviders.Tzdb[tzId]).ToDateTimeUnspecified(); } + + + public class GeonamesApiResult + { + public string timezoneId { get; set; } + } + } diff --git a/Commands/Weather.cs b/Commands/Weather.cs index 50755d9..d4ebc12 100644 --- a/Commands/Weather.cs +++ b/Commands/Weather.cs @@ -13,14 +13,12 @@ namespace dotbot.Commands public class Weather : ModuleBase { private IConfigurationRoot _config; - private string OwmApiUrl = "http://api.openweathermap.org/data/2.5/weather"; - private string GeoNamesUrl = "http://api.geonames.org/timezoneJSON"; + private string OwmUrl; public Weather(IConfigurationRoot config) { _config = config; - OwmApiUrl += $"?APPID={_config["tokens:owm"]}&units=metric"; - GeoNamesUrl += $"?username={_config["tokens:geonames"]}"; + OwmUrl = $"{_config["endpoints:owm"]}?APPID={_config["tokens:owm"]}&units=metric"; } @@ -32,7 +30,7 @@ namespace dotbot.Commands { if (db.UserLocations.Any(u => u.Id == Context.User.Id)) { - var url = $"{OwmApiUrl}&id={db.UserLocations.Find(Context.User.Id).CityId}"; + var url = $"{OwmUrl}&id={db.UserLocations.Find(Context.User.Id).CityId}"; await ReplyAsync("", embed: WeatherEmbed(Utils.GetJson(url))); } else await ReplyAsync($"you don't have a location saved. look one up with `{_config["prefix"]}weather ` or save a location for yourself with `{_config["prefix"]}savelocation `"); @@ -48,7 +46,7 @@ namespace dotbot.Commands { if (db.UserLocations.Any(u => u.Id == user.Id)) { - var url = $"{OwmApiUrl}&id={db.UserLocations.Find(user.Id).CityId}"; + var url = $"{OwmUrl}&id={db.UserLocations.Find(user.Id).CityId}"; await ReplyAsync("", embed: WeatherEmbed(Utils.GetJson(url))); } else await ReplyAsync($"{user.Mention} doesn't have a location saved. look one up with `{_config["prefix"]}weather ` or save a location for yourself with `{_config["prefix"]}savelocation `"); @@ -60,53 +58,11 @@ namespace dotbot.Commands [Summary("look up the weather at a specified location")] public async Task LookupWeather([Remainder] [Summary("location")] string location) { - var url = $"{OwmApiUrl}&q={HttpUtility.UrlEncode(location)}"; + var url = $"{OwmUrl}&q={HttpUtility.UrlEncode(location)}"; await ReplyAsync("", embed: WeatherEmbed(Utils.GetJson(url))); } - 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; } - } - private Embed WeatherEmbed(OwmApiResult result) { var embed = new EmbedBuilder @@ -128,4 +84,47 @@ namespace dotbot.Commands } } + + 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; } + } + } diff --git a/Core/DotbotDbContext.cs b/Core/DotbotDbContext.cs index 610b31d..b7e6460 100644 --- a/Core/DotbotDbContext.cs +++ b/Core/DotbotDbContext.cs @@ -7,6 +7,7 @@ namespace dotbot.Core public DbSet Defs { get; set; } public DbSet Emails { get; set; } public DbSet UserLocations { get; set; } + public DbSet Images { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/Core/Image.cs b/Core/Image.cs new file mode 100644 index 0000000..ac3a05a --- /dev/null +++ b/Core/Image.cs @@ -0,0 +1,8 @@ +namespace dotbot.Core +{ + public class Image + { + public string Id { get; set; } + public string FilePath { get; set; } + } +} diff --git a/Core/Utils.cs b/Core/Utils.cs index 6601ba2..5f1f4a2 100644 --- a/Core/Utils.cs +++ b/Core/Utils.cs @@ -1,4 +1,6 @@ using Newtonsoft.Json; +using NodaTime; +using System; using System.Net; namespace dotbot.Core @@ -13,5 +15,7 @@ namespace dotbot.Core public static double ConvertCToF(double c) => ((9.0 / 5.0) * c) + 32; + public static DateTime IanaIdToDateTime(string tzId) => SystemClock.Instance.GetCurrentInstant().InZone(DateTimeZoneProviders.Tzdb[tzId]).ToDateTimeUnspecified(); + } } diff --git a/Migrations/20171209043817_AddImageTable.Designer.cs b/Migrations/20171209043817_AddImageTable.Designer.cs new file mode 100644 index 0000000..49b48f3 --- /dev/null +++ b/Migrations/20171209043817_AddImageTable.Designer.cs @@ -0,0 +1,80 @@ +// +using dotbot.Core; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage; +using System; + +namespace dotbot.Migrations +{ + [DbContext(typeof(DotbotDbContext))] + [Migration("20171209043817_AddImageTable")] + partial class AddImageTable + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "2.0.1-rtm-125"); + + modelBuilder.Entity("dotbot.Core.Definition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Def"); + + b.HasKey("Id"); + + b.ToTable("Defs"); + }); + + modelBuilder.Entity("dotbot.Core.Email", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("EmailAddress"); + + b.HasKey("Id"); + + b.ToTable("Emails"); + }); + + modelBuilder.Entity("dotbot.Core.Image", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("FilePath"); + + b.HasKey("Id"); + + b.ToTable("Images"); + }); + + modelBuilder.Entity("dotbot.Core.UserLocation", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("City"); + + b.Property("CityId"); + + b.Property("Lat"); + + b.Property("Lng"); + + b.Property("TimeZone"); + + b.HasKey("Id"); + + b.ToTable("UserLocations"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20171209043817_AddImageTable.cs b/Migrations/20171209043817_AddImageTable.cs new file mode 100644 index 0000000..29e6005 --- /dev/null +++ b/Migrations/20171209043817_AddImageTable.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using System; +using System.Collections.Generic; + +namespace dotbot.Migrations +{ + public partial class AddImageTable : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Images", + columns: table => new + { + Id = table.Column(nullable: false), + FilePath = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Images", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Images"); + } + } +} diff --git a/Migrations/DotbotDbContextModelSnapshot.cs b/Migrations/DotbotDbContextModelSnapshot.cs index 8dcfe76..0878d2f 100644 --- a/Migrations/DotbotDbContextModelSnapshot.cs +++ b/Migrations/DotbotDbContextModelSnapshot.cs @@ -42,6 +42,18 @@ namespace dotbot.Migrations b.ToTable("Emails"); }); + modelBuilder.Entity("dotbot.Core.Image", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("FilePath"); + + b.HasKey("Id"); + + b.ToTable("Images"); + }); + modelBuilder.Entity("dotbot.Core.UserLocation", b => { b.Property("Id") @@ -51,9 +63,9 @@ namespace dotbot.Migrations b.Property("CityId"); - b.Property("Lat"); + b.Property("Lat"); - b.Property("Lng"); + b.Property("Lng"); b.Property("TimeZone"); diff --git a/_config.example.json b/_config.example.json index ad0c5c2..cdff9b6 100644 --- a/_config.example.json +++ b/_config.example.json @@ -7,5 +7,9 @@ "gmail": "", // password here "owm": "", "geonames": "" + }, + "endpoints": { + "owm": "http://api.openweathermap.org/data/2.5/weather", + "geonames": "http://api.geonames.org/timezoneJSON" } }