geekbot/Geekbot.net/Commands/Counters.cs

125 lines
5 KiB
C#
Raw Normal View History

2017-04-17 16:58:48 +02:00
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Serilog;
using StackExchange.Redis;
2017-04-17 16:58:48 +02:00
namespace Geekbot.net.Commands
2017-04-17 16:58:48 +02:00
{
public class Counters : ModuleBase
{
private readonly IDatabase _redis;
private readonly IErrorHandler _errorHandler;
2017-09-15 22:56:03 +02:00
public Counters(IDatabase redis, IErrorHandler errorHandler)
{
_redis = redis;
_errorHandler = errorHandler;
}
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
{
try
2017-04-17 16:58:48 +02:00
{
var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString());
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
{
var newKarma = _redis.HashIncrement($"{Context.Guild.Id}:Karma", user.Id.ToString());
_redis.HashSet($"{Context.Guild.Id}:KarmaTimeout",
new HashEntry[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString("u"))});
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(user.GetAvatarUrl())
.WithName(user.Username));
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)
{
_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
{
try
2017-04-19 19:23:22 +02:00
{
var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString());
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
{
var newKarma = _redis.HashDecrement($"{Context.Guild.Id}:Karma", user.Id.ToString());
_redis.HashSet($"{Context.Guild.Id}:KarmaTimeout",
new HashEntry[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString())});
2017-09-15 22:56:03 +02:00
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(user.GetAvatarUrl())
.WithName(user.Username));
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)
{
_errorHandler.HandleCommandException(e, Context);
2017-04-17 16:58:48 +02:00
}
}
2017-04-19 19:23:22 +02:00
private DateTimeOffset ConvertToDateTimeOffset(string dateTimeOffsetString)
2017-04-19 19:23:22 +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
}
private bool TimeoutFinished(DateTimeOffset lastKarma)
2017-04-19 19:23:22 +02:00
{
return lastKarma.AddMinutes(3) > DateTimeOffset.Now;
2017-04-19 19:23:22 +02:00
}
private string GetTimeLeft(DateTimeOffset lastKarma)
2017-04-19 19:23:22 +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
}
}