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:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user