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;
|
|
|
|
|
using Telegram.Bot;
|
2022-01-18 21:36:47 +02:00
|
|
|
using Telegram.Bot.Exceptions;
|
2022-01-18 20:34:21 +02:00
|
|
|
|
|
|
|
|
namespace West.TelegramBot.ChatManagement.Handler
|
|
|
|
|
{
|
2022-01-18 21:59:26 +02:00
|
|
|
class Rules : ManagementHandler
|
2022-01-18 20:34:21 +02:00
|
|
|
{
|
|
|
|
|
[Command(InChat.Public, "setrules", CommandParseMode.Both)]
|
|
|
|
|
public async Task HandleSetRules()
|
|
|
|
|
{
|
|
|
|
|
var replyToMessage = Message?.ReplyToMessage;
|
|
|
|
|
|
2022-01-18 21:59:26 +02:00
|
|
|
if (replyToMessage == null || !await Bot.IsUserAdminAsync(Chat, From)) return;
|
2022-01-18 20:34:21 +02:00
|
|
|
|
2022-01-18 21:36:47 +02:00
|
|
|
var opt = await Db.UserValues.FindOrCreateOption(Chat.Id, "rulesMsgId");
|
|
|
|
|
opt.SetValue(replyToMessage.MessageId);
|
2022-01-22 17:13:38 +03:00
|
|
|
Db.Update(opt);
|
2022-01-18 20:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Command(InChat.Public, "rules", CommandParseMode.Both)]
|
|
|
|
|
public async Task HandleRules()
|
|
|
|
|
{
|
2022-01-18 21:36:47 +02:00
|
|
|
var opt = await Db.UserValues.FindOption(Chat.Id, "rulesMsgId");
|
|
|
|
|
if (opt == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Bot.CopyMessageAsync(Chat.Id, Chat.Id, opt.GetValue<int>());
|
|
|
|
|
}
|
|
|
|
|
catch (ApiRequestException)
|
|
|
|
|
{
|
2022-01-22 17:13:38 +03:00
|
|
|
Db.UserValues.Remove(opt);
|
2022-01-18 21:50:54 +02:00
|
|
|
throw;
|
2022-01-18 21:36:47 +02:00
|
|
|
}
|
2022-01-18 20:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-18 21:36:47 +02:00
|
|
|
public Rules(CoreContext db) : base(db) {}
|
2022-01-18 20:34:21 +02:00
|
|
|
}
|
|
|
|
|
}
|