Files
telegram-bot/modules/UserGroupTag/Handler.cs
T

107 lines
3.7 KiB
C#
Raw Normal View History

2022-01-03 22:13:44 +02:00
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
2022-01-03 23:25:20 +03:00
using Kruzya.TelegramBot.Core.Cache;
2022-01-03 22:13:44 +02:00
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
2022-01-03 23:25:20 +03:00
using Telegram.Bot.Types;
2022-01-03 22:13:44 +02:00
using Telegram.Bot.Types.Enums;
namespace UserGroupTag
{
public class Handler : BotEventHandler
{
private readonly CoreContext _db;
2022-01-03 23:25:20 +03:00
private readonly ICacheStorage<long, User> _userCache;
public Handler(CoreContext db, ICacheStorage<long, User> userCache)
2022-01-03 22:13:44 +02:00
{
_db = db;
2022-01-03 23:25:20 +03:00
_userCache = userCache;
2022-01-03 22:13:44 +02:00
}
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[ParametrizedCommand("add_group", CommandParseMode.Both)]
2022-01-03 22:13:44 +02:00
public async Task HandleAddGroup(string groupName)
{
// var groupName = group.Text;
var repliedMessage = RawUpdate.Message!.ReplyToMessage;
2023-01-02 23:42:07 +02:00
if (repliedMessage == null || repliedMessage.From!.IsBot || !(await Bot.CanPunishMember(Chat, From)) || string.IsNullOrEmpty(groupName))
2022-01-03 22:13:44 +02:00
{
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.SendTextMessageAsync(chatId, $"{from.ToHtml()} уже находится в группе {groupName}",
2023-01-02 19:01:03 +02:00
parseMode: ParseMode.Html);
2022-01-03 22:13:44 +02:00
return;
}
memberList.Add(repliedMessage.From.Id);
tagDictionary[groupName] = memberList;
userGroupOption.SetValue(tagDictionary);
2022-01-25 23:34:37 +03:00
_db.AddOrUpdate(userGroupOption);
2022-01-03 22:13:44 +02:00
2022-01-03 23:25:20 +03:00
_userCache.SetValue(repliedMessage.From.Id, repliedMessage.From, Int32.MaxValue);
2022-01-03 22:13:44 +02:00
await Bot.SendTextMessageAsync(chatId, $"{from.ToHtml()} добавлен в группу {groupName}",
2023-01-02 19:01:03 +02:00
parseMode: ParseMode.Html);
2022-01-03 22:13:44 +02:00
}
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[ParametrizedCommand("tag_group", CommandParseMode.Both)]
2022-01-03 22:13:44 +02:00
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)
2022-01-03 23:25:20 +03:00
.Text($" призывает группу пользователей {groupName}: ");
2022-01-03 22:13:44 +02:00
foreach (var userId in userList)
{
2022-01-03 23:25:20 +03:00
msg.UserMention(await GetUser(userId, Chat)).Text(", ");
2022-01-03 22:13:44 +02:00
}
2023-01-02 19:01:03 +02:00
await Bot.SendTextMessageAsync(Chat.Id, msg.ToString().TrimEnd(',', ' '), parseMode: ParseMode.Html);
2022-01-03 22:13:44 +02:00
}
2022-01-03 23:25:20 +03:00
protected async Task<User> GetUser(long userId, Chat chat)
{
User user;
if (!_userCache.TryGetValue(userId, out user))
{
2022-01-22 17:21:29 +03:00
user = (await Bot.GetChatMemberAsync(Chat.Id, userId)).User;
2022-01-03 23:25:20 +03:00
_userCache.SetValue(userId, user, Int32.MaxValue);
}
return user;
}
2022-01-03 22:13:44 +02:00
}
}