From 696920b224ea166c23d820ef1ba02f2c8f50b147 Mon Sep 17 00:00:00 2001 From: watano1168 Date: Fri, 12 Jan 2024 00:06:35 +0900 Subject: [PATCH] =?UTF-8?q?RareReply:=20=E4=BA=BA=E9=96=93=E5=91=B3?= =?UTF-8?q?=E3=82=92=E6=8C=81=E3=81=9F=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/MasterManager.cs | 4 +++- Common/RandomUtility.cs | 16 ++++++++++++++++ DiscordEntry.cs | 2 +- Events/RareReplyPresenter.cs | 34 ++++++++++++++++++++++++++++++++++ Events/ReplyPresenter.cs | 33 --------------------------------- 5 files changed, 54 insertions(+), 35 deletions(-) create mode 100644 Common/RandomUtility.cs create mode 100644 Events/RareReplyPresenter.cs delete mode 100644 Events/ReplyPresenter.cs diff --git a/Common/MasterManager.cs b/Common/MasterManager.cs index 29bc2a8..8051420 100644 --- a/Common/MasterManager.cs +++ b/Common/MasterManager.cs @@ -2,7 +2,9 @@ public static class MasterManager { - public static double ReplyRate => 0.05; + public static float ReplyRate => 0.05f; + public static float ReplyMaxDelay => 2f; + public static float TypingMaxDelay => 1f; public static IReadOnlyList<(float rate, string message)> ReplyMessages => new List<(float rate, string message)> { diff --git a/Common/RandomUtility.cs b/Common/RandomUtility.cs new file mode 100644 index 0000000..60264d3 --- /dev/null +++ b/Common/RandomUtility.cs @@ -0,0 +1,16 @@ +namespace Approvers.King.Common; + +public static class RandomUtility +{ + private static readonly Random Random = new(); + + public static float GetRandomFloat(float max) + { + return GetRandomFloat(0f, max); + } + + public static float GetRandomFloat(float min, float max) + { + return (float)(min + (max - min) * Random.NextDouble()); + } +} \ No newline at end of file diff --git a/DiscordEntry.cs b/DiscordEntry.cs index 342d67f..b87efb7 100644 --- a/DiscordEntry.cs +++ b/DiscordEntry.cs @@ -17,6 +17,6 @@ private static async Task OnMessageReceived(SocketMessage message) if (message is not SocketUserMessage userMessage || userMessage.Author.IsBot) return; // 発言 - await DiscordManager.ExecuteAsync(userMessage); + await DiscordManager.ExecuteAsync(userMessage); } } \ No newline at end of file diff --git a/Events/RareReplyPresenter.cs b/Events/RareReplyPresenter.cs new file mode 100644 index 0000000..b63350d --- /dev/null +++ b/Events/RareReplyPresenter.cs @@ -0,0 +1,34 @@ +using Approvers.King.Common; +using Discord; + +namespace Approvers.King.Events; + +public class RareReplyPresenter : DiscordMessagePresenterBase +{ + protected override async Task MainAsync() + { + if (RandomUtility.GetRandomFloat(1f) > MasterManager.ReplyRate) return; + + await Task.Delay(TimeSpan.FromSeconds(RandomUtility.GetRandomFloat(MasterManager.ReplyMaxDelay))); + using (Message.Channel.EnterTypingState()) + { + await Task.Delay(TimeSpan.FromSeconds(RandomUtility.GetRandomFloat(MasterManager.TypingMaxDelay))); + await Message.ReplyAsync(PickRandomMessage()); + } + } + + private static string PickRandomMessage() + { + var totalRate = MasterManager.ReplyMessages.Sum(x => x.rate); + var value = RandomUtility.GetRandomFloat(totalRate); + + foreach (var (rate, message) in MasterManager.ReplyMessages) + { + if (value < rate) return message; + + value -= rate; + } + + return MasterManager.ReplyMessages[^1].message; + } +} \ No newline at end of file diff --git a/Events/ReplyPresenter.cs b/Events/ReplyPresenter.cs deleted file mode 100644 index a2dae3c..0000000 --- a/Events/ReplyPresenter.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Approvers.King.Common; -using Discord; - -namespace Approvers.King.Events; - -public class ReplyPresenter : DiscordMessagePresenterBase -{ - private static readonly Random Random = new(); - - protected override async Task MainAsync() - { - var rand = Random.NextDouble(); - Console.WriteLine($"rand: {rand}"); - if (rand > MasterManager.ReplyRate) return; - - await Message.ReplyAsync(PickRandomMessage()); - } - - private static string PickRandomMessage() - { - var totalRate = MasterManager.ReplyMessages.Sum(x => x.rate); - var value = Random.NextDouble() * totalRate; - - foreach (var (rate, message) in MasterManager.ReplyMessages) - { - if (value < rate) return message; - - value -= rate; - } - - return MasterManager.ReplyMessages[^1].message; - } -} \ No newline at end of file