Files
West14 b99614868b Assorted improvements.
- Move BotExtension to Core.
- Update BotFramework
- Fix Fun commands.
- Remove ChatManagement dependency from Entertainment.
2022-01-03 14:58:53 +02:00

71 lines
2.2 KiB
C#

#nullable enable
using System;
using System.Threading.Tasks;
using BotFramework;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace West.TelegramBot.ChatManagement.Handler
{
public abstract class ManagementHandler : BotEventHandler
{
protected readonly CoreContext Db;
protected Message? Message => RawUpdate.Message;
protected User? RepliedUser => Message?.ReplyToMessage?.From;
protected ManagementHandler(CoreContext db)
{
Db = db;
}
protected async Task<Message> Reply(string text)
{
return await Bot.SendTextMessageAsync(Chat.Id, text,
replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html);
}
protected async Task<bool> CanPunish()
{
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
}
#region Bans
protected async Task BanMember(User user, DateTime? banEnd = null, bool sendMessage = true)
{
await BanMember(user.Id, banEnd);
if (sendMessage)
{
await Bot.SendTextMessageAsync(
Chat.Id,
$"{From.ToHtml()} <code>заблокировал</code> {user.ToHtml()} <code>на 7 дней</code>",
disableNotification: true, parseMode: ParseMode.Html);
}
}
protected async Task BanMember(long userId, DateTime? banEnd = null)
{
await Bot.RestrictChatMemberAsync(
Chat.Id,
userId,
new ChatPermissions
{
CanSendMessages = false
},
banEnd ?? DateTime.Now.AddDays(7)
);
}
#endregion
protected async Task<BotUserValue> GetWarnOption(User user)
{
return await Db.UserValues.FindOrCreateOption(Chat.Id, user.Id, "warnCount");
}
}
}