Add ChatManagement commands. Drop appsettings from version control.

This commit is contained in:
West14
2021-12-26 15:40:14 +02:00
parent 685caf52b7
commit 7b37e2f35d
7 changed files with 190 additions and 98 deletions
@@ -0,0 +1,50 @@
using System;
using System.Threading.Tasks;
using BotFramework;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using West.TelegramBot.ChatManagement.Extensions;
namespace West.TelegramBot.ChatManagement.Handler
{
public class ManagementHandler : BotEventHandler
{
protected User GetVictim()
{
return RawUpdate.Message?.ReplyToMessage?.From;
}
protected async Task<bool> CanPunish()
{
return GetVictim() != null && await Bot.CanPunishMember(Chat, From, GetVictim());
}
protected async Task BanMember(User user, DateTime? banEnd = null, bool sendMessage = true)
{
await BanMember(user.Id, banEnd, sendMessage);
if (sendMessage)
{
await Bot.SendTextMessageAsync(
Chat.Id,
$"{From.ToHtml(false)} <code>заблокировал</code> {user.ToHtml(false)} <code>на 7 дней</code>",
disableNotification: true, parseMode: ParseMode.Html);
}
}
protected async Task BanMember(long userId, DateTime? banEnd = null, bool sendMessage = true)
{
await Bot.RestrictChatMemberAsync(
Chat.Id,
userId,
new ChatPermissions
{
CanSendMessages = false
},
banEnd ?? DateTime.Now.AddDays(7)
);
}
}
}