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;
|
|
|
|
|
using Serilog;
|
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
|
|
|
|
{
|
|
|
|
|
public class Counters : ModuleBase
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
private readonly IDatabase _redis;
|
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-10-26 00:55:04 +02:00
|
|
|
|
public Counters(IDatabase redis, IErrorHandler errorHandler)
|
2017-04-18 11:00:38 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_redis = redis;
|
|
|
|
|
_errorHandler = errorHandler;
|
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-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)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync($"Sorry {Context.User.Username}, but you can't lower your own karma");
|
|
|
|
|
}
|
|
|
|
|
else if (TimeoutFinished(lastKarma))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Sorry {Context.User.Username}, but you have to wait {GetTimeLeft(lastKarma)} before you can give karma again...");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
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-09-27 22:48:09 +02:00
|
|
|
|
new HashEntry[] {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));
|
|
|
|
|
eb.Title = "Karma Increased";
|
|
|
|
|
eb.AddInlineField("By", Context.User.Username);
|
|
|
|
|
eb.AddInlineField("amount", "+1");
|
|
|
|
|
eb.AddInlineField("Current Karma", newKarma);
|
|
|
|
|
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-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)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync($"Sorry {Context.User.Username}, but you can't lower your own karma");
|
|
|
|
|
}
|
|
|
|
|
else if (TimeoutFinished(lastKarma))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Sorry {Context.User.Username}, but you have to wait {GetTimeLeft(lastKarma)} before you can take karma again...");
|
|
|
|
|
}
|
|
|
|
|
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-09-27 22:48:09 +02:00
|
|
|
|
new HashEntry[] {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));
|
|
|
|
|
eb.Title = "Karma Decreased";
|
|
|
|
|
eb.AddInlineField("By", Context.User.Username);
|
|
|
|
|
eb.AddInlineField("amount", "-1");
|
|
|
|
|
eb.AddInlineField("Current Karma", newKarma);
|
|
|
|
|
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
|
|
|
|
{
|
2017-09-27 22:48:09 +02:00
|
|
|
|
if(string.IsNullOrEmpty(dateTimeOffsetString)) return DateTimeOffset.Now.Subtract(new TimeSpan(7, 18, 0, 0));
|
|
|
|
|
return DateTimeOffset.Parse(dateTimeOffsetString);
|
2017-04-19 19:23:22 +02: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-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
|
|
|
|
}
|
|
|
|
|
}
|