From ed56a21beba8a4b66bca9670fe6c5704ada792f6 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 09:13:30 +0800 Subject: [PATCH] feat: Finishing reply method --- .github/workflows/dotnet.yml | 8 ++++---- src/Bot/IBot.cs | 6 +++++- src/Bot/Lagrange/QQBot.cs | 30 +++++++++++++++++++++--------- src/Bot/Message/TextMessage.cs | 2 +- src/Bot/MessageRecivedEventArgs.cs | 3 ++- src/Bot/Telegram/TgBot.cs | 19 +++++++++++++------ src/DXKumaBot.csproj | 2 +- src/Draw/Best50.cs | 2 +- src/Utils/AsyncEventHandler.cs | 2 +- src/Utils/ByteExtension.cs | 4 ++-- 10 files changed, 51 insertions(+), 27 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 4f8c077..f88d43e 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -15,14 +15,14 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4.1.1 + - uses: actions/checkout@v4.1.7 - name: Setup .NET - uses: actions/setup-dotnet@v4.0.0 + uses: actions/setup-dotnet@v4.0.1 with: - dotnet-version: 8.0.x + dotnet-version: 9.0.x - name: Build run: dotnet publish -c Release - name: Upload Artifact - uses: actions/upload-artifact@v4.3.1 + uses: actions/upload-artifact@v4.3.4 with: path: src/bin diff --git a/src/Bot/IBot.cs b/src/Bot/IBot.cs index 969c775..b626c11 100644 --- a/src/Bot/IBot.cs +++ b/src/Bot/IBot.cs @@ -1,8 +1,12 @@ using DXKumaBot.Bot.Message; +using DXKumaBot.Utils; +using Lagrange.Core.Event.EventArg; +using TgMessage = Telegram.Bot.Types.Message; namespace DXKumaBot.Bot; public interface IBot { - Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages); + Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages, + Possible source); } \ No newline at end of file diff --git a/src/Bot/Lagrange/QQBot.cs b/src/Bot/Lagrange/QQBot.cs index 580ea2f..bfe4b1a 100644 --- a/src/Bot/Lagrange/QQBot.cs +++ b/src/Bot/Lagrange/QQBot.cs @@ -4,9 +4,11 @@ using Lagrange.Core.Common; using Lagrange.Core.Common.Interface; using Lagrange.Core.Common.Interface.Api; +using Lagrange.Core.Event.EventArg; using Lagrange.Core.Message; using System.Text.Json; using System.Text.Json.Serialization; +using TgMessage = Telegram.Bot.Types.Message; namespace DXKumaBot.Bot.Lagrange; @@ -29,19 +31,23 @@ public QqBot() }, deviceInfo, _keyStore ?? new BotKeystore()); } - public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages) + public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages, + Possible source) { - await SendMessageAsync(messageToReply.QqMessage!.Chain.GroupUin, messages); + await SendMessageAsync(messageToReply.QqMessage!.Chain.GroupUin, messages.Text!, messages.Media, + ((GroupMessageEvent?)source)?.Chain); } public event AsyncEventHandler? MessageReceived; private void RegisterEvents() { +#if DEBUG _bot.Invoker.OnBotCaptchaEvent += (_, @event) => { Console.WriteLine(@event.ToString()); }; _bot.Invoker.OnBotOfflineEvent += (_, @event) => { Console.WriteLine(@event.ToString()); }; _bot.Invoker.OnBotOnlineEvent += (_, @event) => { Console.WriteLine(@event.ToString()); }; _bot.Invoker.OnBotNewDeviceVerify += (_, @event) => { Console.WriteLine(@event.ToString()); }; +#endif _bot.Invoker.OnGroupMessageReceived += async (sender, args) => { if (MessageReceived is null) @@ -113,7 +119,8 @@ private static void SaveKeystore(BotKeystore keystore) }); } - private async Task SendMessageAsync(uint? id, MessagePair messages) + private async Task SendMessageAsync(uint? id, string? text = null, MediaMessage? media = null, + MessageChain? source = null) { if (id is null) { @@ -121,15 +128,20 @@ private async Task SendMessageAsync(uint? id, MessagePair messages) } MessageBuilder messageBuilder = MessageBuilder.Group((uint)id); - if (messages.Text is not null) + if (source is not null) { - messageBuilder.Text(messages.Text); + messageBuilder.Forward(source); } - if (messages.Media is not null) + if (text is not null) { - byte[] data = await File.ReadAllBytesAsync(messages.Media.Path); - switch (messages.Media.Type) + messageBuilder.Text(text); + } + + if (media is not null) + { + byte[] data = await File.ReadAllBytesAsync(media.Path); + switch (media.Type) { case MediaType.Audio: messageBuilder.Record(data); @@ -138,7 +150,7 @@ private async Task SendMessageAsync(uint? id, MessagePair messages) messageBuilder.Image(data); break; default: - throw new ArgumentOutOfRangeException(nameof(messages)); + throw new ArgumentOutOfRangeException(nameof(media)); } } diff --git a/src/Bot/Message/TextMessage.cs b/src/Bot/Message/TextMessage.cs index e0d9456..6a9545a 100644 --- a/src/Bot/Message/TextMessage.cs +++ b/src/Bot/Message/TextMessage.cs @@ -2,7 +2,7 @@ namespace DXKumaBot.Bot.Message; public class TextMessage { - public string Text { get; set; } + public required string Text { get; set; } public static implicit operator TextMessage(string text) { diff --git a/src/Bot/MessageRecivedEventArgs.cs b/src/Bot/MessageRecivedEventArgs.cs index b62a575..33c8923 100644 --- a/src/Bot/MessageRecivedEventArgs.cs +++ b/src/Bot/MessageRecivedEventArgs.cs @@ -26,6 +26,7 @@ public MessageReceivedEventArgs(IBot bot, TgMessage message) public async Task Reply(MessagePair messages) { - await _bot.SendMessageAsync(this, messages); + await _bot.SendMessageAsync(this, messages, + 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 c732644..d1a5614 100644 --- a/src/Bot/Telegram/TgBot.cs +++ b/src/Bot/Telegram/TgBot.cs @@ -1,9 +1,12 @@ using DXKumaBot.Bot.Message; +using DXKumaBot.Utils; +using Lagrange.Core.Event.EventArg; using System.Net; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using File = System.IO.File; +using TgMessage = Telegram.Bot.Types.Message; namespace DXKumaBot.Bot.Telegram; @@ -12,14 +15,15 @@ public class TgBot(TelegramConfig config) : IBot private readonly TelegramBotClient _bot = new(config.BotToken, config.Proxy.Enabled ? new(new HttpClientHandler { Proxy = new WebProxy(config.Proxy.Url, true) }) : default); - public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages) + public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages, + Possible source) { if (messageToReply.TgMessage is null) { throw new ArgumentNullException(nameof(messageToReply)); } - await SendMessageAsync(messageToReply.TgMessage.Chat.Id, messages); + await SendMessageAsync(messageToReply.TgMessage.Chat.Id, messages, source: source); } public event Utils.AsyncEventHandler? MessageReceived; @@ -45,11 +49,12 @@ public void Run() }, (_, e, _) => { Console.WriteLine(e); }); } - private async Task SendMessageAsync(long id, MessagePair messages, int? threadId = null) + private async Task SendMessageAsync(long id, MessagePair messages, int? threadId = null, TgMessage? source = null) { if (messages.Media is null) { - await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId, ParseMode.MarkdownV2); + await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId, ParseMode.MarkdownV2, + replyParameters: source is null ? default(ReplyParameters) : source); return; } @@ -57,10 +62,12 @@ private async Task SendMessageAsync(long id, MessagePair messages, int? threadId switch (messages.Media.Type) { case MediaType.Audio: - await _bot.SendAudioAsync(id, file, threadId, messages.Text?.Text, ParseMode.MarkdownV2); + await _bot.SendAudioAsync(id, file, threadId, messages.Text?.Text, ParseMode.MarkdownV2, + replyParameters: source is null ? default(ReplyParameters) : source); break; case MediaType.Photo: - await _bot.SendPhotoAsync(id, file, threadId, messages.Text?.Text, ParseMode.MarkdownV2); + await _bot.SendPhotoAsync(id, file, threadId, messages.Text?.Text, ParseMode.MarkdownV2, + replyParameters: source is null ? default(ReplyParameters) : source); break; default: throw new ArgumentOutOfRangeException(nameof(messages)); diff --git a/src/DXKumaBot.csproj b/src/DXKumaBot.csproj index f6ffd7a..4db4398 100644 --- a/src/DXKumaBot.csproj +++ b/src/DXKumaBot.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net9.0 enable enable diff --git a/src/Draw/Best50.cs b/src/Draw/Best50.cs index 027a016..d1204ea 100644 --- a/src/Draw/Best50.cs +++ b/src/Draw/Best50.cs @@ -18,7 +18,7 @@ private static async Task DrawAsync(CommonB50 b50, CommonUserInfo (int X, int Y) b35Offset = (25, 795); (int X, int Y) pos = (0, 0); - foreach (CommonScore song in b50.Standard) + foreach (CommonScore song in b50.Standard!) { byte[] jacketImgBytes = await Resource.GetJacketAsync(song.Id); Image partImg = await Image.LoadAsync(Path.Combine("Static", "PartBase", $"{song.LevelIndex}.png")); diff --git a/src/Utils/AsyncEventHandler.cs b/src/Utils/AsyncEventHandler.cs index edaeae5..d928582 100644 --- a/src/Utils/AsyncEventHandler.cs +++ b/src/Utils/AsyncEventHandler.cs @@ -1,3 +1,3 @@ namespace DXKumaBot.Utils; -public delegate Task AsyncEventHandler(object sender, TEventArgs e) where TEventArgs : EventArgs; \ No newline at end of file +public delegate Task AsyncEventHandler(object sender, TEventArgs e) where TEventArgs : EventArgs; \ No newline at end of file diff --git a/src/Utils/ByteExtension.cs b/src/Utils/ByteExtension.cs index 9f8c606..ce27b79 100644 --- a/src/Utils/ByteExtension.cs +++ b/src/Utils/ByteExtension.cs @@ -12,12 +12,12 @@ public static string Hex(this byte[] bytes, bool lower = false, bool space = fal return Hex(bytes.AsSpan(), lower, space); } - public static string Hex(this Span bytes, bool lower = true, bool space = false) + private static string Hex(this Span bytes, bool lower = true, bool space = false) { return Hex((ReadOnlySpan)bytes, lower, space); } - public static string Hex(this ReadOnlySpan bytes, bool lower = true, bool space = false) + private static string Hex(this ReadOnlySpan bytes, bool lower = true, bool space = false) { return space ? HexInternal(bytes, lower)