Files
telegram-bot/modules/ChatManagement/Handler/Rules.cs
T

55 lines
1.4 KiB
C#
Raw Normal View History

2022-01-18 20:34:21 +02:00
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
2023-07-20 21:22:35 +03:00
using Kruzya.TelegramBot.Core.Handler;
2022-01-18 20:34:21 +02:00
using Telegram.Bot;
using Telegram.Bot.Exceptions;
2022-04-21 17:42:44 +03:00
using West.TelegramBot.ChatManagement.Service;
2022-01-18 20:34:21 +02:00
namespace West.TelegramBot.ChatManagement.Handler;
class Rules : CommonHandler
2022-01-18 20:34:21 +02:00
{
2022-04-21 17:42:44 +03:00
private readonly RulesService _rulesService;
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[Command("setrules", CommandParseMode.Both)]
public async Task HandleSetRules()
2022-01-18 20:34:21 +02:00
{
var replyToMessage = Message?.ReplyToMessage;
2022-01-18 20:34:21 +02:00
2022-04-21 17:42:44 +03:00
if (replyToMessage == null || !await Bot.IsUserAdminAsync(Chat, From))
{
return;
}
2022-01-18 20:34:21 +02:00
2022-04-21 17:42:44 +03:00
await _rulesService.SetRulesAsync(Chat, replyToMessage.MessageId);
}
2022-01-18 20:34:21 +02:00
2023-07-29 01:50:28 +03:00
[InChat(InChat.Public)]
[Command("rules", CommandParseMode.Both)]
public async Task HandleRules()
{
2022-04-21 17:42:44 +03:00
if (!await _rulesService.HasRulesAsync(Chat))
2022-01-18 20:34:21 +02:00
{
return;
}
try
{
await Bot.CopyMessage(Chat.Id, Chat.Id, await _rulesService.GetRulesMessageIdAsync(Chat) ?? 0);
}
catch (ApiRequestException)
{
2022-04-21 17:42:44 +03:00
await _rulesService.RemoveChatRulesAsync(Chat);
throw;
2022-01-18 20:34:21 +02:00
}
}
2022-04-21 17:42:44 +03:00
public Rules(CoreContext db, RulesService rulesService) : base(db)
{
_rulesService = rulesService;
}
2022-01-18 20:34:21 +02:00
}