Skip to content

Commit

Permalink
feat: add eat-break function impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Jul 22, 2024
1 parent 5b14eab commit 75b5a7e
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 44 deletions.
6 changes: 4 additions & 2 deletions src/Bot/BotInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/Bot/IBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace DXKumaBot.Bot;
public interface IBot
{
Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages,
Possible<GroupMessageEvent, TgMessage> source);
Possible<GroupMessageEvent, TgMessage>? source);
}
6 changes: 3 additions & 3 deletions src/Bot/Lagrange/QQBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public QqBot()
}

public async Task SendMessageAsync(MessageReceivedEventArgs messageToReply, MessagePair messages,
Possible<GroupMessageEvent, TgMessage> source)
Possible<GroupMessageEvent, TgMessage>? 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<MessageReceivedEventArgs>? MessageReceived;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/Bot/Message/TextMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/Bot/MessageRecivedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 8 additions & 14 deletions src/Bot/Telegram/TgBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupMessageEvent, TgMessage> source)
Possible<GroupMessageEvent, TgMessage>? 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<MessageReceivedEventArgs>? MessageReceived;
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/Functions/Cum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
18 changes: 18 additions & 0 deletions src/Functions/EatBreak.cs
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion src/Functions/LoveYou.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
31 changes: 17 additions & 14 deletions src/Functions/Choose.cs → src/Functions/Roll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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();
}
2 changes: 1 addition & 1 deletion src/Functions/WannaCao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion src/Utils/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static int Choose(this Random random, IList<int> weights)
for (int index = 0; index < weights.Count; index++)
{
randomResult -= weights[index];
if (randomResult < weights[index])
if (randomResult < 0)
{
return index;
}
Expand Down

0 comments on commit 75b5a7e

Please sign in to comment.