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

47 lines
1.3 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;
using Telegram.Bot;
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
var opt = await Db.UserValues.FindOrCreateOption(Chat.Id, "rulesMsgId");
opt.SetValue(replyToMessage.MessageId);
Db.MarkAsModified(opt);
2022-01-18 20:34:21 +02:00
}
[Command(InChat.Public, "rules", CommandParseMode.Both)]
public async Task HandleRules()
{
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)
{
Db.MarkAsDeleted(opt);
throw;
}
2022-01-18 20:34:21 +02:00
}
public Rules(CoreContext db) : base(db) {}
2022-01-18 20:34:21 +02:00
}
}