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
+1 -1
View File
@@ -8,7 +8,7 @@
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Core' " /> <PropertyGroup Condition=" '$(RunConfiguration)' == 'Core' " />
<ItemGroup> <ItemGroup>
<PackageReference Include="AleXr64.BotFramework" Version="0.6.3-gea779222d8" /> <PackageReference Include="AleXr64.BotFramework" Version="0.6.5-g35decc8b27" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -2,7 +2,7 @@
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
namespace West.TelegramBot.ChatManagement.Extensions namespace Kruzya.TelegramBot.Core.Extensions
{ {
public static class BotExtension public static class BotExtension
{ {
@@ -8,7 +8,6 @@ using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using West.TelegramBot.ChatManagement.Extensions;
namespace West.TelegramBot.ChatManagement.Handler namespace West.TelegramBot.ChatManagement.Handler
{ {
+1 -5
View File
@@ -1,13 +1,9 @@
using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core;
using West.TelegramBot.ChatManagement;
namespace West.Entertainment namespace West.Entertainment
{ {
public class Entertainment : Module public class Entertainment : Module
{ {
public Entertainment(Core core) : base(core) public Entertainment(Core core) : base(core) { }
{
// core.EnsureLoaded<ChatManagement>();
}
} }
} }
@@ -8,7 +8,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj" /> <ProjectReference Include="..\..\Core\Core.csproj" />
<ProjectReference Include="..\ChatManagement\ChatManagement.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -2,23 +2,33 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework;
using BotFramework.Enums;
using Castle.Core.Internal; using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot; using Telegram.Bot;
using West.TelegramBot.ChatManagement.Handler; using Telegram.Bot.Types;
namespace West.Entertainment.Handler.Fun 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 protected abstract Regex AnimationMatch
{ {
get; get;
} }
protected AbstractAnimationReply(CoreContext db)
{
_db = db;
}
protected abstract string GetAnimationName(); protected abstract string GetAnimationName();
protected virtual bool CanHandle() => true; protected virtual bool CanHandle() => true;
@@ -30,26 +40,31 @@ namespace West.Entertainment.Handler.Fun
var animationFileOption = await GetAnimationFileOption(); var animationFileOption = await GetAnimationFileOption();
animationFileOption.SetValue(animation.FileId); animationFileOption.SetValue(animation.FileId);
Db.MarkAsModified(animationFileOption); _db.MarkAsModified(animationFileOption);
} }
[Message(InChat.Public, MessageFlag.HasText)] // ReSharper disable once MemberCanBeProtected.Global
protected virtual async Task<bool> HandleAnimation(int? replyToMessageId = null) public virtual async Task<bool> HandleAnimation()
{ {
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty); var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false; if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false;
await Bot.SendAnimationAsync(Chat.Id, animationFileId, await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: replyToMessageId ?? Message?.MessageId); replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
return true; return true;
} }
private async Task<BotUserValue> GetAnimationFileOption() 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;
} }
} }
+16 -7
View File
@@ -13,17 +13,20 @@ namespace West.Entertainment.Handler.Fun
{ {
public Bayan(CoreContext db) : base(db) { } public Bayan(CoreContext db) : base(db) { }
protected override Regex AnimationMatch => new("^баян$", RegexOptions.IgnoreCase); protected override Regex AnimationMatch
=> new("^баян$", RegexOptions.IgnoreCase);
protected override string GetAnimationName() => "bayan";
protected override string GetAnimationName()
=> "bayan";
[Command(InChat.Public, "setbayan", CommandParseMode.Both)] [Command(InChat.Public, "setbayan", CommandParseMode.Both)]
public async Task HandleSetBayan() => await HandleSetAnimation(); public async Task HandleSetBayan()
=> await HandleSetAnimation();
protected override async Task<bool> HandleAnimation(int? replyToMessageId = null) [Message(InChat.Public, MessageFlag.HasText)]
public override async Task<bool> HandleAnimation()
{ {
if (await base.HandleAnimation(replyToMessageId)) if (await base.HandleAnimation())
{ {
await Bot.DeleteMessageAsync(Chat.Id, Message!.MessageId); await Bot.DeleteMessageAsync(Chat.Id, Message!.MessageId);
} }
@@ -31,7 +34,13 @@ namespace West.Entertainment.Handler.Fun
return true; return true;
} }
protected override bool CanHandle() => Message?.ReplyToMessage != null; protected override int? GetMessageIdToReply()
{
return Message!.ReplyToMessage!.MessageId;
}
protected override bool CanHandle()
=> Message?.ReplyToMessage != null;
} }
} }
+11 -3
View File
@@ -13,12 +13,20 @@ namespace West.Entertainment.Handler.Fun
protected override Regex AnimationMatch => protected override Regex AnimationMatch =>
new("питон|пайтон|python|путхон|питухон|петухон", RegexOptions.IgnoreCase); new("питон|пайтон|python|путхон|питухон|петухон", RegexOptions.IgnoreCase);
protected override string GetAnimationName() => "python"; protected override string GetAnimationName()
=> "python";
protected override bool CanHandle() => !Message!.Text!.Contains("/setpython"); protected override bool CanHandle()
=> !Message!.Text!.Contains("/setpython");
[Command(InChat.Public, "setpython", CommandParseMode.Both)] [Command(InChat.Public, "setpython", CommandParseMode.Both)]
public async Task HandleSetPython() => await HandleSetAnimation(); public async Task HandleSetPython()
=> await HandleSetAnimation();
[Message(InChat.Public, MessageFlag.HasText)]
public override async Task<bool> HandleAnimation()
=> await base.HandleAnimation();
public Python(CoreContext db) : base(db) { } public Python(CoreContext db) : base(db) { }
} }