diff --git a/modules/CombotAntiSpam/Combot/CombotApiResponse.cs b/modules/CombotAntiSpam/Combot/CombotApiResponse.cs index 60f5a91..da82a5b 100644 --- a/modules/CombotAntiSpam/Combot/CombotApiResponse.cs +++ b/modules/CombotAntiSpam/Combot/CombotApiResponse.cs @@ -1,31 +1,30 @@ using System; using Newtonsoft.Json; -namespace Kruzya.TelegramBot.CombotAntiSpam.Combot +namespace Kruzya.TelegramBot.CombotAntiSpam.Combot; + +internal class CombotApiResponse { - internal class CombotApiResponse + public class ApiResult { - public class ApiResult - { - [JsonProperty("messages")] - public string[] Messages { get; set; } + [JsonProperty("messages")] + public string[] Messages { get; set; } - [JsonProperty("time_added")] - public int TimeAdded { get; set; } + [JsonProperty("time_added")] + public int TimeAdded { get; set; } - [JsonProperty("offenses")] - public int Offenses { get; set; } + [JsonProperty("offenses")] + public int Offenses { get; set; } - public DateTime AddedAt => DateTimeOffset.FromUnixTimeSeconds(TimeAdded).DateTime; - } - - [JsonProperty("ok")] - public bool IsSpammer { get; set; } - - [JsonProperty("description")] - public string Description { get; set; } - - [JsonProperty("result")] - public ApiResult Result { get; set; } + public DateTime AddedAt => DateTimeOffset.FromUnixTimeSeconds(TimeAdded).DateTime; } + + [JsonProperty("ok")] + public bool IsSpammer { get; set; } + + [JsonProperty("description")] + public string Description { get; set; } + + [JsonProperty("result")] + public ApiResult Result { get; set; } } \ No newline at end of file diff --git a/modules/CombotAntiSpam/Combot/CombotClient.cs b/modules/CombotAntiSpam/Combot/CombotClient.cs index a685fff..1ddd371 100644 --- a/modules/CombotAntiSpam/Combot/CombotClient.cs +++ b/modules/CombotAntiSpam/Combot/CombotClient.cs @@ -4,27 +4,26 @@ using System.Threading.Tasks; using Kruzya.TelegramBot.Core.Extensions; using Telegram.Bot.Types; -namespace Kruzya.TelegramBot.CombotAntiSpam.Combot +namespace Kruzya.TelegramBot.CombotAntiSpam.Combot; + +public class CombotClient : ICombotClient { - public class CombotClient : ICombotClient + public HttpClient _HttpClient; + + public CombotClient() { - public HttpClient _HttpClient; - - public CombotClient() - { - _HttpClient = new HttpClient(); - _HttpClient.BaseAddress = new Uri("https://api.cas.chat/check"); - } + _HttpClient = new HttpClient(); + _HttpClient.BaseAddress = new Uri("https://api.cas.chat/check"); + } - public bool IsSpammer(User user) - { - return IsSpammerAsync(user).GetAwaiter().GetResult(); - } + public bool IsSpammer(User user) + { + return IsSpammerAsync(user).GetAwaiter().GetResult(); + } - public async Task IsSpammerAsync(User user) - { - var result = await _HttpClient.GetJsonAsync($"?user_id={user.Id}"); - return result.IsSpammer; - } + public async Task IsSpammerAsync(User user) + { + var result = await _HttpClient.GetJsonAsync($"?user_id={user.Id}"); + return result.IsSpammer; } } \ No newline at end of file diff --git a/modules/CombotAntiSpam/Combot/ICombotClient.cs b/modules/CombotAntiSpam/Combot/ICombotClient.cs index 94e8bd6..284cc84 100644 --- a/modules/CombotAntiSpam/Combot/ICombotClient.cs +++ b/modules/CombotAntiSpam/Combot/ICombotClient.cs @@ -1,11 +1,10 @@ using System.Threading.Tasks; using Telegram.Bot.Types; -namespace Kruzya.TelegramBot.CombotAntiSpam.Combot +namespace Kruzya.TelegramBot.CombotAntiSpam.Combot; + +public interface ICombotClient { - public interface ICombotClient - { - public bool IsSpammer(User user); - public Task IsSpammerAsync(User user); - } + public bool IsSpammer(User user); + public Task IsSpammerAsync(User user); } \ No newline at end of file diff --git a/modules/CombotAntiSpam/CombotAntiSpam.cs b/modules/CombotAntiSpam/CombotAntiSpam.cs index 95fc214..9bd9944 100644 --- a/modules/CombotAntiSpam/CombotAntiSpam.cs +++ b/modules/CombotAntiSpam/CombotAntiSpam.cs @@ -2,17 +2,16 @@ using Kruzya.TelegramBot.Core; using Microsoft.Extensions.DependencyInjection; -namespace Kruzya.TelegramBot.CombotAntiSpam -{ - public class CombotAntiSpam : Module - { - public CombotAntiSpam(Core.Core core) : base(core) - { - } +namespace Kruzya.TelegramBot.CombotAntiSpam; - public override void ConfigureServices(IServiceCollection services) - { - services.AddSingleton(); - } +public class CombotAntiSpam : Module +{ + public CombotAntiSpam(Core.Core core) : base(core) + { } -} + + public override void ConfigureServices(IServiceCollection services) + { + services.AddSingleton(); + } +} \ No newline at end of file diff --git a/modules/CombotAntiSpam/UserJoinHandler.cs b/modules/CombotAntiSpam/UserJoinHandler.cs index f4f21a0..8beb975 100644 --- a/modules/CombotAntiSpam/UserJoinHandler.cs +++ b/modules/CombotAntiSpam/UserJoinHandler.cs @@ -11,50 +11,49 @@ using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; -namespace Kruzya.TelegramBot.CombotAntiSpam -{ - public class UserJoinHandler : BotEventHandler - { - protected ICombotClient _combotClient; - protected ILogger _logger; +namespace Kruzya.TelegramBot.CombotAntiSpam; - public UserJoinHandler(ICombotClient combotClient) +public class UserJoinHandler : BotEventHandler +{ + protected ICombotClient _combotClient; + protected ILogger _logger; + + public UserJoinHandler(ICombotClient combotClient) + { + _combotClient = combotClient; + } + + [Message(InChat.Public, MessageFlag.HasNewChatMembers)] + public async Task OnUserJoined() + { + // var canKickMembers = (await Bot.GetChatMemberAsync(Chat, (await Bot.GetMeAsync()).Id)).CanRestrictMembers == true; + var meUserId = (await Bot.GetMeAsync()).Id; + var meChatMember = await Bot.GetChatMemberAsync(Chat, meUserId); + + var canKickMembers = false; + if (meChatMember is ChatMemberAdministrator adminMember) { - _combotClient = combotClient; + canKickMembers = adminMember.CanRestrictMembers; } - [Message(InChat.Public, MessageFlag.HasNewChatMembers)] - public async Task OnUserJoined() + foreach (var member in RawUpdate.Message.NewChatMembers) { - // var canKickMembers = (await Bot.GetChatMemberAsync(Chat, (await Bot.GetMeAsync()).Id)).CanRestrictMembers == true; - var meUserId = (await Bot.GetMeAsync()).Id; - var meChatMember = await Bot.GetChatMemberAsync(Chat, meUserId); - - var canKickMembers = false; - if (meChatMember is ChatMemberAdministrator adminMember) + if (!(await _combotClient.IsSpammerAsync(member))) { - canKickMembers = adminMember.CanRestrictMembers; + continue; } - - foreach (var member in RawUpdate.Message.NewChatMembers) + + var messageText = new StringBuilder(); + messageText.Append($"⚠️ Combot Anti-Spam\n\n"); + messageText.Append($"User {member.ToHtml(true)} is spammer.\n"); + messageText.Append( + $"More details you can find on CAS site."); + + await Bot.SendTextMessageAsync(Chat, messageText.ToString(), parseMode: ParseMode.Html); + + if (canKickMembers) { - if (!(await _combotClient.IsSpammerAsync(member))) - { - continue; - } - - var messageText = new StringBuilder(); - messageText.Append($"⚠️ Combot Anti-Spam\n\n"); - messageText.Append($"User {member.ToHtml(true)} is spammer.\n"); - messageText.Append( - $"More details you can find on CAS site."); - - await Bot.SendTextMessageAsync(Chat, messageText.ToString(), parseMode: ParseMode.Html); - - if (canKickMembers) - { - await Bot.BanChatMemberAsync(Chat, member.Id); - } + await Bot.BanChatMemberAsync(Chat, member.Id); } } }