Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interact: メンションされたら返信する機能を実装 #2

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions DiscordEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ private static async Task OnMessageReceived(SocketMessage message)
// botは弾く
if (message is not SocketUserMessage userMessage || userMessage.Author.IsBot) return;

if (userMessage.MentionedUsers.Any(x => x.Id == DiscordManager.Client.CurrentUser.Id))
{
// 返信
await DiscordManager.ExecuteAsync<InteractReplyPresenter>(userMessage);
return;
}

// 発言
await DiscordManager.ExecuteAsync<RareReplyPresenter>(userMessage);
}
Expand Down
17 changes: 17 additions & 0 deletions Events/InteractReplyPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Approvers.King.Common;
using Discord;

namespace Approvers.King.Events;

public class InteractReplyPresenter : DiscordMessagePresenterBase
{
protected override async Task MainAsync()
{
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(MessageUtility.PickRandomMessage());
}
}
}
17 changes: 1 addition & 16 deletions Events/RareReplyPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,7 @@ protected override async Task MainAsync()
using (Message.Channel.EnterTypingState())
{
await Task.Delay(TimeSpan.FromSeconds(RandomUtility.GetRandomFloat(MasterManager.TypingMaxDelay)));
await Message.ReplyAsync(PickRandomMessage());
await Message.ReplyAsync(MessageUtility.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;
}
}
21 changes: 21 additions & 0 deletions Events/ReplyUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Approvers.King.Common;

namespace Approvers.King.Events;

public static class MessageUtility
{
public 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;
}
}