Merge remote-tracking branch 'origin/feature/warns-net5' into feature/new-dotnet-5

This commit is contained in:
2021-12-26 17:41:33 +04:00
9 changed files with 221 additions and 28 deletions
+10 -7
View File
@@ -5,17 +5,20 @@ namespace Kruzya.TelegramBot.Core.Extensions
{ {
public static class UserExtension public static class UserExtension
{ {
public static string ToHtml(this User user) public static string ToHtml(this User user, bool byUsername = true)
{ {
var text = new StringBuilder(); var text = new StringBuilder();
text.Append($"<a href=\"tg://user?id={user.Id}\">"); text.Append($"<a href=\"tg://user?id={user.Id}\">");
if (!string.IsNullOrWhiteSpace(user.Username))
text.Append(user.Username);
else
text.Append($"{user.FirstName} {user.LastName}".Trim().HtmlEncode());
text.Append("</a>");
return text.ToString(); if (byUsername && !string.IsNullOrWhiteSpace(user.Username))
{
text.Append(user.Username);
}
else
{
text.Append($"{user.FirstName} {user.LastName}".Trim().HtmlEncode());
}
return text.Append("</a>").ToString();
} }
} }
} }
-23
View File
@@ -1,23 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"BotConfig": {
"token": "5093569980:AAEnrlpg2VYD0CyQYIZtWpUuRrlpkDG8NaI"
},
"ModuleDirectories": [
"C:\\Users\\Andriy\\RiderProjects\\telegram-bot\\modules\\Destiny2.WhereIsXur\\bin\\Debug\\net5.0",
"C:\\Users\\Andriy\\RiderProjects\\telegram-bot\\modules\\RichSiteSummary\\bin\\Debug\\net5.0"
],
"ConnectionStrings": {
"DefaultConnection": "server=127.0.0.1;UserId=root;Password=;database=kruzyatgbot-core;port=3306",
"RichSiteSummary": "server=127.0.0.1;UserId=root;Password=;database=kruzyatgbot-rss;port=3306"
}
}
+7
View File
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UrlLimitations", "modules\U
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Destiny2.WhereIsXur", "modules\Destiny2.WhereIsXur\Destiny2.WhereIsXur.csproj", "{AE782132-BED8-4D1E-98FA-CB768171D332}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Destiny2.WhereIsXur", "modules\Destiny2.WhereIsXur\Destiny2.WhereIsXur.csproj", "{AE782132-BED8-4D1E-98FA-CB768171D332}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatManagement", "modules\ChatManagement\ChatManagement.csproj", "{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -45,11 +47,16 @@ Global
{AE782132-BED8-4D1E-98FA-CB768171D332}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE782132-BED8-4D1E-98FA-CB768171D332}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE782132-BED8-4D1E-98FA-CB768171D332}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE782132-BED8-4D1E-98FA-CB768171D332}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE782132-BED8-4D1E-98FA-CB768171D332}.Release|Any CPU.Build.0 = Release|Any CPU {AE782132-BED8-4D1E-98FA-CB768171D332}.Release|Any CPU.Build.0 = Release|Any CPU
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{C50D64C3-204B-4B58-927B-C8D759787252} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {C50D64C3-204B-4B58-927B-C8D759787252} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{B82E3339-3B34-4600-8C59-C5A3640B1982} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {B82E3339-3B34-4600-8C59-C5A3640B1982} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{AE782132-BED8-4D1E-98FA-CB768171D332} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {AE782132-BED8-4D1E-98FA-CB768171D332} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal
+11
View File
@@ -0,0 +1,11 @@
using Kruzya.TelegramBot.Core;
namespace West.TelegramBot.ChatManagement
{
public class ChatManagement : Module
{
public ChatManagement(Core core) : base(core)
{
}
}
}
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>TelegramBot.ChatManagement</AssemblyName>
<RootNamespace>West.TelegramBot.ChatManagement</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
namespace West.TelegramBot.ChatManagement.Extensions
{
public static class BotExtension
{
public static async Task<bool> CanPunishMember(this ITelegramBotClient bot, Chat chat, User user, User victim = null)
{
var callerMember = await bot.GetChatMemberAsync(chat, user.Id);
var canUse = callerMember is ChatMemberOwner;
if (callerMember is ChatMemberAdministrator callerAdmin)
{
canUse = callerAdmin.CanRestrictMembers;
}
if (victim != null && canUse)
{
var victimMember = await bot.GetChatMemberAsync(chat, victim.Id);
canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator;
}
return canUse;
}
}
}
+38
View File
@@ -0,0 +1,38 @@
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Telegram.Bot;
using Telegram.Bot.Types;
namespace West.TelegramBot.ChatManagement.Handler
{
public class Ban : ManagementHandler
{
[Command("ban", CommandParseMode.Both)]
public async Task HandleBan()
{
if (!await CanPunish()) return;
await BanMember(GetVictim());
}
[Command("unban", CommandParseMode.Both)]
public async Task HandleUnban()
{
if (!await CanPunish()) return;
await Bot.RestrictChatMemberAsync(Chat.Id, GetVictim().Id,
new ChatPermissions
{
CanSendMessages = true,
CanChangeInfo = true,
CanInviteUsers = true,
CanPinMessages = true,
CanSendPolls = true,
CanSendMediaMessages = true,
CanSendOtherMessages = true,
CanAddWebPagePreviews = true
});
}
}
}
@@ -0,0 +1,50 @@
using System;
using System.Threading.Tasks;
using BotFramework;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using West.TelegramBot.ChatManagement.Extensions;
namespace West.TelegramBot.ChatManagement.Handler
{
public class ManagementHandler : BotEventHandler
{
protected User GetVictim()
{
return RawUpdate.Message?.ReplyToMessage?.From;
}
protected async Task<bool> CanPunish()
{
return GetVictim() != null && await Bot.CanPunishMember(Chat, From, GetVictim());
}
protected async Task BanMember(User user, DateTime? banEnd = null, bool sendMessage = true)
{
await BanMember(user.Id, banEnd, sendMessage);
if (sendMessage)
{
await Bot.SendTextMessageAsync(
Chat.Id,
$"{From.ToHtml(false)} <code>заблокировал</code> {user.ToHtml(false)} <code>на 7 дней</code>",
disableNotification: true, parseMode: ParseMode.Html);
}
}
protected async Task BanMember(long userId, DateTime? banEnd = null, bool sendMessage = true)
{
await Bot.RestrictChatMemberAsync(
Chat.Id,
userId,
new ChatPermissions
{
CanSendMessages = false
},
banEnd ?? DateTime.Now.AddDays(7)
);
}
}
}
+66
View File
@@ -0,0 +1,66 @@
using System;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
namespace West.TelegramBot.ChatManagement.Handler
{
public class Command : ManagementHandler
{
private readonly CoreContext _db;
public Command(CoreContext db)
{
_db = db;
}
[Command("warn", CommandParseMode.Both)]
public async Task HandleWarn()
{
if (!await CanPunish()) return;
var warnUser = GetVictim();
var warnOption = await GetWarnOption();
var warnCount = warnOption.GetValue<int>() + 1;
warnOption.SetValue(warnCount);
_db.MarkAsModified(warnOption);
await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml(false)}<code>, Вам выдано предупреждение! " +
$"Текущее кол-во: {warnCount}/3</code>",
disableNotification: true,
parseMode: ParseMode.Html);
if (warnCount == 3)
{
await BanMember(warnUser);
warnOption.SetValue(0);
}
}
[Command("unwarn", CommandParseMode.Both)]
public async Task HandleUnwarn()
{
if (!await CanPunish()) return;
var warnOption = await GetWarnOption();
var warnCount = Math.Max(warnOption.GetValue<int>() - 1, 0);
warnOption.SetValue(warnCount);
_db.MarkAsModified(warnOption);
await Bot.SendTextMessageAsync(Chat.Id,
$"{GetVictim().ToHtml(false)}<code>, с Вас снято предупреждение! " +
$"Текущее кол-во: {warnCount}/3</code>",
disableNotification: true,
parseMode: ParseMode.Html);
}
private async Task<BotUserValue> GetWarnOption()
{
return await _db.UserValues.FindOrCreateOption(Chat.Id, GetVictim().Id, "warnCount");
}
}
}