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:
@@ -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