mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
ChatManagement module.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using BotFramework;
|
||||
using BotFramework.Attributes;
|
||||
using BotFramework.Enums;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement.Handler
|
||||
{
|
||||
public class Command : BotEventHandler
|
||||
{
|
||||
private CoreContext _db;
|
||||
|
||||
public Command(CoreContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
[Command("warn", CommandParseMode.Both)]
|
||||
public async Task HandleWarn()
|
||||
{
|
||||
var callerMember = await Bot.GetChatMemberAsync(Chat, From.Id);
|
||||
var canUse = callerMember is ChatMemberOwner;
|
||||
|
||||
if (callerMember is ChatMemberAdministrator callerAdmin)
|
||||
{
|
||||
canUse = callerAdmin.CanRestrictMembers;
|
||||
}
|
||||
|
||||
if (canUse)
|
||||
{
|
||||
var userValues = _db.UserValues;
|
||||
|
||||
var warnUser = RawUpdate.Message.ReplyToMessage.From;
|
||||
var warnUserId = warnUser.Id;
|
||||
var warnOption = await userValues.FindOrCreateOption(Chat.Id, warnUserId, "warnCount");
|
||||
var warnCount = warnOption.GetValue<int>() + 1;
|
||||
warnOption.SetValue(warnCount);
|
||||
_db.MarkAsModified(warnOption);
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml()}<pre>, Вам выдано предупреждение! " +
|
||||
$"Текущее кол-во: {warnCount} / 3</pre",
|
||||
disableNotification: true,
|
||||
parseMode: ParseMode.Html);
|
||||
|
||||
if (warnCount == 3)
|
||||
{
|
||||
await Bot.RestrictChatMemberAsync(
|
||||
Chat.Id,
|
||||
warnUserId,
|
||||
new ChatPermissions
|
||||
{
|
||||
CanSendMessages = false
|
||||
},
|
||||
DateTime.Now.AddDays(7)
|
||||
);
|
||||
warnOption.SetValue(0);
|
||||
|
||||
await Bot.SendTextMessageAsync(
|
||||
Chat.Id, $"{From.ToHtml()} <pre>заблокировал</pre> {warnUser.ToHtml()} <pre>на 7 дней</pre>",
|
||||
disableNotification: true, parseMode: ParseMode.Html);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user