[cm] greet new members & refactor rules

This commit is contained in:
West14
2022-04-21 17:42:44 +03:00
parent fc7b341f49
commit 15b19c97dd
4 changed files with 124 additions and 9 deletions
+15 -9
View File
@@ -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;
}
}