2020-03-03 14:11:51 +04:00
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BotFramework;
|
|
|
|
|
using BotFramework.Attributes;
|
2021-12-25 15:17:13 +02:00
|
|
|
using BotFramework.Enums;
|
2020-03-03 14:11:51 +04:00
|
|
|
using BotFramework.Setup;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Cache;
|
2020-03-08 12:38:00 +04:00
|
|
|
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;
|
2020-03-08 12:38:00 +04:00
|
|
|
protected readonly CoreContext dbCtx;
|
2020-03-03 14:11:51 +04:00
|
|
|
|
2020-03-08 12:38:00 +04:00
|
|
|
public MessageHandler(ILogger<MessageHandler> logger, CoreContext coreContext)
|
2020-03-03 14:11:51 +04:00
|
|
|
{
|
|
|
|
|
Logger = logger;
|
2020-03-08 12:38:00 +04:00
|
|
|
dbCtx = coreContext;
|
2020-03-03 14:11:51 +04:00
|
|
|
}
|
|
|
|
|
|
2023-07-29 01:50:28 +03:00
|
|
|
[InChat(InChat.Public)]
|
|
|
|
|
[Message(MessageFlag.HasText)]
|
2020-03-08 12:38:00 +04:00
|
|
|
public async Task OnMessageReceived()
|
2020-03-03 14:11:51 +04:00
|
|
|
{
|
2020-03-08 12:38:00 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 12:38:00 +04:00
|
|
|
var messageCount = option.GetValue<UInt16>();
|
2020-03-03 14:11:51 +04:00
|
|
|
if (messageCount > 10)
|
|
|
|
|
{
|
2020-03-08 12:38:00 +04:00
|
|
|
dbCtx.UserValues.Remove(option);
|
2020-03-03 14:11:51 +04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 12:38:00 +04:00
|
|
|
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) >
|
2020-03-08 12:38:00 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|