mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[code-watcher] init module.
This commit is contained in:
@@ -31,5 +31,16 @@ namespace Kruzya.TelegramBot.Core.Extensions
|
|||||||
|
|
||||||
return canUse;
|
return canUse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat)
|
||||||
|
{
|
||||||
|
return await CanDeleteMessagesAsync(bot, chat, await bot.GetMeAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat, User user)
|
||||||
|
{
|
||||||
|
return await bot.GetChatMemberAsync(chat.Id, user.Id) is ChatMemberOwner
|
||||||
|
or ChatMemberAdministrator {CanRestrictMembers: true};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ COPY ./modules/ChatQuotes/*.csproj ./modules/ChatQuotes/
|
|||||||
COPY ./modules/Reputation/*.csproj ./modules/Reputation/
|
COPY ./modules/Reputation/*.csproj ./modules/Reputation/
|
||||||
COPY ./modules/ContentStore/*.csproj ./modules/ContentStore/
|
COPY ./modules/ContentStore/*.csproj ./modules/ContentStore/
|
||||||
COPY ./modules/DebugTools/*.csproj ./modules/DebugTools/
|
COPY ./modules/DebugTools/*.csproj ./modules/DebugTools/
|
||||||
|
COPY ./modules/CodeWatcher/*.csproj ./modules/CodeWatcher/
|
||||||
RUN dotnet restore Kruzya.TelegramBot.sln
|
RUN dotnet restore Kruzya.TelegramBot.sln
|
||||||
|
|
||||||
# Copy all project files
|
# Copy all project files
|
||||||
|
|||||||
@@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Reputation", "modules\Reput
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentStore", "modules\ContentStore\ContentStore.csproj", "{4F1E8FE8-640F-4671-87FC-0FFCCB6EDF5E}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContentStore", "modules\ContentStore\ContentStore.csproj", "{4F1E8FE8-640F-4671-87FC-0FFCCB6EDF5E}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DebugTools", "modules\DebugTools\DebugTools.csproj", "{B95C9180-4893-4A33-BF43-AB2EE998650C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DebugTools", "modules\DebugTools\DebugTools.csproj", "{B95C9180-4893-4A33-BF43-AB2EE998650C}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeWatcher", "modules\CodeWatcher\CodeWatcher.csproj", "{DEF70068-93E5-4198-8CED-4B3D8AE65162}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -89,6 +91,10 @@ Global
|
|||||||
{B95C9180-4893-4A33-BF43-AB2EE998650C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{B95C9180-4893-4A33-BF43-AB2EE998650C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{B95C9180-4893-4A33-BF43-AB2EE998650C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{B95C9180-4893-4A33-BF43-AB2EE998650C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{B95C9180-4893-4A33-BF43-AB2EE998650C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{B95C9180-4893-4A33-BF43-AB2EE998650C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{DEF70068-93E5-4198-8CED-4B3D8AE65162}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DEF70068-93E5-4198-8CED-4B3D8AE65162}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DEF70068-93E5-4198-8CED-4B3D8AE65162}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DEF70068-93E5-4198-8CED-4B3D8AE65162}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -105,6 +111,7 @@ Global
|
|||||||
{03F3C7D5-6FEF-4A83-9191-501091BFC1AC} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
{03F3C7D5-6FEF-4A83-9191-501091BFC1AC} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
||||||
{4F1E8FE8-640F-4671-87FC-0FFCCB6EDF5E} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
{4F1E8FE8-640F-4671-87FC-0FFCCB6EDF5E} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
||||||
{B95C9180-4893-4A33-BF43-AB2EE998650C} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
{B95C9180-4893-4A33-BF43-AB2EE998650C} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
||||||
|
{DEF70068-93E5-4198-8CED-4B3D8AE65162} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {5BA73C3B-D5FC-4942-9091-504325CDC308}
|
SolutionGuid = {5BA73C3B-D5FC-4942-9091-504325CDC308}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using Kruzya.TelegramBot.Core;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher
|
||||||
|
{
|
||||||
|
public class CodeWatcher : Module
|
||||||
|
{
|
||||||
|
public CodeWatcher(Core core) : base(core) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<AssemblyName>TelegramBot.CodeWatcher</AssemblyName>
|
||||||
|
<RootNamespace>West.TelegramBot.CodeWatcher</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Core\Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using BotFramework;
|
||||||
|
using BotFramework.Attributes;
|
||||||
|
using BotFramework.Enums;
|
||||||
|
using BotFramework.Utils;
|
||||||
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.CodeWatcher;
|
||||||
|
|
||||||
|
public class Handler : BotEventHandler
|
||||||
|
{
|
||||||
|
private static readonly string ResponseMessage = new HtmlString()
|
||||||
|
.Text(", не стоит публиковать столь большой код прямо в чат. Вместо этого загрузите его на ")
|
||||||
|
.Url("https://pastebin.com", "https://pastebin.com")
|
||||||
|
.Text(" и отправьте в виде ссылки.")
|
||||||
|
.ToString();
|
||||||
|
|
||||||
|
|
||||||
|
[Message(InChat.Public, MessageFlag.HasText)]
|
||||||
|
public async Task HandleMessage()
|
||||||
|
{
|
||||||
|
var message = RawUpdate.Message!;
|
||||||
|
|
||||||
|
if (message.Entities == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await Bot.CanDeleteMessagesAsync(Chat))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var entity in message.Entities)
|
||||||
|
{
|
||||||
|
if (entity.Type is MessageEntityType.Code or MessageEntityType.Pre)
|
||||||
|
{
|
||||||
|
var entityLines = message.Text!
|
||||||
|
.Substring(entity.Offset, entity.Length)
|
||||||
|
.SplitToLines()
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (entityLines.Count >= 10)
|
||||||
|
{
|
||||||
|
await DeleteAndNotifyAsync(message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entityLines.Any(line => line.Length > 80))
|
||||||
|
{
|
||||||
|
await DeleteAndNotifyAsync(message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAndNotifyAsync(Message message)
|
||||||
|
{
|
||||||
|
await Bot.DeleteMessageAsync(Chat.Id, message.MessageId);
|
||||||
|
await Bot.SendTextMessageAsync(Chat.Id,
|
||||||
|
(message.From?.ToHtml() ?? "") + ResponseMessage,
|
||||||
|
ParseMode.Html);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace West.TelegramBot.CodeWatcher;
|
||||||
|
|
||||||
|
public static class StringExtension
|
||||||
|
{
|
||||||
|
// https://stackoverflow.com/a/23408020
|
||||||
|
public static IEnumerable<string> SplitToLines(this string input)
|
||||||
|
{
|
||||||
|
using (var reader = new StringReader(input))
|
||||||
|
{
|
||||||
|
string line;
|
||||||
|
while ((line = reader.ReadLine()) != null)
|
||||||
|
{
|
||||||
|
yield return line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user