mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Use file-scoped namespaces
This commit is contained in:
@@ -5,46 +5,46 @@ using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Kruzya.TelegramBot.UrlLimitations
|
||||
namespace Kruzya.TelegramBot.UrlLimitations;
|
||||
|
||||
public static class MessageExtension
|
||||
{
|
||||
public static class MessageExtension
|
||||
public static async Task<bool> HasUnknownMention(this Message message, ITelegramBotClient bot)
|
||||
{
|
||||
public static async Task<bool> HasUnknownMention(this Message message, ITelegramBotClient bot)
|
||||
foreach (var mention in message.Entities.Where(e =>
|
||||
e.Type == MessageEntityType.Mention || e.Type == MessageEntityType.TextMention))
|
||||
{
|
||||
foreach (var mention in message.Entities.Where(e =>
|
||||
e.Type == MessageEntityType.Mention || e.Type == MessageEntityType.TextMention))
|
||||
long userId = 0;
|
||||
if (mention.Type == MessageEntityType.TextMention)
|
||||
{
|
||||
long userId = 0;
|
||||
if (mention.Type == MessageEntityType.TextMention)
|
||||
userId = mention.User.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
userId = mention.User.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var chat = await bot.GetChatAsync(
|
||||
new ChatId(message.Text.Substring(mention.Offset, mention.Length)));
|
||||
var chat = await bot.GetChatAsync(
|
||||
new ChatId(message.Text.Substring(mention.Offset, mention.Length)));
|
||||
|
||||
if (chat != null)
|
||||
if (chat != null)
|
||||
{
|
||||
if (chat.Type != ChatType.Private)
|
||||
{
|
||||
if (chat.Type != ChatType.Private)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
userId = Convert.ToInt64(chat.Id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return true;
|
||||
|
||||
userId = Convert.ToInt64(chat.Id);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
var status = (await bot.GetChatMemberAsync(message.Chat, userId)).Status;
|
||||
var status = (await bot.GetChatMemberAsync(message.Chat, userId)).Status;
|
||||
|
||||
if ((new ChatMemberStatus[]
|
||||
if ((new ChatMemberStatus[]
|
||||
{
|
||||
ChatMemberStatus.Administrator, ChatMemberStatus.Creator, ChatMemberStatus.Member,
|
||||
ChatMemberStatus.Restricted
|
||||
@@ -52,12 +52,11 @@ namespace Kruzya.TelegramBot.UrlLimitations
|
||||
// i'm not sure about "Restricted"
|
||||
// maybe leaved members with some restrictions also can be "Restricted".
|
||||
}).Any(e => e == status) == false)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -11,49 +11,48 @@ using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Kruzya.TelegramBot.UrlLimitations
|
||||
{
|
||||
public class MessageHandler : BotEventHandler
|
||||
{
|
||||
protected ILogger Logger;
|
||||
protected readonly CoreContext dbCtx;
|
||||
namespace Kruzya.TelegramBot.UrlLimitations;
|
||||
|
||||
public MessageHandler(ILogger<MessageHandler> logger, CoreContext coreContext)
|
||||
public class MessageHandler : BotEventHandler
|
||||
{
|
||||
protected ILogger Logger;
|
||||
protected readonly CoreContext dbCtx;
|
||||
|
||||
public MessageHandler(ILogger<MessageHandler> logger, CoreContext coreContext)
|
||||
{
|
||||
Logger = logger;
|
||||
dbCtx = coreContext;
|
||||
}
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task OnMessageReceived()
|
||||
{
|
||||
var option = await dbCtx.UserValues.FindOption(Chat.Id, From.Id, "urlLimitations.messagesAfterJoin");
|
||||
if (option == null)
|
||||
{
|
||||
Logger = logger;
|
||||
dbCtx = coreContext;
|
||||
return;
|
||||
}
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task OnMessageReceived()
|
||||
var messageCount = option.GetValue<UInt16>();
|
||||
if (messageCount > 10)
|
||||
{
|
||||
var option = await dbCtx.UserValues.FindOption(Chat.Id, From.Id, "urlLimitations.messagesAfterJoin");
|
||||
if (option == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
dbCtx.UserValues.Remove(option);
|
||||
return;
|
||||
}
|
||||
|
||||
var messageCount = option.GetValue<UInt16>();
|
||||
if (messageCount > 10)
|
||||
{
|
||||
dbCtx.UserValues.Remove(option);
|
||||
return;
|
||||
}
|
||||
|
||||
option.SetValue(messageCount + 1);
|
||||
var message = RawUpdate.Message;
|
||||
if (message.Entities.Count(e => e.Type == MessageEntityType.Url || e.Type == MessageEntityType.TextLink) >
|
||||
0 || await message.HasUnknownMention(Bot))
|
||||
{
|
||||
var targetChat = new ChatId(Chat.Id);
|
||||
Task.WaitAll(
|
||||
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
|
||||
Bot.DeleteMessageAsync(targetChat, message.MessageId),
|
||||
Bot.SendTextMessageAsync(targetChat,
|
||||
option.SetValue(messageCount + 1);
|
||||
var message = RawUpdate.Message;
|
||||
if (message.Entities.Count(e => e.Type == MessageEntityType.Url || e.Type == MessageEntityType.TextLink) >
|
||||
0 || await message.HasUnknownMention(Bot))
|
||||
{
|
||||
var targetChat = new ChatId(Chat.Id);
|
||||
Task.WaitAll(
|
||||
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
|
||||
Bot.DeleteMessageAsync(targetChat, message.MessageId),
|
||||
Bot.SendTextMessageAsync(targetChat,
|
||||
$"Member {From.ToHtml(true)} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", parseMode: ParseMode.Html)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
using Kruzya.TelegramBot.Core;
|
||||
|
||||
namespace Kruzya.TelegramBot.UrlLimitations
|
||||
namespace Kruzya.TelegramBot.UrlLimitations;
|
||||
|
||||
public class UrlLimitations : Module
|
||||
{
|
||||
public class UrlLimitations : Module
|
||||
public UrlLimitations(Core.Core core) : base(core)
|
||||
{
|
||||
public UrlLimitations(Core.Core core) : base(core)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,24 +6,23 @@ using BotFramework.Enums;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
|
||||
namespace Kruzya.TelegramBot.UrlLimitations
|
||||
{
|
||||
public class UserJoinHandler : BotEventHandler
|
||||
{
|
||||
protected readonly CoreContext dbCtx;
|
||||
|
||||
public UserJoinHandler(CoreContext coreContext)
|
||||
{
|
||||
dbCtx = coreContext;
|
||||
}
|
||||
namespace Kruzya.TelegramBot.UrlLimitations;
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasNewChatMembers)]
|
||||
public async Task OnChatMembersAdded()
|
||||
{
|
||||
foreach (var member in RawUpdate.Message.NewChatMembers)
|
||||
await dbCtx.UserValues.SetOptionValue<UInt16>(Chat.Id, member.Id, "urlLimitations.messagesAfterJoin",
|
||||
0);
|
||||
}
|
||||
public class UserJoinHandler : BotEventHandler
|
||||
{
|
||||
protected readonly CoreContext dbCtx;
|
||||
|
||||
public UserJoinHandler(CoreContext coreContext)
|
||||
{
|
||||
dbCtx = coreContext;
|
||||
}
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasNewChatMembers)]
|
||||
public async Task OnChatMembersAdded()
|
||||
{
|
||||
foreach (var member in RawUpdate.Message.NewChatMembers)
|
||||
await dbCtx.UserValues.SetOptionValue<UInt16>(Chat.Id, member.Id, "urlLimitations.messagesAfterJoin",
|
||||
0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user