diff --git a/modules/ChatManagement/ChatManagement.csproj b/modules/ChatManagement/ChatManagement.csproj index 86601c4..dc8f81f 100644 --- a/modules/ChatManagement/ChatManagement.csproj +++ b/modules/ChatManagement/ChatManagement.csproj @@ -9,5 +9,8 @@ - + + + + diff --git a/modules/ChatManagement/Handler/Ban.cs b/modules/ChatManagement/Handler/Ban.cs index 071f7cb..f90d21d 100644 --- a/modules/ChatManagement/Handler/Ban.cs +++ b/modules/ChatManagement/Handler/Ban.cs @@ -1,6 +1,9 @@ -using System.Threading.Tasks; +#nullable enable + +using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; using Telegram.Bot; using Telegram.Bot.Types; @@ -8,12 +11,14 @@ namespace West.TelegramBot.ChatManagement.Handler { public class Ban : ManagementHandler { + public Ban(CoreContext db) : base(db) {} + [Command("ban", CommandParseMode.Both)] public async Task HandleBan() { if (!await CanPunish()) return; - await BanMember(GetVictim()); + await BanMember(RepliedUser!); } [Command("unban", CommandParseMode.Both)] @@ -21,7 +26,7 @@ namespace West.TelegramBot.ChatManagement.Handler { if (!await CanPunish()) return; - await Bot.RestrictChatMemberAsync(Chat.Id, GetVictim().Id, + await Bot.RestrictChatMemberAsync(Chat.Id, RepliedUser!.Id, new ChatPermissions { CanSendMessages = true, diff --git a/modules/ChatManagement/Handler/ManagementHandler.cs b/modules/ChatManagement/Handler/ManagementHandler.cs index 2254a61..0fadded 100644 --- a/modules/ChatManagement/Handler/ManagementHandler.cs +++ b/modules/ChatManagement/Handler/ManagementHandler.cs @@ -1,6 +1,10 @@ -using System; +#nullable enable + +using System; using System.Threading.Tasks; using BotFramework; +using JetBrains.Annotations; +using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Telegram.Bot; using Telegram.Bot.Types; @@ -9,21 +13,34 @@ using West.TelegramBot.ChatManagement.Extensions; namespace West.TelegramBot.ChatManagement.Handler { - public class ManagementHandler : BotEventHandler + public abstract class ManagementHandler : BotEventHandler { - protected User GetVictim() - { - return RawUpdate.Message?.ReplyToMessage?.From; - } + protected readonly CoreContext Db; + + protected Message? Message => RawUpdate.Message; + + protected User? RepliedUser => Message?.ReplyToMessage?.From; - protected async Task CanPunish() + protected ManagementHandler(CoreContext db) { - return GetVictim() != null && await Bot.CanPunishMember(Chat, From, GetVictim()); + Db = db; } + protected async Task Reply(string text) + { + return await Bot.SendTextMessageAsync(Chat.Id, text, + replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html); + } + + protected async Task CanPunish() + { + return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser); + } + + #region Bans protected async Task BanMember(User user, DateTime? banEnd = null, bool sendMessage = true) { - await BanMember(user.Id, banEnd, sendMessage); + await BanMember(user.Id, banEnd); if (sendMessage) { @@ -34,7 +51,7 @@ namespace West.TelegramBot.ChatManagement.Handler } } - protected async Task BanMember(long userId, DateTime? banEnd = null, bool sendMessage = true) + protected async Task BanMember(long userId, DateTime? banEnd = null) { await Bot.RestrictChatMemberAsync( Chat.Id, @@ -46,5 +63,11 @@ namespace West.TelegramBot.ChatManagement.Handler banEnd ?? DateTime.Now.AddDays(7) ); } + #endregion + + protected async Task GetWarnOption(User user) + { + return await Db.UserValues.FindOrCreateOption(Chat.Id, user.Id, "warnCount"); + } } } \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Reputation.cs b/modules/ChatManagement/Handler/Reputation.cs new file mode 100644 index 0000000..60bfe5f --- /dev/null +++ b/modules/ChatManagement/Handler/Reputation.cs @@ -0,0 +1,32 @@ +#nullable enable + +using System.Threading.Tasks; +using BotFramework.Attributes; +using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; +using Kruzya.TelegramBot.Core.Extensions; + +namespace West.TelegramBot.ChatManagement.Handler +{ + public class Reputation : ManagementHandler + { + public Reputation(CoreContext db) : base(db) {} + + [Command("whois", CommandParseMode.Both)] + public async Task HandleWhois() + { + var user = RepliedUser ?? From; + var message = $"{user.ToHtml(false)}\n"; + if (user.IsBot) + { + message += "Бот"; + } + else + { + message += $"Предупреждения: {(await GetWarnOption(user)).GetValue()}"; + } + + await Reply(message); + } + } +} \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Warn.cs b/modules/ChatManagement/Handler/Warn.cs index e71887e..ac66bde 100644 --- a/modules/ChatManagement/Handler/Warn.cs +++ b/modules/ChatManagement/Handler/Warn.cs @@ -1,4 +1,6 @@ -using System; +#nullable enable + +using System; using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; @@ -9,25 +11,20 @@ using Telegram.Bot.Types.Enums; namespace West.TelegramBot.ChatManagement.Handler { - public class Command : ManagementHandler + public class Warn : ManagementHandler { - private readonly CoreContext _db; - - public Command(CoreContext db) - { - _db = db; - } - + public Warn(CoreContext db) : base(db) {} + [Command("warn", CommandParseMode.Both)] public async Task HandleWarn() { if (!await CanPunish()) return; - var warnUser = GetVictim(); - var warnOption = await GetWarnOption(); - var warnCount = warnOption.GetValue() + 1; + var warnUser = RepliedUser!; + var warnOption = await GetWarnOption(warnUser); + var warnCount = warnOption!.GetValue() + 1; warnOption.SetValue(warnCount); - _db.MarkAsModified(warnOption); + Db.MarkAsModified(warnOption); await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml(false)}, Вам выдано предупреждение! " + $"Текущее кол-во: {warnCount}/3", @@ -46,21 +43,16 @@ namespace West.TelegramBot.ChatManagement.Handler { if (!await CanPunish()) return; - var warnOption = await GetWarnOption(); + var warnOption = await GetWarnOption(RepliedUser!); var warnCount = Math.Max(warnOption.GetValue() - 1, 0); warnOption.SetValue(warnCount); - _db.MarkAsModified(warnOption); + Db.MarkAsModified(warnOption); await Bot.SendTextMessageAsync(Chat.Id, - $"{GetVictim().ToHtml(false)}, с Вас снято предупреждение! " + + $"{RepliedUser.ToHtml(false)}, с Вас снято предупреждение! " + $"Текущее кол-во: {warnCount}/3", disableNotification: true, parseMode: ParseMode.Html); } - - private async Task GetWarnOption() - { - return await _db.UserValues.FindOrCreateOption(Chat.Id, GetVictim().Id, "warnCount"); - } } } \ No newline at end of file diff --git a/modules/RichSiteSummary/RichSiteSummary.csproj b/modules/RichSiteSummary/RichSiteSummary.csproj index 4eab7ec..c82745b 100644 --- a/modules/RichSiteSummary/RichSiteSummary.csproj +++ b/modules/RichSiteSummary/RichSiteSummary.csproj @@ -13,5 +13,8 @@ - + + + +