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