Files
telegram-bot/modules/ChatManagement/Handler/BanStickerSet.Handler.cs

131 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using West.TelegramBot.ChatManagement.ParamParser;
namespace West.TelegramBot.ChatManagement.Handler;
public partial class BanStickerSet
{
[InChat(InChat.Public)]
[ParametrizedCommand("stick_ban", CommandParseMode.Both)]
public async Task HandleBanSticker(StickerSet.Param stickerSet)
{
if (!await PreCommand(stickerSet))
{
return;
}
var option = await GetBannedStickerSetOption();
var stickerSetList = option.GetValue(new List<string>());
var setName = stickerSet.Set.Name;
if (stickerSetList.Contains(setName))
{
await Reply("Этот набор стикеров уже заблокирован.");
return;
}
stickerSetList.Add(setName);
option.SetValue(stickerSetList);
Db.AddOrUpdate(option);
var replyToMsgId = Message?.ReplyToMessage?.MessageId;
if (replyToMsgId != null)
{
await Bot.DeleteMessage(Chat.Id, (int) replyToMsgId);
}
await Reply(FormatStickerSetAction(stickerSet.Set, "заблокирован"));
}
[InChat(InChat.Public)]
[ParametrizedCommand("stick_unban", CommandParseMode.Both)]
public async Task HandleUnBanSticker(string stickerSet)
{
if (!await PreCommand())
{
return;
}
var option = await GetBannedStickerSetOption();
var list = option.GetValue<List<string>>();
if (list == null)
{
return;
}
if (!list.Remove(stickerSet))
{
return;
}
option.SetValue(list);
Db.AddOrUpdate(option);
await Reply(FormatStickerSetAction(stickerSet, "разблокирован"));
}
[InChat(InChat.Public)]
[Command("stick_banlist", CommandParseMode.Both)]
public async Task HandleSBanList()
{
var option = await GetBannedStickerSetOption();
var list = option.GetValue<List<string>>();
if (list == null)
{
await Reply("Нет заблокированных наборов стикеров.");
return;
}
string msg;
if (list.Count > 0)
{
var htmlString = new HtmlString().Bold("Заблокированные наборы стикеров:").Br();
for (var i = 0; i < list.Count; i++)
{
try
{
var stickerSet = await Bot.GetStickerSet(list[i]);
htmlString.Text($"{i + 1}. ")
.Url("https://t.me/addstickers/" + stickerSet.Name, stickerSet.Title)
.Br();
}
catch (Exception)
{
// Suppress error.
// Maybe remove pack from ban, but this exception can be just a timeout or Internal Server Error.
}
}
msg = htmlString.ToString();
}
else
{
msg = "Нет заблокированных наборов стикеров.";
}
await Reply(msg);
}
[InChat(InChat.Public)]
[Message(MessageFlag.HasSticker)]
public async Task HandleMessage()
{
var message = Message!;
if (await IsStickerBanned(message.Sticker!))
{
if (!await CanBotDeleteMessages())
{
await Reply(
"Этот набор стикеров заблокирован. Чтобы я мог его удалить, выдайте мне право на удаление сообщений.");
}
await Bot.DeleteMessage(Chat.Id, message.MessageId);
}
}
}