bot: add birthday command

This commit is contained in:
Ske
2019-05-13 22:44:49 +02:00
parent b42e052fee
commit 62dc2ce78e
7 changed files with 124 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using Npgsql;
namespace PluralKit.Bot
@@ -34,6 +35,14 @@ namespace PluralKit.Bot
SqlMapper.AddTypeHandler<ulong>(new UlongEncodeAsLongHandler());
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
// Also, use NodaTime. it's good.
NpgsqlConnection.GlobalTypeMapper.UseNodaTime();
// With the thing we add above, Npgsql already handles NodaTime integration
// This makes Dapper confused since it thinks it has to convert it anyway and doesn't understand the types
// So we add a custom type handler that literally just passes the type through to Npgsql
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<LocalDate>());
using (var services = BuildServiceProvider())
{
Console.WriteLine("- Connecting to database...");

View File

@@ -2,6 +2,7 @@ using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord.Commands;
using NodaTime;
namespace PluralKit.Bot.Commands
{
@@ -118,11 +119,31 @@ namespace PluralKit.Bot.Commands
await Context.Channel.SendMessageAsync($"{Emojis.Success} Member color {(color == null ? "cleared" : "changed")}.");
}
[Command("birthday")]
[Alias("birthdate", "bday", "cakeday", "bdate")]
[Remarks("member <member> birthday <birthday>")]
[MustPassOwnMember]
public async Task MemberBirthday([Remainder] string birthday = null)
{
LocalDate? date = null;
if (birthday != null)
{
date = PluralKit.Utils.ParseDate(birthday, true);
if (date == null) throw Errors.BirthdayParseError(birthday);
}
ContextEntity.Birthday = date;
await Members.Save(ContextEntity);
await Context.Channel.SendMessageAsync($"{Emojis.Success} Member birthdate {(date == null ? "cleared" : $"changed to {ContextEntity.BirthdayString}")}.");
}
[Command]
[Alias("view", "show", "info")]
[Remarks("member")]
public async Task ViewMember(PKMember member)
{
var system = await Systems.GetById(member.Id);
var system = await Systems.GetById(member.System);
await Context.Channel.SendMessageAsync(embed: await Embeds.CreateMemberEmbed(system, member));
}

View File

@@ -14,6 +14,7 @@ namespace PluralKit.Bot {
public static PKError MemberNameTooLongError(int length) => new PKError($"Member name too long ({length}/{Limits.MaxMemberNameLength} characters).");
public static PKError MemberPronounsTooLongError(int length) => new PKError($"Member pronouns too long ({length}/{Limits.MaxMemberNameLength} characters).");
public static PKError InvalidColorError(string color) => new PKError($"{color} is not a valid color. Color must be in hex format (eg. #ff0000).");
public static PKError InvalidColorError(string color) => new PKError($"\"{color}\" is not a valid color. Color must be in hex format (eg. #ff0000).");
public static PKError BirthdayParseError(string birthday) => new PKError($"\"{birthday}\" could not be parsed as a valid date. Try a format like \"2016-12-24\" or \"May 3 1996\".");
}
}

View File

@@ -9,6 +9,7 @@ using Discord.Commands;
using Discord.Commands.Builders;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
namespace PluralKit.Bot
{
@@ -32,6 +33,19 @@ namespace PluralKit.Bot
}
}
class PassthroughTypeHandler<T> : SqlMapper.TypeHandler<T>
{
public override void SetValue(IDbDataParameter parameter, T value)
{
parameter.Value = value;
}
public override T Parse(object value)
{
return (T) value;
}
}
class PKSystemTypeReader : TypeReader
{
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)