diff --git a/Core/Extensions/UserExtension.cs b/Core/Extensions/UserExtension.cs index 2455952..800a1f9 100644 --- a/Core/Extensions/UserExtension.cs +++ b/Core/Extensions/UserExtension.cs @@ -5,7 +5,7 @@ namespace Kruzya.TelegramBot.Core.Extensions { public static class UserExtension { - public static string ToHtml(this User user, bool byUsername = true) + public static string ToHtml(this User user, bool byUsername = false) { var text = new StringBuilder(); text.Append($""); diff --git a/modules/ChatManagement/Handler/ManagementHandler.cs b/modules/ChatManagement/Handler/ManagementHandler.cs index 0fadded..dc39147 100644 --- a/modules/ChatManagement/Handler/ManagementHandler.cs +++ b/modules/ChatManagement/Handler/ManagementHandler.cs @@ -3,7 +3,6 @@ using System; using System.Threading.Tasks; using BotFramework; -using JetBrains.Annotations; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Telegram.Bot; @@ -46,7 +45,7 @@ namespace West.TelegramBot.ChatManagement.Handler { await Bot.SendTextMessageAsync( Chat.Id, - $"{From.ToHtml(false)} заблокировал {user.ToHtml(false)} на 7 дней", + $"{From.ToHtml()} заблокировал {user.ToHtml()} на 7 дней", disableNotification: true, parseMode: ParseMode.Html); } } diff --git a/modules/ChatManagement/Handler/Reputation.cs b/modules/ChatManagement/Handler/Reputation.cs index 60bfe5f..e9d34a6 100644 --- a/modules/ChatManagement/Handler/Reputation.cs +++ b/modules/ChatManagement/Handler/Reputation.cs @@ -1,32 +1,110 @@ #nullable enable +using System.Collections.Generic; using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; +using Telegram.Bot.Types; namespace West.TelegramBot.ChatManagement.Handler { public class Reputation : ManagementHandler { - public Reputation(CoreContext db) : base(db) {} + private static readonly List RepUpChars = new() + { + "+", + "👍", + "👍🏼", + "👍🏽", + "👍🏾", + "👍🏿", + "👍🏻" + }; + + private static readonly List RepDownChars = new() + { + "-", + "👎", + "👎🏼", + "👎🏽", + "👎🏾", + "👎🏿", + "👎🏻" + }; + + public Reputation(CoreContext db) : base(db) { } [Command("whois", CommandParseMode.Both)] public async Task HandleWhois() { var user = RepliedUser ?? From; - var message = $"{user.ToHtml(false)}\n"; + var message = $"{user.ToHtml()}\n"; if (user.IsBot) { message += "Бот"; } else { - message += $"Предупреждения: {(await GetWarnOption(user)).GetValue()}"; + message += $"Репутация: {await GetUserReputation(user)}\n"; + message += $"Предупреждения: {(await GetWarnOption(user)).GetValue()}"; } await Reply(message); } + + [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(); + + string action; + + if (RepUpChars.Contains(text)) + { + repCount++; + action = "увеличил"; + } + else + { + repCount--; + action = "уменьшил"; + } + + repOption.SetValue(repCount); + Db.MarkAsModified(repOption); + + await Reply($"{From.ToHtml()}({await GetUserReputation(From)}) " + + $"{action} репутацию {receiver.ToHtml()}({repCount})"); + } + } + + private async Task GetUserReputation(User user) + { + return (await GetReputationOption(user)).GetValue(); + } + + private async Task GetReputationOption(User user) + { + return await Db.UserValues.FindOrCreateOption(Chat.Id, user.Id, "reputation"); + } } } \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Warn.cs b/modules/ChatManagement/Handler/Warn.cs index ac66bde..ccc28aa 100644 --- a/modules/ChatManagement/Handler/Warn.cs +++ b/modules/ChatManagement/Handler/Warn.cs @@ -26,7 +26,7 @@ namespace West.TelegramBot.ChatManagement.Handler warnOption.SetValue(warnCount); Db.MarkAsModified(warnOption); - await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml(false)}, Вам выдано предупреждение! " + + await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml()}, Вам выдано предупреждение! " + $"Текущее кол-во: {warnCount}/3", disableNotification: true, parseMode: ParseMode.Html); @@ -49,7 +49,7 @@ namespace West.TelegramBot.ChatManagement.Handler Db.MarkAsModified(warnOption); await Bot.SendTextMessageAsync(Chat.Id, - $"{RepliedUser.ToHtml(false)}, с Вас снято предупреждение! " + + $"{RepliedUser.ToHtml()}, с Вас снято предупреждение! " + $"Текущее кол-во: {warnCount}/3", disableNotification: true, parseMode: ParseMode.Html); diff --git a/modules/CombotAntiSpam/UserJoinHandler.cs b/modules/CombotAntiSpam/UserJoinHandler.cs index ff66722..f81e170 100644 --- a/modules/CombotAntiSpam/UserJoinHandler.cs +++ b/modules/CombotAntiSpam/UserJoinHandler.cs @@ -45,7 +45,7 @@ namespace Kruzya.TelegramBot.CombotAntiSpam var messageText = new StringBuilder(); messageText.Append($"⚠️ Combot Anti-Spam\n\n"); - messageText.Append($"User {member.ToHtml()} is spammer.\n"); + messageText.Append($"User {member.ToHtml(true)} is spammer.\n"); messageText.Append( $"More details you can find on CAS site."); diff --git a/modules/UrlLimitations/MessageHandler.cs b/modules/UrlLimitations/MessageHandler.cs index b7c307d..e41fee6 100644 --- a/modules/UrlLimitations/MessageHandler.cs +++ b/modules/UrlLimitations/MessageHandler.cs @@ -52,7 +52,7 @@ namespace Kruzya.TelegramBot.UrlLimitations Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)), Bot.DeleteMessageAsync(targetChat, message.MessageId), Bot.SendTextMessageAsync(targetChat, - $"Member {From.ToHtml()} has been banned.\nReason:
Spam suspicion
", ParseMode.Html) + $"Member {From.ToHtml(true)} has been banned.\nReason:
Spam suspicion
", ParseMode.Html) }); } }