Skip to content

Commit

Permalink
Merge pull request #3 from approvers/feature/rinia/silent/001
Browse files Browse the repository at this point in the history
Silent: 一定時間黙らせるコマンドの実装
  • Loading branch information
2RiniaR authored Jan 13, 2024
2 parents d69374a + 1ad10b9 commit b7972e9
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 4 deletions.
26 changes: 26 additions & 0 deletions Common/MasterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ public static class MasterManager
(1f, "noob"),
(1f, "それね、ちょっと分かる"),
};

public static string SilentCommandReplyMessage => "(いっそうの気配が{0}から消え去った)";

public static IReadOnlyList<string> SilentTriggerMessages => new List<string>
{
"だまれ",
"黙れ",
"だまって",
"黙って",
"だまろう",
"黙ろう",
"damare",
"しゃべるな",
"喋るな",
"しゃべんな",
"喋んな",
"帰れ",
"かえれ",
"帰って",
"かえって",
"帰ろう",
"かえろう",
"kaere",
};

public static TimeSpan SilentTimeSpan => TimeSpan.FromMinutes(5);
}
21 changes: 21 additions & 0 deletions Common/PresenterBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Discord;
using Discord.WebSocket;

namespace Approvers.King.Common;

Expand Down Expand Up @@ -28,4 +29,24 @@ public static Embed ErrorEmbed(string message)
.WithCurrentTimestamp()
.Build();
}

public static class Format
{
private static readonly string[] SanitizeTarget = { "\\", "*", "_", "~", "`", ".", ":", "/", ">", "|", "#" };

public static string Sanitize(string text)
{
foreach (var target in SanitizeTarget)
{
text = text.Replace(target, "\\" + target);
}

return text;
}

public static string UserName(IUser user)
{
return Sanitize((user as SocketGuildUser)?.Nickname ?? user.Username);
}
}
}
23 changes: 23 additions & 0 deletions Common/SilentManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Approvers.King.Common;

public static class SilentManager
{
private static readonly Dictionary<ulong, DateTime> SilentUsers = new();

/// <summary>
/// 一定時間黙らせる
/// </summary>
public static void SetSilent(ulong userId, TimeSpan timeSpan)
{
var expireTime = TimeManager.GetNow() + timeSpan;
SilentUsers[userId] = expireTime;
}

/// <summary>
/// 黙っている最中か
/// </summary>
public static bool IsSilent(ulong userId)
{
return SilentUsers.TryGetValue(userId, out var expireTime) && !TimeManager.IsExpired(expireTime);
}
}
21 changes: 21 additions & 0 deletions Common/TimeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Approvers.King.Common;

public static class TimeManager
{
/// <summary>
/// アプリ内の現在時刻を取得する
/// デバッグ機能により操作されることがある
/// </summary>
public static DateTime GetNow()
{
return DateTime.Now.ToLocalTime();
}

/// <summary>
/// 過ぎていればtrue
/// </summary>
public static bool IsExpired(DateTime time)
{
return GetNow() > time;
}
}
File renamed without changes.
3 changes: 2 additions & 1 deletion Events/RareReplyPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class RareReplyPresenter : DiscordMessagePresenterBase
{
protected override async Task MainAsync()
{
if (RandomUtility.GetRandomFloat(1f) > MasterManager.ReplyRate) return;
if (SilentManager.IsSilent(Message.Author.Id) ||
RandomUtility.GetRandomFloat(1f) > MasterManager.ReplyRate) return;

await Task.Delay(TimeSpan.FromSeconds(RandomUtility.GetRandomFloat(MasterManager.ReplyMaxDelay)));
using (Message.Channel.EnterTypingState())
Expand Down
16 changes: 16 additions & 0 deletions Events/SilentCommandPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Approvers.King.Common;
using Discord;

namespace Approvers.King.Events;

public class SilentCommandPresenter : DiscordMessagePresenterBase
{
protected override async Task MainAsync()
{
SilentManager.SetSilent(Message.Author.Id, MasterManager.SilentTimeSpan);
var message = string.Format(
MasterManager.SilentCommandReplyMessage,
MentionUtils.MentionUser(Message.Author.Id));
await Message.ReplyAsync(message);
}
}
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Approvers.King.Common;
using Approvers.King.Triggers;

namespace Approvers.King;

Expand All @@ -13,7 +14,7 @@ private static async Task BuildAsync(string[] args)
{
await DiscordManager.InitializeAsync();

DiscordEntry.RegisterEvents();
DiscordTrigger.RegisterEvents();

// 永久に待つ
await Task.Delay(-1);
Expand Down
11 changes: 9 additions & 2 deletions DiscordEntry.cs → Triggers/DiscordTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using Approvers.King.Events;
using Discord.WebSocket;

namespace Approvers.King;
namespace Approvers.King.Triggers;

public static class DiscordEntry
public static class DiscordTrigger
{
public static void RegisterEvents()
{
Expand All @@ -18,6 +18,13 @@ private static async Task OnMessageReceived(SocketMessage message)

if (userMessage.MentionedUsers.Any(x => x.Id == DiscordManager.Client.CurrentUser.Id))
{
if (MasterManager.SilentTriggerMessages.Any(userMessage.Content.Contains))
{
// 黙らせる
await DiscordManager.ExecuteAsync<SilentCommandPresenter>(userMessage);
return;
}

// 返信
await DiscordManager.ExecuteAsync<InteractReplyPresenter>(userMessage);
return;
Expand Down

0 comments on commit b7972e9

Please sign in to comment.