Files

58 lines
1.8 KiB
C#
Raw Permalink Normal View History

2020-03-03 14:11:51 +04:00
using System;
using System.Linq;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
2020-03-03 14:11:51 +04:00
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Logging;
2021-12-25 16:41:17 +02:00
using Telegram.Bot;
2020-03-03 14:11:51 +04:00
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
2023-07-29 15:14:52 +03:00
namespace Kruzya.TelegramBot.UrlLimitations;
public class MessageHandler : BotEventHandler
2020-03-03 14:11:51 +04:00
{
2023-07-29 15:14:52 +03:00
protected ILogger Logger;
protected readonly CoreContext dbCtx;
public MessageHandler(ILogger<MessageHandler> logger, CoreContext coreContext)
2020-03-03 14:11:51 +04:00
{
2023-07-29 15:14:52 +03:00
Logger = logger;
dbCtx = coreContext;
}
2020-03-03 14:11:51 +04:00
2023-07-29 15:14:52 +03:00
[InChat(InChat.Public)]
[Message(MessageFlag.HasText)]
public async Task OnMessageReceived()
{
var option = await dbCtx.UserValues.FindOption(Chat.Id, From.Id, "urlLimitations.messagesAfterJoin");
if (option == null)
2020-03-03 14:11:51 +04:00
{
2023-07-29 15:14:52 +03:00
return;
2020-03-03 14:11:51 +04:00
}
2023-07-29 15:14:52 +03:00
var messageCount = option.GetValue<UInt16>();
if (messageCount > 10)
2020-03-03 14:11:51 +04:00
{
2023-07-29 15:14:52 +03:00
dbCtx.UserValues.Remove(option);
return;
}
2020-03-03 14:11:51 +04:00
2023-07-29 15:14:52 +03:00
option.SetValue(messageCount + 1);
var message = RawUpdate.Message;
if (message.Entities.Count(e => e.Type == MessageEntityType.Url || e.Type == MessageEntityType.TextLink) >
0 || await message.HasUnknownMention(Bot))
{
var targetChat = new ChatId(Chat.Id);
Task.WaitAll(
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
Bot.DeleteMessageAsync(targetChat, message.MessageId),
Bot.SendTextMessageAsync(targetChat,
2023-01-02 19:01:03 +02:00
$"Member {From.ToHtml(true)} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", parseMode: ParseMode.Html)
2023-07-29 15:14:52 +03:00
);
2020-03-03 14:11:51 +04:00
}
}
}