Files
telegram-bot/Core/Extensions/BotExtension.cs
T
Andriy 27ddfd39cc [core] Massive setting system update
- Get rid of attribute crap. Now only interface is required
- Rename canUse to isAdmin in `Bot.IsUserAdminAsync` to match the actual meaning of variable
- Made `AnswerQuery` reusable by moving it from ContentStore to Core's CommonHandler
- Chat Settings edit frontend (inline keyboard)
- Rename NsfwOption to AllowNsfw and move it to ContentStore module
- Remove option backend testing commands
- Add KarmaMode option as an example of Select option type
2023-07-21 17:14:11 +03:00

46 lines
1.6 KiB
C#

using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core.Extensions
{
public static class BotExtension
{
public static async Task<bool> CanPunishMember(this ITelegramBotClient bot, Chat chat, User user, User victim = null)
{
var canUse = await IsUserAdminAsync(bot, chat, user);
if (victim != null && canUse)
{
var victimMember = await bot.GetChatMemberAsync(chat, victim.Id);
canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator && !victim.IsBot;
}
return canUse;
}
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;
if (callerMember is ChatMemberAdministrator callerAdmin)
{
isAdmin = callerAdmin.CanRestrictMembers;
}
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};
}
}
}