Check the users previous roll in the /roll command
This commit is contained in:
parent
a1893c7414
commit
588c93b87d
2 changed files with 37 additions and 4 deletions
|
@ -1,16 +1,26 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Geekbot.Core.Database;
|
||||||
using Geekbot.Core.Interactions;
|
using Geekbot.Core.Interactions;
|
||||||
using Geekbot.Core.Interactions.ApplicationCommand;
|
using Geekbot.Core.Interactions.ApplicationCommand;
|
||||||
using Geekbot.Core.Interactions.Request;
|
using Geekbot.Core.Interactions.Request;
|
||||||
using Geekbot.Core.Interactions.Resolved;
|
|
||||||
using Geekbot.Core.Interactions.Response;
|
using Geekbot.Core.Interactions.Response;
|
||||||
|
using Geekbot.Core.KvInMemoryStore;
|
||||||
using Geekbot.Core.RandomNumberGenerator;
|
using Geekbot.Core.RandomNumberGenerator;
|
||||||
|
|
||||||
namespace Geekbot.Web.Commands
|
namespace Geekbot.Web.Commands
|
||||||
{
|
{
|
||||||
public class Roll : InteractionBase
|
public class Roll : InteractionBase
|
||||||
{
|
{
|
||||||
|
private readonly IKvInMemoryStore _kvInMemoryStore;
|
||||||
|
private readonly IRandomNumberGenerator _randomNumberGenerator;
|
||||||
|
|
||||||
|
public Roll(IKvInMemoryStore kvInMemoryStore, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
|
||||||
|
{
|
||||||
|
_kvInMemoryStore = kvInMemoryStore;
|
||||||
|
_randomNumberGenerator = randomNumberGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
public override Command GetCommandInfo()
|
public override Command GetCommandInfo()
|
||||||
{
|
{
|
||||||
return new Command()
|
return new Command()
|
||||||
|
@ -36,13 +46,27 @@ namespace Geekbot.Web.Commands
|
||||||
var guessOption = interaction.Data.Options.Find(o => o.Name == "guess");
|
var guessOption = interaction.Data.Options.Find(o => o.Name == "guess");
|
||||||
var guess = guessOption.Value.GetInt32();
|
var guess = guessOption.Value.GetInt32();
|
||||||
|
|
||||||
var number = new RandomNumberGenerator().Next(1, 100);
|
var number = _randomNumberGenerator.Next(1, 100);
|
||||||
|
|
||||||
var replyContent = "";
|
var replyContent = "";
|
||||||
|
|
||||||
if (guess <= 100 && guess > 0)
|
if (guess <= 100 && guess > 0)
|
||||||
{
|
{
|
||||||
replyContent = $"{interaction?.Member?.User?.Mention}, you rolled {number}, your guess was {guess}";
|
var kvKey = $"{interaction.GuildId}:{interaction.Member.User.Id}:RollsPrevious";
|
||||||
|
var prevRoll = _kvInMemoryStore.Get<RollTimeout>(kvKey);
|
||||||
|
|
||||||
|
if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now)
|
||||||
|
{
|
||||||
|
replyContent = string.Format(
|
||||||
|
":red_circle: {0}, you can't guess the same number again, guess another number or wait {1}",
|
||||||
|
interaction.Member.Nick ?? interaction.Member.User.Username,
|
||||||
|
prevRoll.GuessedOn.AddDays(1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_kvInMemoryStore.Set(kvKey, new RollTimeout {LastGuess = guess, GuessedOn = DateTime.Now});
|
||||||
|
replyContent = $"{interaction.Member?.User?.Mention}, you rolled {number}, your guess was {guess}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
9
src/Web/Commands/RollTimeout.cs
Normal file
9
src/Web/Commands/RollTimeout.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Geekbot.Web.Commands;
|
||||||
|
|
||||||
|
public record RollTimeout
|
||||||
|
{
|
||||||
|
public int LastGuess { get; set; }
|
||||||
|
public DateTime GuessedOn { get; set; }
|
||||||
|
}
|
Loading…
Reference in a new issue