Files

58 lines
1.9 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.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;
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
}
[TextMessage(InChat.Public)]
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[] {
Bot.KickChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
Bot.DeleteMessageAsync(targetChat, message.MessageId),
Bot.SendTextMessageAsync(targetChat,
$"Member {From.ToHtml()} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html)
});
2020-03-03 14:11:51 +04:00
}
}
}
}