2021-12-27 22:53:13 +02:00
|
|
|
#nullable enable
|
|
|
|
|
|
2022-04-23 21:03:33 +03:00
|
|
|
using System;
|
2021-12-27 22:53:13 +02:00
|
|
|
using System.Threading.Tasks;
|
2021-12-26 15:40:14 +02:00
|
|
|
using BotFramework.Attributes;
|
|
|
|
|
using BotFramework.Enums;
|
2021-12-27 22:53:13 +02:00
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
2022-04-08 15:53:34 +03:00
|
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
2023-07-20 21:22:35 +03:00
|
|
|
using Kruzya.TelegramBot.Core.Handler;
|
2021-12-26 15:40:14 +02:00
|
|
|
using Telegram.Bot;
|
|
|
|
|
using Telegram.Bot.Types;
|
2022-04-23 21:03:33 +03:00
|
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
|
using West.TelegramBot.ChatManagement.ParamParser;
|
2021-12-26 15:40:14 +02:00
|
|
|
|
2022-02-14 10:35:28 +02:00
|
|
|
namespace West.TelegramBot.ChatManagement.Handler;
|
|
|
|
|
|
|
|
|
|
public class Ban : CommonHandler
|
2021-12-26 15:40:14 +02:00
|
|
|
{
|
2022-02-14 10:35:28 +02:00
|
|
|
public Ban(CoreContext db) : base(db) {}
|
2021-12-27 22:53:13 +02:00
|
|
|
|
2022-04-23 21:03:33 +03:00
|
|
|
[ParametrizedCommand("ban", CommandParseMode.Both)]
|
|
|
|
|
public async Task HandleBan(BanLength.Param banLengthParam)
|
2022-02-14 10:35:28 +02:00
|
|
|
{
|
2022-04-23 21:03:33 +03:00
|
|
|
if (!await CanPunish())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
var banEnd = now.AddDays(banLengthParam.DayCount);
|
2021-12-26 15:40:14 +02:00
|
|
|
|
2022-04-25 20:13:43 +03:00
|
|
|
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
|
|
|
|
|
{
|
2025-02-09 17:50:46 +02:00
|
|
|
await Bot.SendMessage(Chat.Id, "Нет прав на блокировку пользователя.");
|
2022-04-25 20:13:43 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2022-04-23 21:03:33 +03:00
|
|
|
|
2025-02-09 17:50:46 +02:00
|
|
|
await Bot.SendMessage(
|
2022-04-23 21:03:33 +03:00
|
|
|
Chat.Id,
|
|
|
|
|
$"{From.ToHtml()} <code>заблокировал</code> {RepliedUser!.ToHtml()} <code>{banEndMsg}</code>",
|
|
|
|
|
disableNotification: true, parseMode: ParseMode.Html);
|
2022-02-14 10:35:28 +02:00
|
|
|
}
|
2021-12-26 15:40:14 +02:00
|
|
|
|
2022-02-14 10:35:28 +02:00
|
|
|
[Command("unban", CommandParseMode.Both)]
|
|
|
|
|
public async Task HandleUnban()
|
|
|
|
|
{
|
2022-04-23 21:03:33 +03:00
|
|
|
if (!await CanPunish())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-26 15:40:14 +02:00
|
|
|
|
2025-02-09 17:40:03 +02:00
|
|
|
await Bot.RestrictChatMember(Chat.Id, RepliedUser!.Id,
|
2022-02-14 10:35:28 +02:00
|
|
|
new ChatPermissions
|
|
|
|
|
{
|
|
|
|
|
CanSendMessages = true,
|
|
|
|
|
CanSendOtherMessages = true,
|
|
|
|
|
});
|
2022-04-08 15:53:34 +03:00
|
|
|
|
|
|
|
|
await Reply($"{From.ToHtml()} <code>разблокировал</code> {RepliedUser!.ToHtml()}");
|
2021-12-26 15:40:14 +02:00
|
|
|
}
|
|
|
|
|
}
|