2017-09-14 22:11:19 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-04-12 22:43:52 +02:00
|
|
|
|
using Discord.Commands;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
using Geekbot.net.Lib.AlmostRedis;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
|
|
|
|
using Geekbot.net.Lib.Localization;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
using StackExchange.Redis;
|
2017-04-12 22:43:52 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Commands.Games
|
2017-04-12 22:43:52 +02:00
|
|
|
|
{
|
|
|
|
|
public class Roll : ModuleBase
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
private readonly IAlmostRedis _redis;
|
2017-11-16 17:19:43 +01:00
|
|
|
|
private readonly ITranslationHandler _translation;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2018-05-26 02:33:45 +02:00
|
|
|
|
public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation)
|
2017-04-22 23:41:15 +02:00
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
_redis = redis;
|
|
|
|
|
_translation = translation;
|
|
|
|
|
_errorHandler = errorHandler;
|
2017-04-22 23:41:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
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 = "noGuess")
|
2017-04-12 22:43:52 +02:00
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
try
|
2017-04-17 23:58:43 +02:00
|
|
|
|
{
|
2018-02-14 23:01:28 +01:00
|
|
|
|
var number = new Random().Next(1, 100);
|
2017-11-16 17:19:43 +01:00
|
|
|
|
var guess = 1000;
|
|
|
|
|
int.TryParse(stuff, out guess);
|
2018-06-13 22:18:57 +02:00
|
|
|
|
var transDict = await _translation.GetDict(Context);
|
2017-11-16 17:19:43 +01:00
|
|
|
|
if (guess <= 100 && guess > 0)
|
2017-04-17 23:58:43 +02:00
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious2", Context.Message.Author.Id).ToString()?.Split('|');
|
|
|
|
|
if (prevRoll?.Length == 2)
|
2017-12-29 01:19:20 +01:00
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
if (prevRoll[0] == guess.ToString() && DateTime.Parse(prevRoll[1]) > DateTime.Now.AddDays(-1))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:19:20 +01:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-07-28 16:31:18 +02:00
|
|
|
|
_redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious2", new[] {new HashEntry(Context.Message.Author.Id, $"{guess}|{DateTime.Now}")});
|
|
|
|
|
|
2017-11-16 17:19:43 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess));
|
|
|
|
|
if (guess == number)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(string.Format(transDict["Gratz"], Context.Message.Author));
|
2018-05-26 02:33:45 +02:00
|
|
|
|
_redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
|
2017-11-16 17:19:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(string.Format(transDict["RolledNoGuess"], Context.Message.Author.Mention, number));
|
2017-04-17 23:58:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-16 17:19:43 +01:00
|
|
|
|
catch (Exception e)
|
2017-04-17 23:58:43 +02:00
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
await _errorHandler.HandleCommandException(e, Context);
|
2017-04-17 23:58:43 +02:00
|
|
|
|
}
|
2017-04-12 22:43:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|