2017-04-17 16:58:48 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-09-27 22:48:09 +02:00
|
|
|
|
using Geekbot.net.Lib;
|
2017-10-02 21:57:48 +02:00
|
|
|
|
using StackExchange.Redis;
|
2017-04-17 16:58:48 +02:00
|
|
|
|
|
2017-10-02 21:57:48 +02:00
|
|
|
|
namespace Geekbot.net.Commands
|
2017-04-17 16:58:48 +02:00
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
public class Karma : ModuleBase
|
2017-04-17 16:58:48 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IDatabase _redis;
|
2017-11-16 17:19:43 +01:00
|
|
|
|
private readonly ITranslationHandler _translation;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-12-29 01:53:50 +01:00
|
|
|
|
public Karma(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation)
|
2017-04-18 11:00:38 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_redis = redis;
|
|
|
|
|
_errorHandler = errorHandler;
|
2017-11-16 17:19:43 +01:00
|
|
|
|
_translation = translation;
|
2017-04-18 11:00:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("good", RunMode = RunMode.Async)]
|
2017-10-12 16:34:10 +02:00
|
|
|
|
[Remarks(CommandCategories.Karma)]
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Summary("Increase Someones Karma")]
|
|
|
|
|
public async Task Good([Summary("@someone")] IUser user)
|
2017-04-17 16:58:48 +02:00
|
|
|
|
{
|
2017-09-27 22:48:09 +02:00
|
|
|
|
try
|
2017-04-17 16:58:48 +02:00
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
var transDict = _translation.GetDict(Context);
|
2017-10-26 00:55:04 +02:00
|
|
|
|
var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString());
|
2017-09-27 22:48:09 +02:00
|
|
|
|
var lastKarma = ConvertToDateTimeOffset(lastKarmaFromRedis.ToString());
|
|
|
|
|
if (user.Id == Context.User.Id)
|
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
|
|
|
|
else if (TimeoutFinished(lastKarma))
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username,
|
|
|
|
|
GetTimeLeft(lastKarma)));
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
else
|
2017-09-27 22:48:09 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
var newKarma = _redis.HashIncrement($"{Context.Guild.Id}:Karma", user.Id.ToString());
|
|
|
|
|
_redis.HashSet($"{Context.Guild.Id}:KarmaTimeout",
|
2017-12-29 01:53:50 +01:00
|
|
|
|
new[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString("u"))});
|
2017-07-12 09:52:55 +02:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
var eb = new EmbedBuilder();
|
|
|
|
|
eb.WithAuthor(new EmbedAuthorBuilder()
|
|
|
|
|
.WithIconUrl(user.GetAvatarUrl())
|
|
|
|
|
.WithName(user.Username));
|
2017-07-12 09:52:55 +02:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
eb.WithColor(new Color(138, 219, 146));
|
2017-11-16 17:19:43 +01:00
|
|
|
|
eb.Title = transDict["Increased"];
|
|
|
|
|
eb.AddInlineField(transDict["By"], Context.User.Username);
|
|
|
|
|
eb.AddInlineField(transDict["Amount"], "+1");
|
|
|
|
|
eb.AddInlineField(transDict["Current"], newKarma);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
2017-04-17 16:58:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("bad", RunMode = RunMode.Async)]
|
2017-10-12 16:34:10 +02:00
|
|
|
|
[Remarks(CommandCategories.Karma)]
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Summary("Decrease Someones Karma")]
|
|
|
|
|
public async Task Bad([Summary("@someone")] IUser user)
|
2017-04-17 16:58:48 +02:00
|
|
|
|
{
|
2017-09-27 22:48:09 +02:00
|
|
|
|
try
|
2017-04-19 19:23:22 +02:00
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
var transDict = _translation.GetDict(Context);
|
2017-10-26 00:55:04 +02:00
|
|
|
|
var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString());
|
2017-09-27 22:48:09 +02:00
|
|
|
|
var lastKarma = ConvertToDateTimeOffset(lastKarmaFromRedis.ToString());
|
|
|
|
|
if (user.Id == Context.User.Id)
|
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
|
|
|
|
else if (TimeoutFinished(lastKarma))
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username,
|
|
|
|
|
GetTimeLeft(lastKarma)));
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
var newKarma = _redis.HashDecrement($"{Context.Guild.Id}:Karma", user.Id.ToString());
|
|
|
|
|
_redis.HashSet($"{Context.Guild.Id}:KarmaTimeout",
|
2017-12-29 01:53:50 +01:00
|
|
|
|
new[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString())});
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
var eb = new EmbedBuilder();
|
|
|
|
|
eb.WithAuthor(new EmbedAuthorBuilder()
|
|
|
|
|
.WithIconUrl(user.GetAvatarUrl())
|
|
|
|
|
.WithName(user.Username));
|
2017-07-12 09:52:55 +02:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
eb.WithColor(new Color(138, 219, 146));
|
2017-11-16 17:19:43 +01:00
|
|
|
|
eb.Title = transDict["Decreased"];
|
|
|
|
|
eb.AddInlineField(transDict["By"], Context.User.Username);
|
|
|
|
|
eb.AddInlineField(transDict["Amount"], "-1");
|
|
|
|
|
eb.AddInlineField(transDict["Current"], newKarma);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
2017-04-17 16:58:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-19 19:23:22 +02:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
private DateTimeOffset ConvertToDateTimeOffset(string dateTimeOffsetString)
|
2017-04-19 19:23:22 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
return string.IsNullOrEmpty(dateTimeOffsetString) ? DateTimeOffset.Now.Subtract(new TimeSpan(7, 18, 0, 0)) : DateTimeOffset.Parse(dateTimeOffsetString);
|
2017-04-19 19:23:22 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
private bool TimeoutFinished(DateTimeOffset lastKarma)
|
2017-04-19 19:23:22 +02:00
|
|
|
|
{
|
2017-09-27 22:48:09 +02:00
|
|
|
|
return lastKarma.AddMinutes(3) > DateTimeOffset.Now;
|
2017-04-19 19:23:22 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-09-27 22:48:09 +02:00
|
|
|
|
private string GetTimeLeft(DateTimeOffset lastKarma)
|
2017-04-19 19:23:22 +02:00
|
|
|
|
{
|
2017-09-27 22:48:09 +02:00
|
|
|
|
var dt = lastKarma.AddMinutes(3).Subtract(DateTimeOffset.Now);
|
2017-04-19 19:23:22 +02:00
|
|
|
|
return $"{dt.Minutes} Minutes and {dt.Seconds} Seconds";
|
|
|
|
|
}
|
2017-04-17 16:58:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|