#nullable enable using System; using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Handler; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using West.TelegramBot.ChatManagement.ParamParser; namespace West.TelegramBot.ChatManagement.Handler; public class Ban : CommonHandler { public Ban(CoreContext db) : base(db) {} [ParametrizedCommand("ban", CommandParseMode.Both)] public async Task HandleBan(BanLength.Param banLengthParam) { if (!await CanPunish()) { return; } var now = DateTime.Now; var banEnd = now.AddDays(banLengthParam.DayCount); var banEndMsg = (banEnd - now).Days > 366 ? "навсегда" : $"до {banEnd:D}"; /* TODO: cause this block of code is repeated in warn handler, TODO: I think we should abstract it somehow TODO: West, 25.04.2022, 20:10 */ try { await BanMember(RepliedUser!.Id, banEnd); } catch { await Bot.SendMessage(Chat.Id, "Нет прав на блокировку пользователя."); return; } await Bot.SendMessage( Chat.Id, $"{From.ToHtml()} заблокировал {RepliedUser!.ToHtml()} {banEndMsg}", disableNotification: true, parseMode: ParseMode.Html); } [Command("unban", CommandParseMode.Both)] public async Task HandleUnban() { if (!await CanPunish()) { return; } // TODO: in Telegram.Bot >=22.4.0 this could be replaced with new ChatPermissions(true) await Bot.RestrictChatMember(Chat.Id, RepliedUser!.Id, new ChatPermissions { CanSendMessages = true, CanAddWebPagePreviews = true, CanChangeInfo = true, CanInviteUsers = true, CanManageTopics = true, CanPinMessages = true, CanSendAudios = true, CanSendDocuments = true, CanSendOtherMessages = true, CanSendPhotos = true, CanSendPolls = true, CanSendVideos = true, CanSendVideoNotes = true, CanSendVoiceNotes = true, }); await Reply($"{From.ToHtml()} разблокировал {RepliedUser!.ToHtml()}"); } }