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;
|
2022-01-18 21:36:47 +02:00
|
|
|
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
|
|
|
|
2022-02-14 00:18:53 +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)]
|
2022-02-14 00:18:53 +02:00
|
|
|
public async Task HandleSetRules()
|
2022-01-18 20:34:21 +02:00
|
|
|
{
|
2022-02-14 00:18:53 +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-02-14 00:18:53 +02:00
|
|
|
}
|
2022-01-18 20:34:21 +02:00
|
|
|
|
2023-07-29 01:50:28 +03:00
|
|
|
[InChat(InChat.Public)]
|
|
|
|
|
[Command("rules", CommandParseMode.Both)]
|
2022-02-14 00:18:53 +02:00
|
|
|
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
|
|
|
{
|
2022-02-14 00:18:53 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2022-01-18 21:36:47 +02:00
|
|
|
|
2022-02-14 00:18:53 +02:00
|
|
|
try
|
|
|
|
|
{
|
2025-02-09 17:50:46 +02:00
|
|
|
await Bot.CopyMessage(Chat.Id, Chat.Id, await _rulesService.GetRulesMessageIdAsync(Chat) ?? 0);
|
2022-02-14 00:18:53 +02:00
|
|
|
}
|
|
|
|
|
catch (ApiRequestException)
|
|
|
|
|
{
|
2022-04-21 17:42:44 +03:00
|
|
|
await _rulesService.RemoveChatRulesAsync(Chat);
|
2022-02-14 00:18:53 +02:00
|
|
|
throw;
|
2022-01-18 20:34:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-14 00:18:53 +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
|
|
|
}
|