Skip to content

Commit

Permalink
Merge pull request #2 from approvers/feature/rinia/interact/001
Browse files Browse the repository at this point in the history
Interact: メンションされたら返信する機能を実装
  • Loading branch information
2RiniaR authored Jan 11, 2024
2 parents a48d666 + 05838a6 commit d69374a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
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;
}
}

0 comments on commit d69374a

Please sign in to comment.