mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
#nullable enable
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using BotFramework;
|
|
using BotFramework.Attributes;
|
|
using BotFramework.Enums;
|
|
using BotFramework.Utils;
|
|
using Kruzya.TelegramBot.Core.Cache;
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
|
using Telegram.Bot;
|
|
using Telegram.Bot.Types;
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
namespace UserGroupTag;
|
|
|
|
public class Handler : BotEventHandler
|
|
{
|
|
private readonly CoreContext _db;
|
|
private readonly ICacheStorage<long, User> _userCache;
|
|
|
|
public Handler(CoreContext db, ICacheStorage<long, User> userCache)
|
|
{
|
|
_db = db;
|
|
_userCache = userCache;
|
|
}
|
|
|
|
[InChat(InChat.Public)]
|
|
[ParametrizedCommand("add_group", CommandParseMode.Both)]
|
|
public async Task HandleAddGroup(string groupName)
|
|
{
|
|
// var groupName = group.Text;
|
|
var repliedMessage = RawUpdate.Message!.ReplyToMessage;
|
|
if (repliedMessage == null || repliedMessage.From!.IsBot || !(await Bot.CanPunishMember(Chat, From)) || string.IsNullOrEmpty(groupName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var from = repliedMessage.From;
|
|
var fromId = from.Id;
|
|
var chatId = Chat!.Id;
|
|
|
|
var userGroupOption = await _db.UserValues.FindOrCreateOption(Chat.Id, "userGroups");
|
|
var tagDictionary = userGroupOption.GetValue(new Dictionary<string, List<long>>());
|
|
|
|
var memberList = tagDictionary.GetValueOrDefault(groupName, new List<long>());
|
|
if (memberList.Contains(fromId))
|
|
{
|
|
await Bot.SendMessage(chatId, $"{from.ToHtml()} уже находится в группе {groupName}",
|
|
parseMode: ParseMode.Html);
|
|
return;
|
|
}
|
|
memberList.Add(repliedMessage.From.Id);
|
|
tagDictionary[groupName] = memberList;
|
|
|
|
userGroupOption.SetValue(tagDictionary);
|
|
_db.AddOrUpdate(userGroupOption);
|
|
|
|
_userCache.SetValue(repliedMessage.From.Id, repliedMessage.From, int.MaxValue);
|
|
await Bot.SendMessage(chatId, $"{from.ToHtml()} добавлен в группу {groupName}",
|
|
parseMode: ParseMode.Html);
|
|
}
|
|
|
|
[InChat(InChat.Public)]
|
|
[ParametrizedCommand("tag_group", CommandParseMode.Both)]
|
|
public async Task HandleTagGroup(string groupName)
|
|
{
|
|
if (string.IsNullOrEmpty(groupName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var userGroupOption = await _db.UserValues.FindOption(Chat.Id, "userGroups");
|
|
|
|
var userList = userGroupOption?.GetValue<Dictionary<string, List<long>>>()?[groupName];
|
|
if (userList == null || userList.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var msg = new HtmlString()
|
|
.Text("Пользователь ")
|
|
.UserMention(From)
|
|
.Text($" призывает группу пользователей {groupName}: ");
|
|
foreach (var userId in userList)
|
|
{
|
|
msg.UserMention(await GetUser(userId, Chat)).Text(", ");
|
|
}
|
|
|
|
await Bot.SendMessage(Chat.Id, msg.ToString().TrimEnd(',', ' '), parseMode: ParseMode.Html);
|
|
}
|
|
|
|
protected async Task<User> GetUser(long userId, Chat chat)
|
|
{
|
|
if (!_userCache.TryGetValue(userId, out var user))
|
|
{
|
|
user = (await Bot.GetChatMember(Chat.Id, userId)).User;
|
|
_userCache.SetValue(userId, user, int.MaxValue);
|
|
}
|
|
|
|
return user;
|
|
}
|
|
} |