2021-12-27 22:53:13 +02:00
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-12-26 15:40:14 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BotFramework;
|
2021-12-27 22:53:13 +02:00
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
2021-12-26 15:40:14 +02:00
|
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
|
|
|
|
using Telegram.Bot;
|
|
|
|
|
using Telegram.Bot.Types;
|
|
|
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
namespace Kruzya.TelegramBot.Core.Handler;
|
|
|
|
|
|
|
|
|
|
public abstract class CommonHandler : BotEventHandler
|
2021-12-26 15:40:14 +02:00
|
|
|
{
|
2023-07-29 15:14:52 +03:00
|
|
|
protected readonly CoreContext Db;
|
|
|
|
|
|
|
|
|
|
protected Message? Message => RawUpdate.Message;
|
|
|
|
|
|
|
|
|
|
protected User? RepliedUser => Message?.ReplyToMessage?.From;
|
|
|
|
|
|
|
|
|
|
protected CommonHandler(CoreContext db)
|
|
|
|
|
{
|
|
|
|
|
Db = db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected async Task BanMember(long userId, DateTime banEnd)
|
|
|
|
|
{
|
|
|
|
|
await Bot.RestrictChatMemberAsync(
|
|
|
|
|
Chat.Id,
|
|
|
|
|
userId,
|
|
|
|
|
new ChatPermissions
|
|
|
|
|
{
|
|
|
|
|
CanSendMessages = false
|
|
|
|
|
},
|
|
|
|
|
banEnd
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task<BotUserValue> GetWarnOption(User user)
|
|
|
|
|
{
|
|
|
|
|
return await Db.UserValues.FindOrCreateOption(Chat.Id, user.Id, "warnCount");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task AnswerQuery(string id, string? message = null)
|
2021-12-26 15:40:14 +02:00
|
|
|
{
|
2023-07-29 15:14:52 +03:00
|
|
|
await Bot.AnswerCallbackQueryAsync(id, message);
|
2021-12-26 15:40:14 +02:00
|
|
|
}
|
|
|
|
|
}
|