Files
telegram-bot/modules/CombotAntiSpam/UserJoinHandler.cs
T

50 lines
1.5 KiB
C#
Raw Normal View History

2020-03-01 22:46:37 +04:00
using System.Text;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
2020-03-01 22:46:37 +04:00
using Kruzya.TelegramBot.CombotAntiSpam.Combot;
using Kruzya.TelegramBot.Core.Extensions;
2021-12-25 16:41:17 +02:00
using Telegram.Bot;
using Telegram.Bot.Types;
2020-03-01 22:46:37 +04:00
using Telegram.Bot.Types.Enums;
2023-07-28 19:34:28 +03:00
namespace Kruzya.TelegramBot.CombotAntiSpam;
public class UserJoinHandler : BotEventHandler
2020-03-01 22:46:37 +04:00
{
2023-07-29 15:03:45 +03:00
private readonly ICombotClient _combotClient;
2023-07-28 19:34:28 +03:00
public UserJoinHandler(ICombotClient combotClient)
{
_combotClient = combotClient;
}
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[Message(MessageFlag.HasNewChatMembers)]
2023-07-28 19:34:28 +03:00
public async Task OnUserJoined()
2020-03-01 22:46:37 +04:00
{
2023-07-29 15:03:45 +03:00
var canKickMembers = await Bot.IsUserAdminAsync(Chat, await Bot.GetMeAsync());
2020-03-01 22:46:37 +04:00
2023-07-29 15:03:45 +03:00
foreach (var member in RawUpdate.Message!.NewChatMembers!)
2020-03-01 22:46:37 +04:00
{
2023-07-28 19:34:28 +03:00
if (!(await _combotClient.IsSpammerAsync(member)))
2021-12-25 16:41:17 +02:00
{
2023-07-28 19:34:28 +03:00
continue;
2021-12-25 16:41:17 +02:00
}
2020-03-01 22:46:37 +04:00
2023-07-28 19:34:28 +03:00
var messageText = new StringBuilder();
2023-07-29 15:03:45 +03:00
messageText.Append("⚠️ <b>Combot Anti-Spam</b>\n\n");
2023-07-28 19:34:28 +03:00
messageText.Append($"User {member.ToHtml(true)} is spammer.\n");
messageText.Append(
$"More details you can find on <a href=\"https://cas.chat/query?u={member.Id}\">CAS site</a>.");
2020-03-01 22:46:37 +04:00
2023-07-28 19:34:28 +03:00
await Bot.SendTextMessageAsync(Chat, messageText.ToString(), parseMode: ParseMode.Html);
2020-03-01 22:46:37 +04:00
2023-07-28 19:34:28 +03:00
if (canKickMembers)
{
await Bot.BanChatMemberAsync(Chat, member.Id);
2020-03-01 22:46:37 +04:00
}
}
}
}