Add UserGroupTag module.

This commit is contained in:
West14
2022-01-03 22:13:44 +02:00
parent 9f108d5021
commit 64a4a1fabb
4 changed files with 122 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
namespace UserGroupTag
{
public class Handler : BotEventHandler
{
private readonly CoreContext _db;
public Handler(CoreContext db)
{
_db = db;
}
[ParametrizedCommand(InChat.Public, "add_group", CommandParseMode.Both)]
public async Task HandleAddGroup(string groupName)
{
Console.WriteLine("add_group");
// var groupName = group.Text;
var repliedMessage = RawUpdate.Message!.ReplyToMessage;
if (repliedMessage == null || repliedMessage.From!.IsBot || groupName.IsNullOrEmpty())
{
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}",
ParseMode.Html);
return;
}
memberList.Add(repliedMessage.From.Id);
tagDictionary[groupName] = memberList;
userGroupOption.SetValue(tagDictionary);
_db.MarkAsModified(userGroupOption);
await Bot.SendTextMessageAsync(chatId, $"{from.ToHtml()} добавлен в группу {groupName}",
ParseMode.Html);
}
[ParametrizedCommand(InChat.Public, "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(" охуел и хочет доебаться к: ");
foreach (var userId in userList)
{
msg.User(userId, userId.ToString()).Text(", ");
}
await Bot.SendTextMessageAsync(Chat.Id, msg.ToString().TrimEnd(',', ' '), ParseMode.Html);
}
}
}
+10
View File
@@ -0,0 +1,10 @@
using System;
using Kruzya.TelegramBot.Core;
namespace UserGroupTag
{
public class UserGroupTag : Module
{
public UserGroupTag(Core core) : base(core) { }
}
}
+15
View File
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Delete Files="$(OutDir)\TelegramBot.dll" />
</Target>
</Project>