status command

This commit is contained in:
jetsparrow 2018-12-19 23:55:58 +03:00
parent ab90a15c8c
commit 4355ae4058
3 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,50 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Perfusion;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types;
namespace JetKarmaBot.Commands
{
class StatusCommand : IChatCommand
{
public IReadOnlyCollection<string> Names => new[] { "status" };
public bool Execute(CommandString cmd, MessageEventArgs args)
{
var asker = args.Message.From;
var awardTypeName = cmd.Parameters.FirstOrDefault();
string response;
if (string.IsNullOrWhiteSpace(awardTypeName))
{
var awards = Db.CountAllUserAwards(asker.Id);
response = "Your badges report:\n"
+ string.Join("\n", awards.Select(a => $" - {Db.AwardTypes[a.AwardTypeId].Symbol} {a.Amount}"));
}
else
{
var awardTypeId = Db.GetAwardTypeId(cmd.Parameters.FirstOrDefault());
var awardType = Db.AwardTypes[awardTypeId];
response = $"You are at {Db.CountUserAwards(asker.Id, awardTypeId)}{awardType.Symbol} now.";
}
Client.SendTextMessageAsync(
args.Message.Chat.Id,
response,
replyToMessageId: args.Message.MessageId);
return true;
}
[Inject(true)] Db Db { get; set; }
[Inject(true)] TelegramBotClient Client { get; set; }
}
}

View File

@ -62,6 +62,7 @@ namespace JetKarmaBot
Commands = new ChatCommandRouter(Me); Commands = new ChatCommandRouter(Me);
Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new StartCommand()));
Commands.Add(c.ResolveObject(new AwardCommand(Me))); Commands.Add(c.ResolveObject(new AwardCommand(Me)));
Commands.Add(c.ResolveObject(new StatusCommand()));
} }
#endregion #endregion

View File

@ -46,7 +46,7 @@ namespace JetKarmaBot
public IReadOnlyDictionary<byte, AwardType> AwardTypes => m_AwardTypes; public IReadOnlyDictionary<byte, AwardType> AwardTypes => m_AwardTypes;
public IReadOnlyDictionary<string, AwardType> AwardTypesByCommandName { get; private set; } public IReadOnlyDictionary<string, AwardType> AwardTypesByCommandName { get; private set; }
public int CountAwards(long userId, byte awardTypeId) public int CountUserAwards(long userId, byte awardTypeId)
{ {
return Conn.QuerySingle<int?> return Conn.QuerySingle<int?>
( (
@ -55,6 +55,27 @@ namespace JetKarmaBot
) ?? 0; ) ?? 0;
} }
public struct UserAwardsReport
{
public int Amount { get; private set; }
public byte AwardTypeId { get; private set; }
}
public IEnumerable<UserAwardsReport> CountAllUserAwards(long userId)
{
return Conn.Query<UserAwardsReport>
(
@"SELECT SUM(amount) AS amount, t.awardtypeid
FROM award a
JOIN awardtype t on a.awardtypeid = t.awardtypeid
WHERE toid = @userId
GROUP BY awardtypeid;",
new { userId }
);
}
public byte GetAwardTypeId(string name) public byte GetAwardTypeId(string name)
=> AwardTypesByCommandName.GetOrDefault(name)?.AwardTypeId ?? DefaultAwardTypeId; => AwardTypesByCommandName.GetOrDefault(name)?.AwardTypeId ?? DefaultAwardTypeId;