Files

60 lines
2.0 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;
2020-03-03 14:11:51 +04:00
using BotFramework.Setup;
using Kruzya.TelegramBot.Core.Cache;
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;
namespace Kruzya.TelegramBot.UrlLimitations
{
public class MessageHandler : BotEventHandler
{
protected ILogger Logger;
protected readonly CoreContext dbCtx;
2020-03-03 14:11:51 +04:00
public MessageHandler(ILogger<MessageHandler> logger, CoreContext coreContext)
2020-03-03 14:11:51 +04:00
{
Logger = logger;
dbCtx = coreContext;
2020-03-03 14:11:51 +04:00
}
[Message(InChat.Public, MessageFlag.HasText)]
public async Task OnMessageReceived()
2020-03-03 14:11:51 +04:00
{
var option = await dbCtx.UserValues.FindOption(Chat.Id, From.Id, "urlLimitations.messagesAfterJoin");
if (option == null)
2020-03-03 14:11:51 +04:00
{
return;
}
var messageCount = option.GetValue<UInt16>();
2020-03-03 14:11:51 +04:00
if (messageCount > 10)
{
dbCtx.UserValues.Remove(option);
2020-03-03 14:11:51 +04:00
return;
}
option.SetValue(messageCount + 1);
2020-03-03 14:11:51 +04:00
var message = RawUpdate.Message;
if (message.Entities.Count(e => e.Type == MessageEntityType.Url || e.Type == MessageEntityType.TextLink) >
0 || await message.HasUnknownMention(Bot))
2020-03-03 14:11:51 +04:00
{
var targetChat = new ChatId(Chat.Id);
2020-03-04 11:55:10 +04:00
Task.WaitAll(new Task[] {
2021-12-25 20:08:40 +04:00
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
2020-03-04 11:55:10 +04:00
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)
2020-03-04 11:55:10 +04:00
});
2020-03-03 14:11:51 +04:00
}
}
}
}