mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Merge pull request #16 from Bubuni-Team/feature/stickerset-ban
[cm] Ability to ban sticker sets
This commit is contained in:
@@ -16,7 +16,9 @@ public class ChatManagement : Module
|
|||||||
{
|
{
|
||||||
services.AddScoped<RulesService>();
|
services.AddScoped<RulesService>();
|
||||||
services.AddScoped<WarnService>();
|
services.AddScoped<WarnService>();
|
||||||
|
|
||||||
services.AddTelegramBotParameterParser<BanLength.Param, BanLength.Parser>();
|
services.AddTelegramBotParameterParser<BanLength.Param, BanLength.Parser>();
|
||||||
|
services.AddTelegramBotRawUpdateParser<StickerSet.Param, StickerSet.Parser>();
|
||||||
|
|
||||||
WarnService.WarnEvent += Notifier.WarnNotify;
|
WarnService.WarnEvent += Notifier.WarnNotify;
|
||||||
WarnService.UnWarnEvent += Notifier.UnWarnNotify;
|
WarnService.UnWarnEvent += Notifier.UnWarnNotify;
|
||||||
|
|||||||
@@ -10,10 +10,6 @@
|
|||||||
<ProjectReference Include="..\..\Core\Core.csproj" />
|
<ProjectReference Include="..\..\Core\Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="ParamParser\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||||
<Delete Files="$(OutDir)\TelegramBot.dll" />
|
<Delete Files="$(OutDir)\TelegramBot.dll" />
|
||||||
</Target>
|
</Target>
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
[ParametrizedCommand(InChat.Public, "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.DeleteMessageAsync(Chat.Id, (int) replyToMsgId);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Reply(FormatStickerSetAction(stickerSet.Set, "заблокирован"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[ParametrizedCommand(InChat.Public, "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, "разблокирован"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command(InChat.Public, "stick_banlist", CommandParseMode.Both)]
|
||||||
|
public async Task HandleSBanList()
|
||||||
|
{
|
||||||
|
var option = await GetBannedStickerSetOption();
|
||||||
|
var list = option.GetValue<List<string>>();
|
||||||
|
if (list == null)
|
||||||
|
{
|
||||||
|
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.GetStickerSetAsync(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Message(InChat.Public, MessageFlag.HasSticker)]
|
||||||
|
public async Task HandleMessage()
|
||||||
|
{
|
||||||
|
if (!await CanBotDeleteMessages())
|
||||||
|
{
|
||||||
|
await Reply(
|
||||||
|
"Этот набор стикеров заблокирован. Чтобы я мог его удалить, выдайте мне право на удаление сообщений.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = Message!;
|
||||||
|
if (await IsStickerBanned(message.Sticker!))
|
||||||
|
{
|
||||||
|
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BotFramework.Utils;
|
||||||
|
using Kruzya.TelegramBot.Core.Data;
|
||||||
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ChatManagement.Handler;
|
||||||
|
|
||||||
|
public partial class BanStickerSet
|
||||||
|
{
|
||||||
|
private async Task<bool> CanBotDeleteMessages()
|
||||||
|
{
|
||||||
|
return await Bot.GetChatMemberAsync(
|
||||||
|
Chat.Id,
|
||||||
|
(await Bot.GetMeAsync()).Id
|
||||||
|
) is ChatMemberAdministrator {CanDeleteMessages: true};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<bool> IsStickerBanned(Sticker sticker)
|
||||||
|
{
|
||||||
|
var bannedList = (await GetBannedStickerSetOption()).GetValue<List<string>>();
|
||||||
|
|
||||||
|
return bannedList != null && sticker.SetName != null && bannedList.Contains(sticker.SetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<BotUserValue> GetBannedStickerSetOption()
|
||||||
|
{
|
||||||
|
return await Db.UserValues.FindOrCreateOption(Chat.Id, "cmBannedStickers");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> PreCommand(ParamParser.StickerSet.Param? stickerSetParam = null)
|
||||||
|
{
|
||||||
|
if (stickerSetParam != null && !stickerSetParam.IsValid)
|
||||||
|
{
|
||||||
|
await Reply("Неверный набор стикеров.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await CanUseCommands();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string FormatStickerSetAction(StickerSet stickerSet, string action)
|
||||||
|
=> FormatStickerSetAction(stickerSet.Name, stickerSet.Title, action);
|
||||||
|
|
||||||
|
public string FormatStickerSetAction(string name, string action)
|
||||||
|
=> FormatStickerSetAction(name, name, action);
|
||||||
|
|
||||||
|
public string FormatStickerSetAction(string name, string title, string action)
|
||||||
|
=> new HtmlString()
|
||||||
|
.Text("Набор стикеров ")
|
||||||
|
.Url($"https://t.me/addstickers/{name}", title)
|
||||||
|
.Text($" {action}.")
|
||||||
|
.ToString();
|
||||||
|
|
||||||
|
private async Task<bool> CanUseCommands()
|
||||||
|
{
|
||||||
|
return await Bot.GetChatMemberAsync(Chat.Id, From.Id) is ChatMemberAdministrator {CanDeleteMessages: true}
|
||||||
|
or ChatMemberOwner;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using Kruzya.TelegramBot.Core;
|
||||||
|
using Kruzya.TelegramBot.Core.Data;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ChatManagement.Handler;
|
||||||
|
|
||||||
|
public partial class BanStickerSet : CommonHandler
|
||||||
|
{
|
||||||
|
public BanStickerSet(CoreContext db) : base(db) { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using BotFramework;
|
||||||
|
using BotFramework.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
using TgStickerSet = Telegram.Bot.Types.StickerSet;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ChatManagement.ParamParser;
|
||||||
|
|
||||||
|
public class StickerSet
|
||||||
|
{
|
||||||
|
public class Param
|
||||||
|
{
|
||||||
|
public TgStickerSet Set { get; set; }
|
||||||
|
public bool IsValid { get; set; } = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Parser : IRawParameterParser<Param>
|
||||||
|
{
|
||||||
|
private readonly IBotInstance _bot;
|
||||||
|
private readonly ILogger<StickerSet> _logger;
|
||||||
|
|
||||||
|
public Parser(IBotInstance bot, ILogger<StickerSet> logger)
|
||||||
|
{
|
||||||
|
_bot = bot;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Param DefaultInstance()
|
||||||
|
{
|
||||||
|
return new Param();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetValueByRawUpdate(Update update, out Param result)
|
||||||
|
{
|
||||||
|
result = DefaultInstance();
|
||||||
|
|
||||||
|
var bot = _bot.BotClient;
|
||||||
|
|
||||||
|
var message = update?.Message;
|
||||||
|
var sticker = message?.ReplyToMessage?.Sticker;
|
||||||
|
if (sticker is { SetName: { } })
|
||||||
|
{
|
||||||
|
result.Set = bot.GetStickerSetAsync(sticker.SetName).GetAwaiter().GetResult();
|
||||||
|
result.IsValid = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var messageText = message?.Text;
|
||||||
|
if (messageText != null)
|
||||||
|
{
|
||||||
|
var setName = messageText.Split(' ').Skip(1).ToList();
|
||||||
|
if (setName.Any())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result.Set = bot.GetStickerSetAsync(setName.First())
|
||||||
|
.GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
|
result.IsValid = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError("Got error while fetching sticker set: {ErrorMessage}", e.Message);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user