mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[cm] ability to ban user for an arbitrary period
This commit is contained in:
+2
-16
@@ -35,21 +35,8 @@ namespace Kruzya.TelegramBot.Core
|
|||||||
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
|
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Bans
|
|
||||||
protected async Task BanMember(User user, DateTime? banEnd = null, bool sendMessage = true)
|
|
||||||
{
|
|
||||||
await BanMember(user.Id, banEnd);
|
|
||||||
|
|
||||||
if (sendMessage)
|
protected async Task BanMember(long userId, DateTime banEnd)
|
||||||
{
|
|
||||||
await Bot.SendTextMessageAsync(
|
|
||||||
Chat.Id,
|
|
||||||
$"{From.ToHtml()} <code>заблокировал</code> {user.ToHtml()} <code>на 7 дней</code>",
|
|
||||||
disableNotification: true, parseMode: ParseMode.Html);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task BanMember(long userId, DateTime? banEnd = null)
|
|
||||||
{
|
{
|
||||||
await Bot.RestrictChatMemberAsync(
|
await Bot.RestrictChatMemberAsync(
|
||||||
Chat.Id,
|
Chat.Id,
|
||||||
@@ -58,10 +45,9 @@ namespace Kruzya.TelegramBot.Core
|
|||||||
{
|
{
|
||||||
CanSendMessages = false
|
CanSendMessages = false
|
||||||
},
|
},
|
||||||
banEnd ?? DateTime.Now.AddDays(7)
|
banEnd
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
protected async Task<BotUserValue> GetWarnOption(User user)
|
protected async Task<BotUserValue> GetWarnOption(User user)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using Kruzya.TelegramBot.Core;
|
using BotFramework;
|
||||||
|
using Kruzya.TelegramBot.Core;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using West.TelegramBot.ChatManagement.ParamParser;
|
||||||
using West.TelegramBot.ChatManagement.Service;
|
using West.TelegramBot.ChatManagement.Service;
|
||||||
|
|
||||||
namespace West.TelegramBot.ChatManagement;
|
namespace West.TelegramBot.ChatManagement;
|
||||||
@@ -13,5 +15,6 @@ public class ChatManagement : Module
|
|||||||
public override void ConfigureServices(IServiceCollection services)
|
public override void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddScoped<RulesService>();
|
services.AddScoped<RulesService>();
|
||||||
|
services.AddTelegramBotParameterParser<BanLength.Param, BanLength.Parser>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,10 @@
|
|||||||
<ProjectReference Include="..\..\Core\Core.csproj" />
|
<ProjectReference Include="..\..\Core\Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="ParamParser\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||||
<Delete Files="$(OutDir)\TelegramBot.dll" />
|
<Delete Files="$(OutDir)\TelegramBot.dll" />
|
||||||
</Target>
|
</Target>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BotFramework.Attributes;
|
using BotFramework.Attributes;
|
||||||
using BotFramework.Enums;
|
using BotFramework.Enums;
|
||||||
@@ -8,6 +9,8 @@ using Kruzya.TelegramBot.Core.Data;
|
|||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using West.TelegramBot.ChatManagement.ParamParser;
|
||||||
|
|
||||||
namespace West.TelegramBot.ChatManagement.Handler;
|
namespace West.TelegramBot.ChatManagement.Handler;
|
||||||
|
|
||||||
@@ -15,18 +18,33 @@ public class Ban : CommonHandler
|
|||||||
{
|
{
|
||||||
public Ban(CoreContext db) : base(db) {}
|
public Ban(CoreContext db) : base(db) {}
|
||||||
|
|
||||||
[Command("ban", CommandParseMode.Both)]
|
[ParametrizedCommand("ban", CommandParseMode.Both)]
|
||||||
public async Task HandleBan()
|
public async Task HandleBan(BanLength.Param banLengthParam)
|
||||||
{
|
{
|
||||||
if (!await CanPunish()) return;
|
if (!await CanPunish())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var now = DateTime.Now;
|
||||||
|
var banEnd = now.AddDays(banLengthParam.DayCount);
|
||||||
|
|
||||||
await BanMember(RepliedUser!);
|
var banEndMsg = (banEnd - now).Days > 366 ? "навсегда" : $"до {banEnd.ToUniversalTime()}";
|
||||||
|
|
||||||
|
await Bot.SendTextMessageAsync(
|
||||||
|
Chat.Id,
|
||||||
|
$"{From.ToHtml()} <code>заблокировал</code> {RepliedUser!.ToHtml()} <code>{banEndMsg}</code>",
|
||||||
|
disableNotification: true, parseMode: ParseMode.Html);
|
||||||
|
|
||||||
|
await BanMember(RepliedUser!.Id, banEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("unban", CommandParseMode.Both)]
|
[Command("unban", CommandParseMode.Both)]
|
||||||
public async Task HandleUnban()
|
public async Task HandleUnban()
|
||||||
{
|
{
|
||||||
if (!await CanPunish()) return;
|
if (!await CanPunish())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await Bot.RestrictChatMemberAsync(Chat.Id, RepliedUser!.Id,
|
await Bot.RestrictChatMemberAsync(Chat.Id, RepliedUser!.Id,
|
||||||
new ChatPermissions
|
new ChatPermissions
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class Warn : CommonHandler
|
|||||||
|
|
||||||
if (warnCount == 3)
|
if (warnCount == 3)
|
||||||
{
|
{
|
||||||
await BanMember(warnUser);
|
await BanMember(warnUser.Id, DateTime.Now.AddDays(7));
|
||||||
warnOption.SetValue(0);
|
warnOption.SetValue(0);
|
||||||
Db.AddOrUpdate(warnOption);
|
Db.AddOrUpdate(warnOption);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using BotFramework;
|
||||||
|
using static System.UInt32;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ChatManagement.ParamParser;
|
||||||
|
|
||||||
|
public class BanLength
|
||||||
|
{
|
||||||
|
public class Param
|
||||||
|
{
|
||||||
|
public uint DayCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Parser : IParameterParser<Param>
|
||||||
|
{
|
||||||
|
public Param DefaultInstance()
|
||||||
|
{
|
||||||
|
return new Param();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetValue(string text, out Param result)
|
||||||
|
{
|
||||||
|
result = DefaultInstance();
|
||||||
|
|
||||||
|
if (!TryParse(text, out result.DayCount))
|
||||||
|
{
|
||||||
|
result.DayCount = 7;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user