Assorted improvements.

- Move BotExtension to Core.
- Update BotFramework
- Fix Fun commands.
- Remove ChatManagement dependency from Entertainment.
This commit is contained in:
West14
2022-01-03 14:58:53 +02:00
parent fad01168aa
commit b99614868b
8 changed files with 57 additions and 31 deletions
@@ -2,23 +2,33 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using West.TelegramBot.ChatManagement.Handler;
using Telegram.Bot.Types;
namespace West.Entertainment.Handler.Fun
{
public abstract class AbstractAnimationReply : ManagementHandler
public abstract class AbstractAnimationReply : BotEventHandler
{
private readonly CoreContext _db;
protected Message? Message => RawUpdate.Message;
private User? RepliedUser => Message?.ReplyToMessage?.From;
protected abstract Regex AnimationMatch
{
get;
}
protected AbstractAnimationReply(CoreContext db)
{
_db = db;
}
protected abstract string GetAnimationName();
protected virtual bool CanHandle() => true;
@@ -30,26 +40,31 @@ namespace West.Entertainment.Handler.Fun
var animationFileOption = await GetAnimationFileOption();
animationFileOption.SetValue(animation.FileId);
Db.MarkAsModified(animationFileOption);
_db.MarkAsModified(animationFileOption);
}
[Message(InChat.Public, MessageFlag.HasText)]
protected virtual async Task<bool> HandleAnimation(int? replyToMessageId = null)
// ReSharper disable once MemberCanBeProtected.Global
public virtual async Task<bool> HandleAnimation()
{
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false;
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: replyToMessageId ?? Message?.MessageId);
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
return true;
}
private async Task<BotUserValue> GetAnimationFileOption()
{
return await Db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
}
protected AbstractAnimationReply(CoreContext db) : base(db) {}
private async Task<bool> CanPunish()
{
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
}
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
}
}