mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[cas] use file-scoped namespaces
This commit is contained in:
@@ -1,31 +1,30 @@
|
|||||||
using System;
|
using System;
|
||||||
using Newtonsoft.Json;
|
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")]
|
[JsonProperty("time_added")]
|
||||||
public int TimeAdded { get; set; }
|
public int TimeAdded { get; set; }
|
||||||
|
|
||||||
[JsonProperty("offenses")]
|
[JsonProperty("offenses")]
|
||||||
public int Offenses { get; set; }
|
public int Offenses { get; set; }
|
||||||
|
|
||||||
public DateTime AddedAt => DateTimeOffset.FromUnixTimeSeconds(TimeAdded).DateTime;
|
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; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[JsonProperty("ok")]
|
||||||
|
public bool IsSpammer { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("description")]
|
||||||
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("result")]
|
||||||
|
public ApiResult Result { get; set; }
|
||||||
}
|
}
|
||||||
@@ -4,27 +4,26 @@ using System.Threading.Tasks;
|
|||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Telegram.Bot.Types;
|
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;
|
_HttpClient = new HttpClient();
|
||||||
|
_HttpClient.BaseAddress = new Uri("https://api.cas.chat/check");
|
||||||
public CombotClient()
|
}
|
||||||
{
|
|
||||||
_HttpClient = new HttpClient();
|
|
||||||
_HttpClient.BaseAddress = new Uri("https://api.cas.chat/check");
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsSpammer(User user)
|
public bool IsSpammer(User user)
|
||||||
{
|
{
|
||||||
return IsSpammerAsync(user).GetAwaiter().GetResult();
|
return IsSpammerAsync(user).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> IsSpammerAsync(User user)
|
public async Task<bool> IsSpammerAsync(User user)
|
||||||
{
|
{
|
||||||
var result = await _HttpClient.GetJsonAsync<CombotApiResponse>($"?user_id={user.Id}");
|
var result = await _HttpClient.GetJsonAsync<CombotApiResponse>($"?user_id={user.Id}");
|
||||||
return result.IsSpammer;
|
return result.IsSpammer;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot.Types;
|
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<bool> IsSpammerAsync(User user);
|
||||||
public bool IsSpammer(User user);
|
|
||||||
public Task<bool> IsSpammerAsync(User user);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,17 +2,16 @@
|
|||||||
using Kruzya.TelegramBot.Core;
|
using Kruzya.TelegramBot.Core;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace Kruzya.TelegramBot.CombotAntiSpam
|
namespace Kruzya.TelegramBot.CombotAntiSpam;
|
||||||
{
|
|
||||||
public class CombotAntiSpam : Module
|
|
||||||
{
|
|
||||||
public CombotAntiSpam(Core.Core core) : base(core)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void ConfigureServices(IServiceCollection services)
|
public class CombotAntiSpam : Module
|
||||||
{
|
{
|
||||||
services.AddSingleton<ICombotClient, CombotClient>();
|
public CombotAntiSpam(Core.Core core) : base(core)
|
||||||
}
|
{
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public override void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<ICombotClient, CombotClient>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,50 +11,49 @@ using Telegram.Bot;
|
|||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
namespace Kruzya.TelegramBot.CombotAntiSpam
|
namespace Kruzya.TelegramBot.CombotAntiSpam;
|
||||||
{
|
|
||||||
public class UserJoinHandler : BotEventHandler
|
|
||||||
{
|
|
||||||
protected ICombotClient _combotClient;
|
|
||||||
protected ILogger _logger;
|
|
||||||
|
|
||||||
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)]
|
foreach (var member in RawUpdate.Message.NewChatMembers)
|
||||||
public async Task OnUserJoined()
|
|
||||||
{
|
{
|
||||||
// var canKickMembers = (await Bot.GetChatMemberAsync(Chat, (await Bot.GetMeAsync()).Id)).CanRestrictMembers == true;
|
if (!(await _combotClient.IsSpammerAsync(member)))
|
||||||
var meUserId = (await Bot.GetMeAsync()).Id;
|
|
||||||
var meChatMember = await Bot.GetChatMemberAsync(Chat, meUserId);
|
|
||||||
|
|
||||||
var canKickMembers = false;
|
|
||||||
if (meChatMember is ChatMemberAdministrator adminMember)
|
|
||||||
{
|
{
|
||||||
canKickMembers = adminMember.CanRestrictMembers;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var member in RawUpdate.Message.NewChatMembers)
|
var messageText = new StringBuilder();
|
||||||
|
messageText.Append($"⚠️ <b>Combot Anti-Spam</b>\n\n");
|
||||||
|
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>.");
|
||||||
|
|
||||||
|
await Bot.SendTextMessageAsync(Chat, messageText.ToString(), parseMode: ParseMode.Html);
|
||||||
|
|
||||||
|
if (canKickMembers)
|
||||||
{
|
{
|
||||||
if (!(await _combotClient.IsSpammerAsync(member)))
|
await Bot.BanChatMemberAsync(Chat, member.Id);
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var messageText = new StringBuilder();
|
|
||||||
messageText.Append($"⚠️ <b>Combot Anti-Spam</b>\n\n");
|
|
||||||
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>.");
|
|
||||||
|
|
||||||
await Bot.SendTextMessageAsync(Chat, messageText.ToString(), parseMode: ParseMode.Html);
|
|
||||||
|
|
||||||
if (canKickMembers)
|
|
||||||
{
|
|
||||||
await Bot.BanChatMemberAsync(Chat, member.Id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user