From 75b5a7e951dfa71a24fbe25cec11424023b9bd8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=88=E7=BA=BE?= <100036558+Yuisyuu@users.noreply.github.com> Date: Mon, 22 Jul 2024 19:34:02 +0800 Subject: [PATCH] feat: add eat-break function impl --- src/Bot/BotInstance.cs | 6 ++++-- src/Bot/IBot.cs | 2 +- src/Bot/Lagrange/QQBot.cs | 6 +++--- src/Bot/Message/TextMessage.cs | 5 +---- src/Bot/MessageRecivedEventArgs.cs | 4 ++-- src/Bot/Telegram/TgBot.cs | 22 +++++++------------- src/Functions/Cum.cs | 2 +- src/Functions/EatBreak.cs | 18 ++++++++++++++++ src/Functions/LoveYou.cs | 2 +- src/Functions/{Choose.cs => Roll.cs} | 31 +++++++++++++++------------- src/Functions/WannaCao.cs | 2 +- src/Utils/RandomExtensions.cs | 2 +- 12 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 src/Functions/EatBreak.cs rename src/Functions/{Choose.cs => Roll.cs} (57%) diff --git a/src/Bot/BotInstance.cs b/src/Bot/BotInstance.cs index 851835f..0278c37 100644 --- a/src/Bot/BotInstance.cs +++ b/src/Bot/BotInstance.cs @@ -17,12 +17,14 @@ private static void RegisterFunctions() LoveYou loveYou = new(); WannaCao wannaCao = new(); Cum cum = new(); - Choose choose = new(); + Roll roll = new(); + EatBreak eatBreak = new(); loveYou.Register(); wannaCao.Register(); cum.Register(); - choose.Register(); + roll.Register(); + eatBreak.Register(); } private void RegisterEvents() diff --git a/src/Bot/IBot.cs b/src/Bot/IBot.cs index b626c11..7b9d410 100644 --- a/src/Bot/IBot.cs +++ b/src/Bot/IBot.cs @@ -8,5 +8,5 @@ namespace DXKumaBot.Bot; public interface IBot { Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages, - Possible source); + Possible? source); } \ No newline at end of file diff --git a/src/Bot/Lagrange/QQBot.cs b/src/Bot/Lagrange/QQBot.cs index bfe4b1a..5da7a38 100644 --- a/src/Bot/Lagrange/QQBot.cs +++ b/src/Bot/Lagrange/QQBot.cs @@ -32,10 +32,10 @@ public QqBot() } public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages, - Possible source) + Possible? source = null) { await SendMessageAsync(messageToReply.QqMessage!.Chain.GroupUin, messages.Text!, messages.Media, - ((GroupMessageEvent?)source)?.Chain); + source is null ? default : ((GroupMessageEvent?)source)?.Chain); } public event AsyncEventHandler? MessageReceived; @@ -127,7 +127,7 @@ private async Task SendMessageAsync(uint? id, string? text = null, MediaMessage? throw new ArgumentNullException(nameof(id)); } - MessageBuilder messageBuilder = MessageBuilder.Group((uint)id); + MessageBuilder messageBuilder = MessageBuilder.Group(id.Value); if (source is not null) { messageBuilder.Forward(source); diff --git a/src/Bot/Message/TextMessage.cs b/src/Bot/Message/TextMessage.cs index 6a9545a..a300dad 100644 --- a/src/Bot/Message/TextMessage.cs +++ b/src/Bot/Message/TextMessage.cs @@ -6,10 +6,7 @@ public class TextMessage public static implicit operator TextMessage(string text) { - return new() - { - Text = text - }; + return new() { Text = text }; } public static implicit operator string(TextMessage message) diff --git a/src/Bot/MessageRecivedEventArgs.cs b/src/Bot/MessageRecivedEventArgs.cs index 33c8923..e2de016 100644 --- a/src/Bot/MessageRecivedEventArgs.cs +++ b/src/Bot/MessageRecivedEventArgs.cs @@ -24,9 +24,9 @@ public MessageReceivedEventArgs(IBot bot, TgMessage message) public TgMessage? TgMessage { get; } public string Text => QqMessage?.Chain.ToPreviewText() ?? TgMessage?.Text ?? throw new NullReferenceException(); - public async Task Reply(MessagePair messages) + public async Task Reply(MessagePair messages, bool noReply = false) { await _bot.SendMessageAsync(this, messages, - QqMessage is null ? TgMessage ?? throw new NullReferenceException() : QqMessage); + noReply ? default : QqMessage is null ? TgMessage ?? throw new NullReferenceException() : QqMessage); } } \ No newline at end of file diff --git a/src/Bot/Telegram/TgBot.cs b/src/Bot/Telegram/TgBot.cs index a079486..5050a80 100644 --- a/src/Bot/Telegram/TgBot.cs +++ b/src/Bot/Telegram/TgBot.cs @@ -16,14 +16,15 @@ public class TgBot(TelegramConfig config) : IBot config.Proxy.Enabled ? new(new HttpClientHandler { Proxy = new WebProxy(config.Proxy.Url, true) }) : default); public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages, - Possible source) + Possible? source) { if (messageToReply.TgMessage is null) { throw new ArgumentNullException(nameof(messageToReply)); } - await SendMessageAsync(messageToReply.TgMessage.Chat.Id, messages, source: source); + await SendMessageAsync(messageToReply.TgMessage.Chat.Id, messages, + source: source is null ? default(TgMessage?) : source); } public event Utils.AsyncEventHandler? MessageReceived; @@ -32,15 +33,8 @@ public void Run() { _bot.StartReceiving(async (bot, update, _) => { - if (update is not - { - Type: UpdateType.Message, - Message: - { - Type: MessageType.Text, - Text: not null - } - } || MessageReceived is null) + if (update is not { Type: UpdateType.Message, Message: { Type: MessageType.Text, Text: not null } } || + MessageReceived is null) { return; } @@ -54,7 +48,7 @@ private async Task SendMessageAsync(long id, MessagePair messages, int? threadId if (messages.Media is null) { await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId, - replyParameters: source is null ? default(ReplyParameters) : source); + replyParameters: source is null ? default(ReplyParameters?) : source); return; } @@ -63,11 +57,11 @@ await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId, { case MediaType.Audio: await _bot.SendAudioAsync(id, file, threadId, messages.Text?.Text, - replyParameters: source is null ? default(ReplyParameters) : source); + replyParameters: source is null ? default(ReplyParameters?) : source); break; case MediaType.Photo: await _bot.SendPhotoAsync(id, file, threadId, messages.Text?.Text, - replyParameters: source is null ? default(ReplyParameters) : source); + replyParameters: source is null ? default(ReplyParameters?) : source); break; default: throw new ArgumentOutOfRangeException(nameof(messages)); diff --git a/src/Functions/Cum.cs b/src/Functions/Cum.cs index 2589802..f7edfba 100644 --- a/src/Functions/Cum.cs +++ b/src/Functions/Cum.cs @@ -15,6 +15,6 @@ protected override async Task Main(object? sender, MessageReceivedEventArgs args await args.Reply(message); } - [GeneratedRegex("dlxcum", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)] + [GeneratedRegex("dlxcum", RegexOptions.IgnoreCase | RegexOptions.Singleline)] protected override partial Regex MessageRegex(); } \ No newline at end of file diff --git a/src/Functions/EatBreak.cs b/src/Functions/EatBreak.cs new file mode 100644 index 0000000..6bf88e4 --- /dev/null +++ b/src/Functions/EatBreak.cs @@ -0,0 +1,18 @@ +using DXKumaBot.Bot; +using DXKumaBot.Bot.Message; +using System.Text.RegularExpressions; + +namespace DXKumaBot.Functions; + +public sealed partial class EatBreak : RegexFunctionBase +{ + protected override async Task Main(object? sender, MessageReceivedEventArgs args) + { + string filePath = Path.Combine("Static", nameof(EatBreak), "0.png"); + MediaMessage message = new(MediaType.Photo, filePath); + await args.Reply(new("谢谢~", message)); + } + + [GeneratedRegex("(绝赞(给|请)你吃|(给|请)你吃绝赞)", RegexOptions.IgnoreCase | RegexOptions.Singleline)] + protected override partial Regex MessageRegex(); +} \ No newline at end of file diff --git a/src/Functions/LoveYou.cs b/src/Functions/LoveYou.cs index ce8ef4a..eba1b87 100644 --- a/src/Functions/LoveYou.cs +++ b/src/Functions/LoveYou.cs @@ -13,6 +13,6 @@ protected override async Task Main(object? sender, MessageReceivedEventArgs args await args.Reply(new("迪拉熊也喜欢你❤️", message)); } - [GeneratedRegex("^(迪拉熊|dlx)我喜欢你$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)] + [GeneratedRegex("(迪拉熊|dlx)我喜欢你", RegexOptions.IgnoreCase | RegexOptions.Singleline)] protected override partial Regex MessageRegex(); } \ No newline at end of file diff --git a/src/Functions/Choose.cs b/src/Functions/Roll.cs similarity index 57% rename from src/Functions/Choose.cs rename to src/Functions/Roll.cs index 6db7967..79635c5 100644 --- a/src/Functions/Choose.cs +++ b/src/Functions/Roll.cs @@ -4,47 +4,50 @@ namespace DXKumaBot.Functions; -public sealed partial class Choose : RegexFunctionBase +public sealed partial class Roll : RegexFunctionBase { protected override async Task Main(object? sender, MessageReceivedEventArgs args) { - Match match = MessageRegex().Match(args.Text); + MatchCollection matches = MessageRegex().Matches(args.Text); List values = []; - foreach (Group group in match.Groups) + foreach (Match match in matches) { - if (group.Index is 0) + foreach (Group group in match.Groups) { - continue; - } + if (group.Index is 0) + { + continue; + } - foreach (Capture capture in group.Captures) - { - values.Add(capture.Value); + foreach (Capture capture in group.Captures) + { + values.Add(capture.Value); + } } } if (values.Count is 0) { - string filePath = Path.Combine("Static", nameof(Choose), "1.png"); + string filePath = Path.Combine("Static", nameof(Roll), "1.png"); MediaMessage message = new(MediaType.Photo, filePath); await args.Reply(new("没有选项要让迪拉熊怎么选嘛~", message)); } else if (values.Count is 1 || values.All(x => x == values[0])) { - string filePath = Path.Combine("Static", nameof(Choose), "1.png"); + string filePath = Path.Combine("Static", nameof(Roll), "1.png"); MediaMessage message = new(MediaType.Photo, filePath); await args.Reply(new("就一个选项要让迪拉熊怎么选嘛~", message)); } else { int index = Random.Shared.Next(values.Count); - string filePath = Path.Combine("Static", nameof(Choose), "0.png"); + string filePath = Path.Combine("Static", nameof(Roll), "0.png"); MediaMessage message = new(MediaType.Photo, filePath); await args.Reply(new($"迪拉熊建议你选择“{values[index]}”呢~", message)); } } - [GeneratedRegex("^(?:.*?是)(.+?)(?:还是(.+?))+$", - RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)] + [GeneratedRegex("(?:.*?是)(.+?)(?:还是(.+?))+", + RegexOptions.IgnoreCase | RegexOptions.Singleline)] protected override partial Regex MessageRegex(); } \ No newline at end of file diff --git a/src/Functions/WannaCao.cs b/src/Functions/WannaCao.cs index 1fb8b95..9fe9baa 100644 --- a/src/Functions/WannaCao.cs +++ b/src/Functions/WannaCao.cs @@ -32,6 +32,6 @@ protected override async Task Main(object? sender, MessageReceivedEventArgs args await args.Reply(new(reply.Text, message)); } - [GeneratedRegex("^(香草|想草)(迪拉熊|dlx)$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)] + [GeneratedRegex("(香草|想草)(迪拉熊|dlx)", RegexOptions.IgnoreCase | RegexOptions.Singleline)] protected override partial Regex MessageRegex(); } \ No newline at end of file diff --git a/src/Utils/RandomExtensions.cs b/src/Utils/RandomExtensions.cs index aa41b67..ee4bf39 100644 --- a/src/Utils/RandomExtensions.cs +++ b/src/Utils/RandomExtensions.cs @@ -9,7 +9,7 @@ public static int Choose(this Random random, IList weights) for (int index = 0; index < weights.Count; index++) { randomResult -= weights[index]; - if (randomResult < weights[index]) + if (randomResult < 0) { return index; }