Files
telegram-bot/Core/Extensions/BotExtension.cs
T

45 lines
1.5 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
2023-07-29 15:14:52 +03:00
namespace Kruzya.TelegramBot.Core.Extensions;
public static class BotExtension
{
2023-07-29 15:14:52 +03:00
public static async Task<bool> CanPunishMember(this ITelegramBotClient bot, Chat chat, User user, User victim = null)
{
2023-07-29 15:14:52 +03:00
var canUse = await IsUserAdminAsync(bot, chat, user);
2022-01-18 21:59:26 +02:00
2023-07-29 15:14:52 +03:00
if (victim != null && canUse)
{
2023-07-29 15:14:52 +03:00
var victimMember = await bot.GetChatMemberAsync(chat, victim.Id);
canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator && !victim.IsBot;
}
2023-07-29 15:14:52 +03:00
return canUse;
}
2023-07-29 15:14:52 +03:00
public static async Task<bool> IsUserAdminAsync(this ITelegramBotClient bot, Chat chat, User user)
{
var callerMember = await bot.GetChatMemberAsync(chat, user.Id);
var isAdmin = callerMember is ChatMemberOwner;
2022-04-20 00:38:47 +03:00
2023-07-29 15:14:52 +03:00
if (callerMember is ChatMemberAdministrator callerAdmin)
2022-04-20 00:38:47 +03:00
{
2023-07-29 15:14:52 +03:00
isAdmin = callerAdmin.CanRestrictMembers;
2022-04-20 00:38:47 +03:00
}
2023-07-29 15:14:52 +03:00
return isAdmin;
}
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat)
{
return await CanDeleteMessagesAsync(bot, chat, await bot.GetMeAsync());
}
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat, User user)
{
return await bot.GetChatMemberAsync(chat.Id, user.Id) is ChatMemberOwner
or ChatMemberAdministrator {CanRestrictMembers: true};
}
}