Do not parse sticker pack when removing from ban

Also added try-catch strategy when printing banned packs
This commit is contained in:
2022-05-05 21:50:00 +03:00
parent 76ad165695
commit 97fc51775d
2 changed files with 35 additions and 17 deletions
@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
@@ -43,9 +44,9 @@ public partial class BanStickerSet
} }
[ParametrizedCommand(InChat.Public, "stick_unban", CommandParseMode.Both)] [ParametrizedCommand(InChat.Public, "stick_unban", CommandParseMode.Both)]
public async Task HandleUnBanSticker(StickerSet.Param stickerSet) public async Task HandleUnBanSticker(string stickerSet)
{ {
if (!await PreCommand(stickerSet)) if (!await PreCommand())
{ {
return; return;
} }
@@ -57,13 +58,16 @@ public partial class BanStickerSet
return; return;
} }
var setName = stickerSet.Set.Name; if (!list.Exists(e => e == stickerSet))
{
return;
}
list.Remove(setName); list.Remove(stickerSet);
option.SetValue(list); option.SetValue(list);
Db.AddOrUpdate(option); Db.AddOrUpdate(option);
await Reply(FormatStickerSetAction(stickerSet.Set, "разблокирован")); await Reply(FormatStickerSetAction(stickerSet, "разблокирован"));
} }
[Command(InChat.Public, "stick_banlist", CommandParseMode.Both)] [Command(InChat.Public, "stick_banlist", CommandParseMode.Both)]
@@ -81,12 +85,20 @@ public partial class BanStickerSet
{ {
var htmlString = new HtmlString().Bold("Заблокированные наборы стикеров:").Br(); var htmlString = new HtmlString().Bold("Заблокированные наборы стикеров:").Br();
for (var i = 0; i < list.Count; i++) for (var i = 0; i < list.Count; i++)
{
try
{ {
var stickerSet = await Bot.GetStickerSetAsync(list[i]); var stickerSet = await Bot.GetStickerSetAsync(list[i]);
htmlString.Text($"{i + 1}. ") htmlString.Text($"{i + 1}. ")
.Url("https://t.me/addstickers/" + stickerSet.Name, stickerSet.Title) .Url("https://t.me/addstickers/" + stickerSet.Name, stickerSet.Title)
.Br(); .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(); msg = htmlString.ToString();
} }
@@ -1,4 +1,6 @@
using System.Collections.Generic; #nullable enable
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Utils; using BotFramework.Utils;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
@@ -30,9 +32,9 @@ public partial class BanStickerSet
return await Db.UserValues.FindOrCreateOption(Chat.Id, "cmBannedStickers"); return await Db.UserValues.FindOrCreateOption(Chat.Id, "cmBannedStickers");
} }
public async Task<bool> PreCommand(ParamParser.StickerSet.Param stickerSetParam) public async Task<bool> PreCommand(ParamParser.StickerSet.Param? stickerSetParam = null)
{ {
if (!stickerSetParam.IsValid) if (stickerSetParam != null && !stickerSetParam.IsValid)
{ {
await Reply("Неверный набор стикеров."); await Reply("Неверный набор стикеров.");
return false; return false;
@@ -42,13 +44,17 @@ public partial class BanStickerSet
} }
public string FormatStickerSetAction(StickerSet stickerSet, string action) public string FormatStickerSetAction(StickerSet stickerSet, string action)
{ => FormatStickerSetAction(stickerSet.Name, stickerSet.Title, action);
return new HtmlString()
public string FormatStickerSetAction(string name, string action)
=> FormatStickerSetAction(name, name, action);
public string FormatStickerSetAction(string name, string title, string action)
=> new HtmlString()
.Text("Набор стикеров ") .Text("Набор стикеров ")
.Url($"https://t.me/addstickers/{stickerSet.Name}", stickerSet.Title) .Url($"https://t.me/addstickers/{name}", title)
.Text($" {action}.") .Text($" {action}.")
.ToString(); .ToString();
}
private async Task<bool> CanUseCommands() private async Task<bool> CanUseCommands()
{ {