Skip to content

Commit

Permalink
feat: Finishing reply method
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Jul 22, 2024
1 parent 8a9b0c3 commit ed56a21
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 27 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/[email protected].1
- uses: actions/[email protected].7
- name: Setup .NET
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Build
run: dotnet publish -c Release
- name: Upload Artifact
uses: actions/[email protected].1
uses: actions/[email protected].4
with:
path: src/bin
6 changes: 5 additions & 1 deletion src/Bot/IBot.cs
Original file line number Diff line number Diff line change
@@ -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<GroupMessageEvent, TgMessage> source);
}
30 changes: 21 additions & 9 deletions src/Bot/Lagrange/QQBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<GroupMessageEvent, TgMessage> source)
{
await SendMessageAsync(messageToReply.QqMessage!.Chain.GroupUin, messages);
await SendMessageAsync(messageToReply.QqMessage!.Chain.GroupUin, messages.Text!, messages.Media,
((GroupMessageEvent?)source)?.Chain);
}

public event AsyncEventHandler<MessageReceivedEventArgs>? 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)
Expand Down Expand Up @@ -113,23 +119,29 @@ 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)
{
throw new ArgumentNullException(nameof(id));
}

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);
Expand All @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Bot/Message/TextMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Bot/MessageRecivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 13 additions & 6 deletions src/Bot/Telegram/TgBot.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<GroupMessageEvent, TgMessage> 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<MessageReceivedEventArgs>? MessageReceived;
Expand All @@ -45,22 +49,25 @@ 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;
}

InputFile file = InputFile.FromStream(File.OpenRead(messages.Media.Path));
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));
Expand Down
2 changes: 1 addition & 1 deletion src/DXKumaBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Draw/Best50.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static async Task<MemoryStream> 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"));
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/AsyncEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace DXKumaBot.Utils;

public delegate Task AsyncEventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
public delegate Task AsyncEventHandler<in TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
4 changes: 2 additions & 2 deletions src/Utils/ByteExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> bytes, bool lower = true, bool space = false)
private static string Hex(this Span<byte> bytes, bool lower = true, bool space = false)
{
return Hex((ReadOnlySpan<byte>)bytes, lower, space);
}

public static string Hex(this ReadOnlySpan<byte> bytes, bool lower = true, bool space = false)
private static string Hex(this ReadOnlySpan<byte> bytes, bool lower = true, bool space = false)
{
return space
? HexInternal<WithSpaceHexByteStruct>(bytes, lower)
Expand Down

0 comments on commit ed56a21

Please sign in to comment.