mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
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:
@@ -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.",
|
||||||
@@ -51,23 +56,23 @@ public class EightBallCommand : ICommand
|
|||||||
];
|
];
|
||||||
|
|
||||||
private static readonly string[] AnswersUncertain = [
|
private static readonly string[] AnswersUncertain = [
|
||||||
"Concentrate and ask again.",
|
"Concentrate and ask again.",
|
||||||
"Ask again later.",
|
"Ask again later.",
|
||||||
"Cannot predict now.",
|
"Cannot predict now.",
|
||||||
"Reply hazy, try again.",
|
"Reply hazy, try again.",
|
||||||
"Better not tell you now.",
|
"Better not tell you now.",
|
||||||
"It's unclear at the moment.",
|
"It's unclear at the moment.",
|
||||||
"I'm not sure.",
|
"I'm not sure.",
|
||||||
"Maybe...",
|
"Maybe...",
|
||||||
"That's a mystery.",
|
"That's a mystery.",
|
||||||
"The answer is unclear.",
|
"The answer is unclear.",
|
||||||
"It's hard to say.",
|
"It's hard to say.",
|
||||||
"I'm undecided.",
|
"I'm undecided.",
|
||||||
"The future is uncertain.",
|
"The future is uncertain.",
|
||||||
"It's anyone's guess.",
|
"It's anyone's guess.",
|
||||||
"I can't tell you right now.",
|
"I can't tell you right now.",
|
||||||
"The signs are unclear.",
|
"The signs are unclear.",
|
||||||
];
|
];
|
||||||
|
|
||||||
private static readonly string[] AnswersNo = [
|
private static readonly string[] AnswersNo = [
|
||||||
"No, absolutely not.",
|
"No, absolutely not.",
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user