using BotFramework.Attributes; using BotFramework.Enums; using System.Threading.Tasks; using System; using System.Linq; using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Service; using Microsoft.Extensions.Logging; namespace West.Entertainment.Handler; public class Roll : CommonHandler { private readonly ILogger _logger; private readonly IReputation _reputation; private readonly UserService _userService; [Command(InChat.Public, "roll", CommandParseMode.Both)] public async Task HandleRoll() { var cdOption = await Db.UserValues.FindOrCreateOption(Chat.Id, From.Id, "rollNextUse"); var cd = cdOption.GetValue(); if (cd > DateTime.Now) { await Reply($"Следующее использование через {cd - DateTime.Now:hh\\:mm\\:ss}."); return; } var newRep = new Random().Next(-50, 50); await Reply($"Вы получили {newRep} кармы."); await _reputation.IncrementReputationAsync(Chat, From, newRep); cdOption.SetValue(DateTime.Now.AddDays(1)); Db.AddOrUpdate(cdOption); } [Command(InChat.Public, "reset_roll", CommandParseMode.Both)] public async Task HandleResetRoll() { if (!_userService.IsUserSuper(From)) { _logger.LogWarning("{User} attempted to use super-user command /reset_roll in {Chat}", From.ToLog(), Chat.ToLog() ); return; } var cooldownList = Db.UserValues .Where(q => q.BotUser.ChatId == Chat.Id && q.Name == "rollNextUse"); Db.UserValues.RemoveRange(cooldownList); await Db.SaveChangesAsync(); await Reply($"Кулдаун /roll сброшен для чата \"{Chat.Title}\"."); } public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger logger) : base(db) { _reputation = reputation; _userService = userService; _logger = logger; } }