Updated 8ball

* Reduce permissions to Loser
* Add rate limit options
* Use the FormatUsername() extension method
* Convert to a switch expression
* Reformat
* Namespace
This commit is contained in:
barelyprofessional
2026-01-08 20:03:44 -06:00
parent 6cdb7b6702
commit 82da292cd8

View File

@@ -1,21 +1,26 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using KfChatDotNetBot; using KfChatDotNetBot.Extensions;
using KfChatDotNetBot.Commands;
using KfChatDotNetBot.Models; using KfChatDotNetBot.Models;
using KfChatDotNetBot.Models.DbModels; using KfChatDotNetBot.Models.DbModels;
using KfChatDotNetWsClient.Models.Events; using KfChatDotNetWsClient.Models.Events;
using RandN; using RandN;
using RandN.Compat; using RandN.Compat;
namespace KfChatDotNetBot.Commands;
public class EightBallCommand : ICommand public class EightBallCommand : ICommand
{ {
public List<Regex> Patterns => [ public List<Regex> Patterns => [
new Regex("^8ball", RegexOptions.IgnoreCase) new Regex("^8ball", RegexOptions.IgnoreCase)
]; ];
public string? HelpText => "Ask the magic 8-ball a question"; public string? HelpText => "Ask the magic 8-ball a question";
public UserRight RequiredRight => UserRight.Guest; public UserRight RequiredRight => UserRight.Loser;
public TimeSpan Timeout => TimeSpan.FromSeconds(10); public TimeSpan Timeout => TimeSpan.FromSeconds(10);
public RateLimitOptionsModel? RateLimitOptions => null; public RateLimitOptionsModel? RateLimitOptions => new RateLimitOptionsModel
{
MaxInvocations = 3,
Window = TimeSpan.FromSeconds(15)
};
private static readonly string[] AnswersYes = [ private static readonly string[] AnswersYes = [
"Yes, definitely.", "Yes, definitely.",
@@ -101,23 +106,16 @@ public class EightBallCommand : ICommand
public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx)
{ {
var random = RandomShim.Create(StandardRng.Create()); var random = RandomShim.Create(StandardRng.Create());
string response;
var outcome = random.Next(0, 110); var outcome = random.Next(0, 110);
if (outcome < 50) var response = outcome switch
{ {
response = AnswersYes[random.Next(AnswersYes.Length)]; < 50 => AnswersYes[random.Next(AnswersYes.Length)],
} < 100 => AnswersNo[random.Next(AnswersNo.Length)],
else if (outcome < 100) _ => AnswersUncertain[random.Next(AnswersUncertain.Length)]
{ };
response = AnswersNo[random.Next(AnswersNo.Length)];
}
else
{
response = AnswersUncertain[random.Next(AnswersUncertain.Length)];
}
await botInstance.SendChatMessageAsync($"@{user.KfUsername}, {response}", true); await botInstance.SendChatMessageAsync($"{user.FormatUsername()}, {response}", true);
} }
} }