mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Merge pull request #13 from Bubuni-Team/feature/timed-bans
[cm] ability to ban user for an arbitrary period
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using Kruzya.TelegramBot.Core;
|
||||
using BotFramework;
|
||||
using Kruzya.TelegramBot.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using West.TelegramBot.ChatManagement.ParamParser;
|
||||
using West.TelegramBot.ChatManagement.Service;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement;
|
||||
@@ -13,5 +15,6 @@ public class ChatManagement : Module
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<RulesService>();
|
||||
services.AddTelegramBotParameterParser<BanLength.Param, BanLength.Parser>();
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,10 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="ParamParser\" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Delete Files="$(OutDir)\TelegramBot.dll" />
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using BotFramework.Attributes;
|
||||
using BotFramework.Enums;
|
||||
@@ -8,6 +9,8 @@ using Kruzya.TelegramBot.Core.Data;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using West.TelegramBot.ChatManagement.ParamParser;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement.Handler;
|
||||
|
||||
@@ -15,18 +18,33 @@ public class Ban : CommonHandler
|
||||
{
|
||||
public Ban(CoreContext db) : base(db) {}
|
||||
|
||||
[Command("ban", CommandParseMode.Both)]
|
||||
public async Task HandleBan()
|
||||
[ParametrizedCommand("ban", CommandParseMode.Both)]
|
||||
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)]
|
||||
public async Task HandleUnban()
|
||||
{
|
||||
if (!await CanPunish()) return;
|
||||
if (!await CanPunish())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Bot.RestrictChatMemberAsync(Chat.Id, RepliedUser!.Id,
|
||||
new ChatPermissions
|
||||
|
||||
@@ -34,7 +34,7 @@ public class Warn : CommonHandler
|
||||
|
||||
if (warnCount == 3)
|
||||
{
|
||||
await BanMember(warnUser);
|
||||
await BanMember(warnUser.Id, DateTime.Now.AddDays(7));
|
||||
warnOption.SetValue(0);
|
||||
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