mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[core] move core handlers into subfolder
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#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 Kruzya.TelegramBot.Core.Handler
|
||||
{
|
||||
public abstract class CommonHandler : BotEventHandler
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using BotFramework;
|
||||
using BotFramework.Attributes;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Handler
|
||||
{
|
||||
public class GeneralHandler : BotEventHandler
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class HandleEverythingAttribute : HandlerAttribute
|
||||
{
|
||||
protected override bool CanHandle(HandlerParams param)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected readonly CoreContext databaseContext;
|
||||
|
||||
public GeneralHandler(CoreContext dbCtx)
|
||||
{
|
||||
databaseContext = dbCtx;
|
||||
}
|
||||
|
||||
[HandleEverything]
|
||||
[Priority(short.MaxValue - 10)]
|
||||
public async Task<bool> Listener()
|
||||
{
|
||||
foreach (var message in new Message[] { RawUpdate.Message, RawUpdate.EditedMessage, RawUpdate.ChannelPost, RawUpdate.EditedChannelPost })
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await VerifyChatPair(message);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task VerifyChatPair(Message message)
|
||||
{
|
||||
await VerifyChatPair(message.Chat, message.From);
|
||||
}
|
||||
|
||||
public async Task VerifyChatPair(Chat chat, User user)
|
||||
{
|
||||
await VerifyChatPair(chat.Id, user.Id);
|
||||
}
|
||||
|
||||
public async Task VerifyChatPair(long chat, long user)
|
||||
{
|
||||
var hasEntry = await databaseContext.Users.AnyAsync(e => e.ChatId == chat && e.UserId == user);
|
||||
if (hasEntry)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await MakeChatPair(chat, user);
|
||||
}
|
||||
|
||||
public async Task MakeChatPair(long chat, long user)
|
||||
{
|
||||
await databaseContext.Users.AddAsync(new BotUser()
|
||||
{
|
||||
ChatId = chat,
|
||||
UserId = user
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user