Files

161 lines
4.9 KiB
C#
Raw Permalink Normal View History

using BotFramework.Attributes;
using BotFramework.Enums;
using System.Threading.Tasks;
using System;
2022-12-14 12:31:53 +02:00
using System.Linq;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service;
2022-12-14 12:31:53 +02:00
using Microsoft.Extensions.Logging;
using Telegram.Bot;
2023-01-29 10:05:44 +03:00
using Kruzya.TelegramBot.Core.AutoDelete;
using System.Collections.Concurrent;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types;
2023-07-20 21:22:35 +03:00
using Kruzya.TelegramBot.Core.Handler;
2023-12-13 00:23:56 +02:00
using System.Collections.Generic;
using System.Security.Cryptography;
namespace West.Entertainment.Handler;
public class Roll : CommonHandler
{
2022-12-14 12:31:53 +02:00
private readonly ILogger<Roll> _logger;
private readonly IReputation _reputation;
2022-12-14 12:31:53 +02:00
private readonly UserService _userService;
2023-01-29 10:05:44 +03:00
private readonly ConcurrentBag<DeleteRequest> _requests;
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[Command("roll", CommandParseMode.Both)]
public async Task HandleRoll()
{
var cdOption = await Db.UserValues.FindOrCreateOption(Chat.Id, From.Id, "rollNextUse");
var cd = cdOption.GetValue<DateTime>();
_logger.LogInformation("[{Datetime}] {User} used /roll in {Chat}. Cooldown is: {Cooldown}",
DateTime.Now,
From.ToLog(),
Chat.ToLog(),
cd
);
2023-01-29 10:11:55 +03:00
// Drop user message.
_requests.Add(new DeleteRequest
{
ChatId = Chat.Id,
DeleteAt = DateTime.UtcNow.AddSeconds(20),
2023-07-29 15:03:45 +03:00
MessageId = Message!.MessageId
2023-01-29 10:11:55 +03:00
});
if (cd > DateTime.Now)
{
await Reply($"Следующее использование через {cd - DateTime.Now:hh\\:mm\\:ss}.");
return;
}
var newRep = RandomNumberGenerator.GetInt32(-20, 80);
await Reply($"Вы получили {newRep} кармы.");
await _reputation.IncrementReputationAsync(Chat, From, newRep);
_logger.LogInformation("[{Datetime}] Added {Count} reputation to {User} in {Chat} from /roll",
DateTime.Now,
newRep,
From.ToLog(),
Chat.ToLog()
);
cdOption.SetValue(DateTime.Now.AddDays(1));
Db.AddOrUpdate(cdOption);
_logger.LogInformation("[{Datetime}] Updated /roll cooldown for {User} in {Chat} to {Cooldown}",
DateTime.Now,
From.ToLog(),
Chat.ToLog(),
cdOption.GetValue<DateTime>()
);
}
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[Command("reset_roll", CommandParseMode.Both)]
2022-12-14 12:31:53 +02:00
public async Task HandleResetRoll()
{
if (!_userService.IsUserSuper(From))
{
2022-12-14 12:58:12 +02:00
_logger.LogWarning("{User} attempted to use super-user command /reset_roll in {Chat}",
From.ToLog(),
Chat.ToLog()
);
2022-12-14 12:31:53 +02:00
return;
}
var cooldownList = Db.UserValues
.Where(q => q.BotUser.ChatId == Chat.Id && q.Name == "rollNextUse");
Db.UserValues.RemoveRange(cooldownList);
await Db.SaveChangesAsync();
_logger.LogInformation("/roll cooldowns have been reset for {Chat} by {User}", Chat.ToLog(), From.ToLog());
2022-12-14 12:31:53 +02:00
await Reply($"Кулдаун /roll сброшен для чата \"{Chat.Title}\".");
}
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[ParametrizedCommand("rng_test", CommandParseMode.Both)]
public async Task HandleRngTest(int minValue, int maxValue)
{
if (!_userService.IsUserSuper(From))
{
_logger.LogWarning("{User} attempted to use super-user command /rng_test in {Chat}",
From.ToLog(),
Chat.ToLog()
);
return;
}
var positive = 0;
var negative = 0;
2023-12-13 00:23:56 +02:00
var numbers = new List<int>();
for (var i = 0; i < 100; i++)
{
var number = RandomNumberGenerator.GetInt32(minValue, maxValue);
2023-12-13 00:23:56 +02:00
numbers.Add(number);
if (number > 0)
{
positive++;
}
else
{
negative++;
}
}
2023-12-13 00:23:56 +02:00
await Bot.SendTextMessageAsync(Chat, $"{string.Join(", ", numbers)}\n\nPositive: {positive}.\nNegative: {negative}");
}
2023-07-29 01:50:28 +03:00
2023-01-29 10:05:44 +03:00
protected override async Task<Message> Reply(string text)
{
// TODO: move feature with autocleaning to Core CommonHandler.
var message = await Bot.SendTextMessageAsync(Chat.Id, text,
replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html);
_requests.Add(new DeleteRequest
{
ChatId = Chat.Id,
DeleteAt = DateTime.UtcNow.AddSeconds(20),
MessageId = message.MessageId
});
return message;
}
public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger<Roll> logger, ConcurrentBag<DeleteRequest> requests) : base(db)
{
_reputation = reputation;
2022-12-14 12:31:53 +02:00
_userService = userService;
_logger = logger;
2023-01-29 10:05:44 +03:00
_requests = requests;
}
}