Skip to content

Commit

Permalink
feat: add more impl of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Jul 22, 2024
1 parent d0dcc61 commit b116490
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Bot/BotInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ public sealed class BotInstance(Config config)
private static void RegisterFunctions()
{
LoveYou loveYou = new();
WannaCao wannaCao = new();
Cum cum = new();
Choose choose = new();

loveYou.Register();
wannaCao.Register();
cum.Register();
choose.Register();
}

private void RegisterEvents()
Expand Down
6 changes: 3 additions & 3 deletions src/Bot/Telegram/TgBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private async Task SendMessageAsync(long id, MessagePair messages, int? threadId
{
if (messages.Media is null)
{
await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId, ParseMode.MarkdownV2,
await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId,
replyParameters: source is null ? default(ReplyParameters) : source);
return;
}
Expand All @@ -62,11 +62,11 @@ await _bot.SendTextMessageAsync(id, messages.Text!.Text, threadId, ParseMode.Mar
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,
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,
replyParameters: source is null ? default(ReplyParameters) : source);
break;
default:
Expand Down
56 changes: 56 additions & 0 deletions src/Functions/Choose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using DXKumaBot.Bot;
using DXKumaBot.Bot.Message;
using System.Text.RegularExpressions;

namespace DXKumaBot.Functions;

public sealed partial class Choose : RegexFunctionBase
{
protected override async Task Main(object? sender, MessageReceivedEventArgs args)
{
Match match = MessageRegex().Match(args.Text);
HashSet<string> values = [];
foreach (Group group in match.Groups)
{
if (group.Index is 0)
{
continue;
}

foreach (Capture capture in group.Captures)
{
values.Add(capture.Value);
}
}

switch (values.Count)
{
case 0:
{
string filePath = Path.Combine("Static", nameof(Choose), "1.png");
MediaMessage message = new(MediaType.Photo, filePath);
await args.Reply(new("没有选项要让迪拉熊怎么选嘛~", message));
break;
}
case 1:
{
string filePath = Path.Combine("Static", nameof(Choose), "1.png");
MediaMessage message = new(MediaType.Photo, filePath);
await args.Reply(new("就一个选项要让迪拉熊怎么选嘛~", message));
break;
}
default:
{
int index = Random.Shared.Next(values.Count);
string filePath = Path.Combine("Static", nameof(Choose), "0.png");
MediaMessage message = new(MediaType.Photo, filePath);
await args.Reply(new($"迪拉熊建议你选择“{values.ElementAt(index)}”呢~", message));
break;
}
}
}

[GeneratedRegex("^(?:.*?是)(.+?)(?:还是(.+?))+$",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
protected override partial Regex MessageRegex();
}
20 changes: 20 additions & 0 deletions src/Functions/Cum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using DXKumaBot.Bot;
using DXKumaBot.Bot.Message;
using DXKumaBot.Utils;
using System.Text.RegularExpressions;

namespace DXKumaBot.Functions;

public sealed partial class Cum : RegexFunctionBase
{
protected override async Task Main(object? sender, MessageReceivedEventArgs args)
{
int index = Random.Shared.Choose([9, 1]);
string filePath = Path.Combine("Static", nameof(Cum), $"{index}.png");
MediaMessage message = new(MediaType.Photo, filePath);
await args.Reply(message);
}

[GeneratedRegex("dlxcum", RegexOptions.IgnoreCase | RegexOptions.Compiled | 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 @@ -8,7 +8,7 @@ public sealed partial class LoveYou : RegexFunctionBase
{
protected override async Task Main(object? sender, MessageReceivedEventArgs args)
{
string filePath = Path.Combine("Static", "LoveYou", "0.png");
string filePath = Path.Combine("Static", nameof(LoveYou), "0.png");
MediaMessage message = new(MediaType.Photo, filePath);
await args.Reply(new("迪拉熊也喜欢你❤️", message));
}
Expand Down
37 changes: 37 additions & 0 deletions src/Functions/WannaCao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DXKumaBot.Bot;
using DXKumaBot.Bot.Message;
using DXKumaBot.Utils;
using System.Text.RegularExpressions;

namespace DXKumaBot.Functions;

public sealed partial class WannaCao : RegexFunctionBase
{
private readonly (string, int)[] _replies =
[
("变态!!!", 0),
("走开!!!", 0),
("别靠近迪拉熊!!!", 0),
("迪拉熊不和你玩了!", 0),
("信不信迪拉熊吃你绝赞!", 0),
("信不信迪拉熊吃你星星!", 0),
("你不能这样对迪拉熊!", 0),
("迪拉熊不想理你了,哼!", 0),
("不把白潘AP了就别想!", 0),
("……你会对迪拉熊负责的,对吧?", 1)
];

private readonly int[] _weights = [11, 11, 11, 11, 11, 11, 11, 11, 11, 1];

protected override async Task Main(object? sender, MessageReceivedEventArgs args)
{
int index = Random.Shared.Choose(_weights);
(string Text, int PhotoIndex) reply = _replies[index];
string filePath = Path.Combine("Static", nameof(WannaCao), $"{reply.PhotoIndex}.png");
MediaMessage message = new(MediaType.Photo, filePath);
await args.Reply(new(reply.Text, message));
}

[GeneratedRegex("^(香草|想草)(迪拉熊|dlx)$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)]
protected override partial Regex MessageRegex();
}
20 changes: 20 additions & 0 deletions src/Utils/RandomExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace DXKumaBot.Utils;

public static class RandomExtensions
{
public static int Choose(this Random random, IList<int> weights)
{
int totalWeight = weights.Sum();
int randomResult = random.Next(totalWeight);
for (int index = 0; index < weights.Count; index++)
{
randomResult -= weights[index];
if (randomResult < weights[index])
{
return index;
}
}

return -1;
}
}

0 comments on commit b116490

Please sign in to comment.