Move fun commands to separate module.

This commit is contained in:
West14
2022-01-02 21:56:14 +02:00
parent d2077df114
commit 8d3e24295b
10 changed files with 126 additions and 93 deletions
@@ -1,39 +0,0 @@
#nullable enable
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Telegram.Bot;
namespace West.TelegramBot.ChatManagement.Handler.Fun
{
public class Bayan : AbstractAnimationReply
{
public Bayan(CoreContext db) : base(db) { }
protected override string GetAnimationName()
{
return "bayan";
}
[Command(InChat.Public, "setbayan", CommandParseMode.Both)]
public async Task HandleSetBayan()
{
await HandleSetAnimation();
}
[Message(InChat.Public, "(?i)баян|боян", MessageFlag.HasText | MessageFlag.IsReply, true)]
public async Task HandleBayan()
{
var msg = Message;
var replyMsg = msg?.ReplyToMessage!;
if (msg == null || !await HandleAnimation(replyMsg.MessageId))
{
return;
}
await Bot.DeleteMessageAsync(Chat.Id, msg.MessageId);
}
}
}
@@ -1,48 +0,0 @@
#nullable enable
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
namespace West.TelegramBot.ChatManagement.Handler.Fun
{
public class Python : AbstractAnimationReply
{
protected override string GetAnimationName()
{
return "python";
}
[Command(InChat.Public, "setpython", CommandParseMode.Both)]
public async Task HandleSetPython()
{
await HandleSetAnimation();
}
[Message(InChat.Public, "(?i)питон|пайтон|python|путхон", MessageFlag.HasText, true)]
public async Task HandlePython()
{
// var cmdEntity = Message?.Entities?.Find(e => e.Type == MessageEntityType.BotCommand);
// if (cmdEntity is { Length: > 0 })
// {
// return;
// }
if (Message!.Text!.Contains("/setpython"))
{
return;
}
await HandleAnimation();
}
private async Task<BotUserValue> GetPythonFileOption()
{
return await Db.UserValues.FindOrCreateOption(Chat.Id, "pythonFile");
}
public Python(CoreContext db) : base(db) { }
}
}
+13
View File
@@ -0,0 +1,13 @@
using Kruzya.TelegramBot.Core;
using West.TelegramBot.ChatManagement;
namespace West.Entertainment
{
public class Entertainment : Module
{
public Entertainment(Core core) : base(core)
{
core.EnsureLoaded<ChatManagement>();
}
}
}
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>TelegramBot.Entertainment</AssemblyName>
<RootNamespace>West.Entertainment</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj" />
<ProjectReference Include="..\ChatManagement\ChatManagement.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Handler" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Delete Files="$(OutDir)\TelegramBot.dll" />
<Delete Files="$(OutDir)\TelegramBot.ChatManagement.dll" />
</Target>
</Project>
@@ -1,17 +1,28 @@
#nullable enable
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using West.TelegramBot.ChatManagement.Handler;
namespace West.TelegramBot.ChatManagement.Handler.Fun
namespace West.Entertainment.Handler.Fun
{
public abstract class AbstractAnimationReply : ManagementHandler
{
protected abstract Regex AnimationMatch
{
get;
}
protected abstract string GetAnimationName();
protected virtual bool CanHandle() => true;
protected async Task HandleSetAnimation()
{
var animation = Message?.ReplyToMessage?.Animation;
@@ -22,12 +33,14 @@ namespace West.TelegramBot.ChatManagement.Handler.Fun
Db.MarkAsModified(animationFileOption);
}
protected async Task<bool> HandleAnimation(int? replyToMessageId = null)
[Message(InChat.Public, MessageFlag.HasText)]
protected virtual async Task<bool> HandleAnimation(int? replyToMessageId = null)
{
var anumationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (anumationFileId.IsNullOrEmpty()) return false;
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false;
await Bot.SendAnimationAsync(Chat.Id, anumationFileId,
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: replyToMessageId ?? Message?.MessageId);
return true;
}
@@ -0,0 +1,37 @@
#nullable enable
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Telegram.Bot;
namespace West.Entertainment.Handler.Fun
{
public class Bayan : AbstractAnimationReply
{
public Bayan(CoreContext db) : base(db) { }
protected override Regex AnimationMatch => new("^баян$", RegexOptions.IgnoreCase);
protected override string GetAnimationName() => "bayan";
[Command(InChat.Public, "setbayan", CommandParseMode.Both)]
public async Task HandleSetBayan() => await HandleSetAnimation();
protected override async Task<bool> HandleAnimation(int? replyToMessageId = null)
{
if (await base.HandleAnimation(replyToMessageId))
{
await Bot.DeleteMessageAsync(Chat.Id, Message!.MessageId);
}
return true;
}
protected override bool CanHandle() => Message?.ReplyToMessage != null;
}
}
@@ -0,0 +1,25 @@
#nullable enable
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
namespace West.Entertainment.Handler.Fun
{
public class Python : AbstractAnimationReply
{
protected override Regex AnimationMatch =>
new("питон|пайтон|python|путхон|питухон|петухон", RegexOptions.IgnoreCase);
protected override string GetAnimationName() => "python";
protected override bool CanHandle() => !Message!.Text!.Contains("/setpython");
[Command(InChat.Public, "setpython", CommandParseMode.Both)]
public async Task HandleSetPython() => await HandleSetAnimation();
public Python(CoreContext db) : base(db) { }
}
}