mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
status command
This commit is contained in:
parent
2fbfde551b
commit
0025be1474
50
JetKarmaBot/Commands/StatusCommand.cs
Normal file
50
JetKarmaBot/Commands/StatusCommand.cs
Normal 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; }
|
||||
|
||||
}
|
||||
}
|
||||
@ -62,6 +62,7 @@ namespace JetKarmaBot
|
||||
Commands = new ChatCommandRouter(Me);
|
||||
Commands.Add(c.ResolveObject(new StartCommand()));
|
||||
Commands.Add(c.ResolveObject(new AwardCommand(Me)));
|
||||
Commands.Add(c.ResolveObject(new StatusCommand()));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -46,7 +46,7 @@ namespace JetKarmaBot
|
||||
public IReadOnlyDictionary<byte, AwardType> AwardTypes => m_AwardTypes;
|
||||
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?>
|
||||
(
|
||||
@ -55,6 +55,27 @@ namespace JetKarmaBot
|
||||
) ?? 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)
|
||||
=> AwardTypesByCommandName.GetOrDefault(name)?.AwardTypeId ?? DefaultAwardTypeId;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user