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

47 lines
1.3 KiB
C#

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;
namespace West.TelegramBot.ChatManagement.Handler
{
class Rules : ManagementHandler
{
[Command(InChat.Public, "setrules", CommandParseMode.Both)]
public async Task HandleSetRules()
{
var replyToMessage = Message?.ReplyToMessage;
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);
}
[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.UserValues.Remove(opt);
throw;
}
}
public Rules(CoreContext db) : base(db) {}
}
}