geekbot/Geekbot.net/Commands/Games/Roll/Roll.cs

98 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
2017-04-12 22:43:52 +02:00
using Discord.Commands;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
2020-05-30 17:02:17 +02:00
using Geekbot.net.Lib.KvInMemoryStore;
using Geekbot.net.Lib.Localization;
2019-05-11 01:18:22 +02:00
using Geekbot.net.Lib.RandomNumberGenerator;
2017-04-12 22:43:52 +02:00
2020-06-14 17:27:07 +02:00
namespace Geekbot.net.Commands.Games.Roll
2017-04-12 22:43:52 +02:00
{
public class Roll : ModuleBase
{
2017-12-29 01:53:50 +01:00
private readonly IErrorHandler _errorHandler;
2020-05-30 17:02:17 +02:00
private readonly IKvInMemoryStore _kvInMemoryStore;
private readonly ITranslationHandler _translation;
private readonly DatabaseContext _database;
2019-05-11 01:18:22 +02:00
private readonly IRandomNumberGenerator _randomNumberGenerator;
2017-09-15 22:56:03 +02:00
2020-05-30 17:02:17 +02:00
public Roll(IKvInMemoryStore kvInMemoryStore,IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
{
2020-05-30 17:02:17 +02:00
_kvInMemoryStore = kvInMemoryStore;
_translation = translation;
_database = database;
2019-05-11 01:18:22 +02:00
_randomNumberGenerator = randomNumberGenerator;
_errorHandler = errorHandler;
}
2017-09-15 22:56:03 +02:00
[Command("roll", RunMode = RunMode.Async)]
2017-10-12 16:34:10 +02:00
[Summary("Guess which number the bot will roll (1-100")]
public async Task RollCommand([Remainder] [Summary("guess")] string stuff = null)
2017-04-12 22:43:52 +02:00
{
try
{
2019-05-11 01:18:22 +02:00
var number = _randomNumberGenerator.Next(1, 100);
2020-05-30 17:02:17 +02:00
int.TryParse(stuff, out var guess);
var transContext = await _translation.GetGuildContext(Context);
if (guess <= 100 && guess > 0)
{
2020-05-30 17:02:17 +02:00
var kvKey = $"{Context.Guild.Id}:{Context.User.Id}:RollsPrevious";
2020-06-14 17:27:07 +02:00
var prevRoll = _kvInMemoryStore.Get<RollTimeout>(kvKey);
if (prevRoll?.LastGuess == guess && prevRoll?.GuessedOn.AddDays(1) > DateTime.Now)
{
2020-06-14 17:27:07 +02:00
await ReplyAsync(transContext.GetString(
"NoPrevGuess",
Context.Message.Author.Mention,
transContext.FormatDateTimeAsRemaining(prevRoll.GuessedOn.AddDays(1))));
return;
}
2017-12-29 01:53:50 +01:00
2020-06-14 17:27:07 +02:00
_kvInMemoryStore.Set(kvKey, new RollTimeout { LastGuess = guess, GuessedOn = DateTime.Now });
2018-07-28 16:31:18 +02:00
await ReplyAsync(transContext.GetString("Rolled", Context.Message.Author.Mention, number, guess));
if (guess == number)
{
await ReplyAsync(transContext.GetString("Gratz", Context.Message.Author));
var user = await GetUser(Context.User.Id);
user.Rolls += 1;
_database.Rolls.Update(user);
await _database.SaveChangesAsync();
}
}
else
{
await ReplyAsync(transContext.GetString("RolledNoGuess", Context.Message.Author.Mention, number));
}
}
catch (Exception e)
{
2018-07-28 16:31:18 +02:00
await _errorHandler.HandleCommandException(e, Context);
}
2017-04-12 22:43:52 +02:00
}
private async Task<RollsModel> GetUser(ulong userId)
{
var user = _database.Rolls.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId);
return user;
}
private async Task<RollsModel> CreateNewRow(ulong userId)
{
var user = new RollsModel()
{
GuildId = Context.Guild.Id.AsLong(),
UserId = userId.AsLong(),
Rolls = 0
};
var newUser = _database.Rolls.Add(user).Entity;
await _database.SaveChangesAsync();
return newUser;
}
2017-04-12 22:43:52 +02:00
}
}