start work on time and weather

This commit is contained in:
Ben Harris 2017-12-06 00:28:58 -05:00
parent 77ea9dae53
commit 043f20a42d
8 changed files with 221 additions and 11 deletions

66
Commands/Time.cs Normal file
View File

@ -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<SocketCommandContext>
{
[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`");
}
}
}
}

9
Commands/Weather.cs Normal file
View File

@ -0,0 +1,9 @@
using Discord.Commands;
namespace dotbot.Commands
{
public class Weather : ModuleBase<SocketCommandContext>
{
}
}

View File

@ -6,6 +6,7 @@ namespace dotbot.Core
{
public DbSet<Definition> Defs { get; set; }
public DbSet<Email> Emails { get; set; }
public DbSet<UserLocation> UserLocations { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

12
Core/UserLocation.cs Normal file
View File

@ -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; }
}
}

View File

@ -0,0 +1,68 @@
// <auto-generated />
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<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Def");
b.HasKey("Id");
b.ToTable("Defs");
});
modelBuilder.Entity("dotbot.Core.Email", b =>
{
b.Property<ulong>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("EmailAddress");
b.HasKey("Id");
b.ToTable("Emails");
});
modelBuilder.Entity("dotbot.Core.UserLocation", b =>
{
b.Property<ulong>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("City");
b.Property<int>("CityId");
b.Property<float>("Lat");
b.Property<float>("Lng");
b.Property<string>("TimeZone");
b.HasKey("Id");
b.ToTable("UserLocations");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -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<ulong>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
City = table.Column<string>(nullable: true),
CityId = table.Column<int>(nullable: false),
Lat = table.Column<float>(nullable: false),
Lng = table.Column<float>(nullable: false),
TimeZone = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_UserLocations", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "UserLocations");
}
}
}

View File

@ -41,6 +41,26 @@ namespace dotbot.Migrations
b.ToTable("Emails");
});
modelBuilder.Entity("dotbot.Core.UserLocation", b =>
{
b.Property<ulong>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("City");
b.Property<int>("CityId");
b.Property<float>("Lat");
b.Property<float>("Lng");
b.Property<string>("TimeZone");
b.HasKey("Id");
b.ToTable("UserLocations");
});
#pragma warning restore 612, 618
}
}

View File

@ -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());
}
}