From 043f20a42d5b8eb666566ec3397a4dbaa43a5f94 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Wed, 6 Dec 2017 00:28:58 -0500 Subject: [PATCH] start work on time and weather --- Commands/Time.cs | 66 ++++++++++++++++++ Commands/Weather.cs | 9 +++ Core/DotbotDbContext.cs | 1 + Core/UserLocation.cs | 12 ++++ .../20171206050609_UserLocations.Designer.cs | 68 +++++++++++++++++++ Migrations/20171206050609_UserLocations.cs | 35 ++++++++++ Migrations/DotbotDbContextModelSnapshot.cs | 20 ++++++ Services/CommandHandlerService.cs | 21 +++--- 8 files changed, 221 insertions(+), 11 deletions(-) create mode 100644 Commands/Time.cs create mode 100644 Commands/Weather.cs create mode 100644 Core/UserLocation.cs create mode 100644 Migrations/20171206050609_UserLocations.Designer.cs create mode 100644 Migrations/20171206050609_UserLocations.cs diff --git a/Commands/Time.cs b/Commands/Time.cs new file mode 100644 index 0000000..e840aad --- /dev/null +++ b/Commands/Time.cs @@ -0,0 +1,66 @@ +using Discord; +using Discord.Commands; +using dotbot.Core; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace dotbot.Commands +{ + [Group("time")] + public class Time : ModuleBase + { + [Command] + [Summary("get the time at the bot's location")] + public async Task GetTime() + { + await ReplyAsync($"{DateTime.Now:g}"); + } + + + [Command] + [Summary("get the time for a user (if they have a location saved)")] + public async Task GetUserLocationTime([Summary("user to get time for")] IUser user) + { + using (var db = new DotbotDbContext()) + { + if (db.UserLocations.Any(u => u.Id == user.Id)) + { + var tz = db.UserLocations.Find(user.Id).TimeZone; + var dt = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, tz); + await ReplyAsync($"the time for {user.Mention} is {dt:g}"); + } + else + { + await ReplyAsync($"{user.Mention} does not have a saved location"); + } + } + } + + [Command("save")] + [Summary("save your location")] + public async Task SaveUserLocation([Remainder] [Summary("the location")] string location) + { + using (var db = new DotbotDbContext()) + { + if (db.UserLocations.Any(u => u.Id == Context.User.Id)) + { + var loc = db.UserLocations.Find(Context.User.Id); + // TODO: update location for user + } + else + { + // TODO: lookup location and save + db.UserLocations.Add(new UserLocation + { + Id = Context.User.Id, + // TODO: City = jsonresult + }); + } + db.SaveChanges(); + await ReplyAsync($"your location has been updated to `City`"); + } + } + + } +} diff --git a/Commands/Weather.cs b/Commands/Weather.cs new file mode 100644 index 0000000..a0f223e --- /dev/null +++ b/Commands/Weather.cs @@ -0,0 +1,9 @@ +using Discord.Commands; + +namespace dotbot.Commands +{ + public class Weather : ModuleBase + { + + } +} diff --git a/Core/DotbotDbContext.cs b/Core/DotbotDbContext.cs index aab36cd..610b31d 100644 --- a/Core/DotbotDbContext.cs +++ b/Core/DotbotDbContext.cs @@ -6,6 +6,7 @@ namespace dotbot.Core { public DbSet Defs { get; set; } public DbSet Emails { get; set; } + public DbSet UserLocations { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/Core/UserLocation.cs b/Core/UserLocation.cs new file mode 100644 index 0000000..6f89ce6 --- /dev/null +++ b/Core/UserLocation.cs @@ -0,0 +1,12 @@ +namespace dotbot.Core +{ + public class UserLocation + { + public ulong Id { get; set; } + public float Lat { get; set; } + public float Lng { get; set; } + public string City { get; set; } + public int CityId { get; set; } + public string TimeZone { get; set; } + } +} diff --git a/Migrations/20171206050609_UserLocations.Designer.cs b/Migrations/20171206050609_UserLocations.Designer.cs new file mode 100644 index 0000000..8a07969 --- /dev/null +++ b/Migrations/20171206050609_UserLocations.Designer.cs @@ -0,0 +1,68 @@ +// +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("20171206050609_UserLocations")] + partial class UserLocations + { + 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.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/20171206050609_UserLocations.cs b/Migrations/20171206050609_UserLocations.cs new file mode 100644 index 0000000..d0c48b8 --- /dev/null +++ b/Migrations/20171206050609_UserLocations.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using System; +using System.Collections.Generic; + +namespace dotbot.Migrations +{ + public partial class UserLocations : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "UserLocations", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Sqlite:Autoincrement", true), + City = table.Column(nullable: true), + CityId = table.Column(nullable: false), + Lat = table.Column(nullable: false), + Lng = table.Column(nullable: false), + TimeZone = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserLocations", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "UserLocations"); + } + } +} diff --git a/Migrations/DotbotDbContextModelSnapshot.cs b/Migrations/DotbotDbContextModelSnapshot.cs index babb6b6..8dcfe76 100644 --- a/Migrations/DotbotDbContextModelSnapshot.cs +++ b/Migrations/DotbotDbContextModelSnapshot.cs @@ -41,6 +41,26 @@ namespace dotbot.Migrations b.ToTable("Emails"); }); + + 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/Services/CommandHandlerService.cs b/Services/CommandHandlerService.cs index 8adfab2..51e3e23 100644 --- a/Services/CommandHandlerService.cs +++ b/Services/CommandHandlerService.cs @@ -42,24 +42,23 @@ namespace dotbot.Services if (msg.HasStringPrefix(_config["prefix"], ref argPos) || msg.HasMentionPrefix(_discord.CurrentUser, ref argPos)) { var result = await _commands.ExecuteAsync(context, argPos, _provider); // Execute the command + if (result.IsSuccess) return; - if (!result.IsSuccess) // If not successful, reply with the error. + if (msg.HasStringPrefix(_config["prefix"], ref argPos)) { - if (msg.HasStringPrefix(_config["prefix"], ref argPos)) + using (var db = new DotbotDbContext()) { - using (var db = new DotbotDbContext()) + var key = msg.Content.Substring(_config["prefix"].Length); + if (db.Defs.Any(d => d.Id == key)) { - var key = msg.Content.Substring(_config["prefix"].Length); - if (db.Defs.Any(d => d.Id == key)) - { - await context.Channel.SendMessageAsync($"**{key}**: {db.Defs.Find(key).Def}"); - return; - } + await context.Channel.SendMessageAsync($"**{key}**: {db.Defs.Find(key).Def}"); + return; } } - if (!result.ToString().Contains("UnknownCommand")) - await context.Channel.SendMessageAsync(result.ToString()); } + if (!result.ToString().Contains("UnknownCommand")) + await context.Channel.SendMessageAsync(result.ToString()); + } }