Skip to content

Commit

Permalink
RareReply: 人間味を持たせる
Browse files Browse the repository at this point in the history
  • Loading branch information
2RiniaR committed Jan 11, 2024
1 parent fe37f6e commit 696920b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
4 changes: 3 additions & 1 deletion Common/MasterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>
{
Expand Down
16 changes: 16 additions & 0 deletions Common/RandomUtility.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 1 addition & 1 deletion DiscordEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ private static async Task OnMessageReceived(SocketMessage message)
if (message is not SocketUserMessage userMessage || userMessage.Author.IsBot) return;

// 発言
await DiscordManager.ExecuteAsync<ReplyPresenter>(userMessage);
await DiscordManager.ExecuteAsync<RareReplyPresenter>(userMessage);
}
}
34 changes: 34 additions & 0 deletions Events/RareReplyPresenter.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 0 additions & 33 deletions Events/ReplyPresenter.cs

This file was deleted.

0 comments on commit 696920b

Please sign in to comment.