mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Add ChatManagement commands. Drop appsettings from version control.
This commit is contained in:
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user