mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[cm] greet new members & refactor rules
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Kruzya.TelegramBot.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using West.TelegramBot.ChatManagement.Service;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement;
|
||||
|
||||
@@ -7,4 +9,9 @@ public class ChatManagement : Module
|
||||
public ChatManagement(Core core) : base(core)
|
||||
{
|
||||
}
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<RulesService>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BotFramework;
|
||||
using BotFramework.Attributes;
|
||||
using BotFramework.Enums;
|
||||
using BotFramework.Utils;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using West.TelegramBot.ChatManagement.Service;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement.Handler;
|
||||
|
||||
public class Greet : BotEventHandler
|
||||
{
|
||||
private readonly RulesService _rulesService;
|
||||
|
||||
public Greet(RulesService rulesService)
|
||||
{
|
||||
_rulesService = rulesService;
|
||||
}
|
||||
|
||||
[Message(InChat.Public, MessageFlag.HasNewChatMembers)]
|
||||
public async Task HandleNewChatMembers()
|
||||
{
|
||||
var message = RawUpdate.Message!;
|
||||
var members = message.NewChatMembers!;
|
||||
var reply = new HtmlString();
|
||||
string membersHtml;
|
||||
if (members.Length > 1)
|
||||
{
|
||||
reply.Pre("Приветствуем новых участников!");
|
||||
membersHtml = string.Join(", ", members.Select(u => u.ToHtml()));
|
||||
}
|
||||
else
|
||||
{
|
||||
reply.Pre("Приветствуем нового участника!");
|
||||
membersHtml = members.First().ToHtml();
|
||||
}
|
||||
|
||||
reply.Br().TextBr("{0}");
|
||||
|
||||
if (await _rulesService.HasRulesAsync(Chat))
|
||||
{
|
||||
reply.Text("Пожалуйста, ознакомьтесь с правилами. /rules");
|
||||
}
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat.Id, string.Format(reply.ToString(), membersHtml),
|
||||
ParseMode.Html, replyToMessageId: message.MessageId);
|
||||
}
|
||||
}
|
||||
@@ -6,42 +6,48 @@ using Kruzya.TelegramBot.Core.Data;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Exceptions;
|
||||
using West.TelegramBot.ChatManagement.Service;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement.Handler;
|
||||
|
||||
class Rules : CommonHandler
|
||||
{
|
||||
private readonly RulesService _rulesService;
|
||||
|
||||
[Command(InChat.Public, "setrules", CommandParseMode.Both)]
|
||||
public async Task HandleSetRules()
|
||||
{
|
||||
var replyToMessage = Message?.ReplyToMessage;
|
||||
|
||||
if (replyToMessage == null || !await Bot.IsUserAdminAsync(Chat, From)) return;
|
||||
if (replyToMessage == null || !await Bot.IsUserAdminAsync(Chat, From))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var opt = await Db.UserValues.FindOrCreateOption(Chat.Id, "rulesMsgId");
|
||||
opt.SetValue(replyToMessage.MessageId);
|
||||
Db.AddOrUpdate(opt);
|
||||
await _rulesService.SetRulesAsync(Chat, replyToMessage.MessageId);
|
||||
}
|
||||
|
||||
[Command(InChat.Public, "rules", CommandParseMode.Both)]
|
||||
public async Task HandleRules()
|
||||
{
|
||||
var opt = await Db.UserValues.FindOption(Chat.Id, "rulesMsgId");
|
||||
if (opt == null)
|
||||
if (!await _rulesService.HasRulesAsync(Chat))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Bot.CopyMessageAsync(Chat.Id, Chat.Id, opt.GetValue<int>());
|
||||
await Bot.CopyMessageAsync(Chat.Id, Chat.Id, await _rulesService.GetRulesMessageIdAsync(Chat) ?? 0);
|
||||
}
|
||||
catch (ApiRequestException)
|
||||
{
|
||||
Db.UserValues.Remove(opt);
|
||||
await _rulesService.RemoveChatRulesAsync(Chat);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Rules(CoreContext db) : base(db) {}
|
||||
public Rules(CoreContext db, RulesService rulesService) : base(db)
|
||||
{
|
||||
_rulesService = rulesService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Kruzya.TelegramBot.Core.Extensions;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace West.TelegramBot.ChatManagement.Service;
|
||||
|
||||
public class RulesService
|
||||
{
|
||||
private readonly CoreContext _db;
|
||||
|
||||
public RulesService(CoreContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<BotUserValue?> GetRulesOptionAsync(Chat chat)
|
||||
{
|
||||
return await _db.UserValues.FindOption(chat.Id, "rulesMsgId");
|
||||
}
|
||||
|
||||
public async Task<int?> GetRulesMessageIdAsync(Chat chat)
|
||||
{
|
||||
return (await GetRulesOptionAsync(chat))?.GetValue<int>();
|
||||
}
|
||||
|
||||
public async Task<bool> HasRulesAsync(Chat chat)
|
||||
{
|
||||
return await GetRulesMessageIdAsync(chat) != null;
|
||||
}
|
||||
|
||||
public async Task SetRulesAsync(Chat chat, int messageId)
|
||||
{
|
||||
var option = await GetRulesOptionAsync(chat) ?? _db.UserValues.Create();
|
||||
option.SetValue(messageId);
|
||||
_db.AddOrUpdate(option);
|
||||
}
|
||||
|
||||
public async Task RemoveChatRulesAsync(Chat chat)
|
||||
{
|
||||
var option = await GetRulesOptionAsync(chat);
|
||||
if (option == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_db.Remove(option);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user