Files
telegram-bot/modules/ChatManagement/Handler/Reputation.cs
T

149 lines
4.5 KiB
C#
Raw Normal View History

2021-12-27 22:53:13 +02:00
#nullable enable
2021-12-28 21:00:23 +02:00
using System.Collections.Generic;
2021-12-27 22:53:13 +02:00
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
2022-01-04 17:45:06 +02:00
using BotFramework.Utils;
2021-12-27 22:53:13 +02:00
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
2022-01-04 17:45:06 +02:00
using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
2021-12-28 21:00:23 +02:00
using Telegram.Bot.Types;
2021-12-27 22:53:13 +02:00
namespace West.TelegramBot.ChatManagement.Handler
{
public class Reputation : ManagementHandler
{
2022-01-04 17:45:06 +02:00
private readonly UserService _userService;
private readonly ILogger _logger;
2021-12-28 21:00:23 +02:00
private static readonly List<string> RepUpChars = new()
{
"+",
"👍",
"👍🏼",
"👍🏽",
"👍🏾",
"👍🏿",
"👍🏻"
};
private static readonly List<string> RepDownChars = new()
{
"-",
"👎",
"👎🏼",
"👎🏽",
"👎🏾",
"👎🏿",
"👎🏻"
};
2022-01-04 17:45:06 +02:00
public Reputation(CoreContext db, UserService userService, ILogger<Reputation> logger) : base(db)
{
_userService = userService;
_logger = logger;
}
2021-12-27 22:53:13 +02:00
[Command("whois", CommandParseMode.Both)]
public async Task HandleWhois()
{
var user = RepliedUser ?? From;
2021-12-28 21:00:23 +02:00
var message = $"{user.ToHtml()}\n";
2021-12-27 22:53:13 +02:00
if (user.IsBot)
{
message += "<code>Бот</code>";
}
else
{
2021-12-28 21:00:23 +02:00
message += $"<code>Репутация: {await GetUserReputation(user)}\n";
message += $"Предупреждения: {(await GetWarnOption(user)).GetValue<int>()}</code>";
2021-12-27 22:53:13 +02:00
}
await Reply(message);
}
2021-12-28 21:00:23 +02:00
[Message(InChat.Public, MessageFlag.HasText | MessageFlag.HasSticker)]
public async Task HandleReputation()
{
var text = Message?.Text;
if (string.IsNullOrEmpty(text))
{
text = Message?.Sticker?.Emoji;
}
if (text == null)
{
return;
}
var receiver = RepliedUser;
var canChangeRep = receiver is { IsBot: false } && !From.IsBot && receiver.Id != From.Id;
if (canChangeRep && (RepUpChars.Contains(text) || RepDownChars.Contains(text)))
{
var repOption = await GetReputationOption(receiver!);
var repCount = repOption.GetValue<int>();
string action;
if (RepUpChars.Contains(text))
{
repCount++;
action = "увеличил";
}
else
{
repCount--;
action = "уменьшил";
}
repOption.SetValue(repCount);
Db.MarkAsModified(repOption);
await Reply($"{From.ToHtml()}<code>({await GetUserReputation(From)}) " +
$"{action} репутацию </code>{receiver.ToHtml()}<code>({repCount})</code>");
}
}
2022-01-04 17:45:06 +02:00
[ParametrizedCommand(InChat.Public, "setrep", CommandParseMode.Both)]
public async Task HandleSetRep(int reputation)
{
if (!_userService.IsUserSuper(From))
{
_logger.LogInformation("{User} attempted to call /setrep in {Chat}", From, Chat.Title);
2022-01-04 17:45:06 +02:00
return;
}
if (RepliedUser == null || RepliedUser.IsBot)
{
return;
}
var opt = await GetReputationOption(RepliedUser);
opt.SetValue(reputation);
Db.MarkAsModified(opt);
2021-12-28 21:00:23 +02:00
2022-01-04 17:45:06 +02:00
await Reply(
new HtmlString()
.Code("Пользователю ")
.UserMention(RepliedUser)
.Code($" установлена репутация: {reputation}")
.ToString()
);
}
2021-12-28 21:00:23 +02:00
private async Task<int> GetUserReputation(User user)
{
return (await GetReputationOption(user)).GetValue<int>();
}
private async Task<BotUserValue> GetReputationOption(User user)
{
return await Db.UserValues.FindOrCreateOption(Chat.Id, user.Id, "reputation");
}
2021-12-27 22:53:13 +02:00
}
}