2017-04-17 16:58:48 +02:00
|
|
|
|
using System;
|
2018-05-10 02:33:10 +02:00
|
|
|
|
using System.Linq;
|
2017-04-17 16:58:48 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2018-05-10 02:33:10 +02:00
|
|
|
|
using Geekbot.net.Database;
|
|
|
|
|
using Geekbot.net.Database.Models;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2018-05-10 02:33:10 +02:00
|
|
|
|
using Geekbot.net.Lib.Extensions;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Localization;
|
2017-04-17 16:58:48 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Commands.User
|
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;
|
2018-05-10 02:33:10 +02:00
|
|
|
|
private readonly DatabaseContext _database;
|
2017-11-16 17:19:43 +01:00
|
|
|
|
private readonly ITranslationHandler _translation;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2018-05-10 02:33:10 +02:00
|
|
|
|
public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation)
|
2017-04-18 11:00:38 +02:00
|
|
|
|
{
|
2018-05-10 02:33:10 +02:00
|
|
|
|
_database = database;
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_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)]
|
|
|
|
|
[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
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
var transDict = await _translation.GetDict(Context);
|
|
|
|
|
var actor = await GetUser(Context.User.Id);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
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
|
|
|
|
}
|
2018-05-10 02:33:10 +02:00
|
|
|
|
else if (TimeoutFinished(actor.TimeOut))
|
2017-09-27 22:48:09 +02:00
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username,
|
2018-05-10 02:33:10 +02:00
|
|
|
|
GetTimeLeft(actor.TimeOut)));
|
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
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
var target = await GetUser(user.Id);
|
2018-05-10 02:33:10 +02:00
|
|
|
|
target.Karma = target.Karma + 1;
|
|
|
|
|
SetUser(target);
|
|
|
|
|
|
|
|
|
|
actor.TimeOut = DateTimeOffset.Now;
|
|
|
|
|
SetUser(actor);
|
|
|
|
|
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
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");
|
2018-05-10 02:33:10 +02:00
|
|
|
|
eb.AddInlineField(transDict["Current"], target.Karma);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
await _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)]
|
|
|
|
|
[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
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
var transDict = await _translation.GetDict(Context);
|
|
|
|
|
var actor = await GetUser(Context.User.Id);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
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
|
|
|
|
}
|
2018-05-10 02:33:10 +02:00
|
|
|
|
else if (TimeoutFinished(actor.TimeOut))
|
2017-09-27 22:48:09 +02:00
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username,
|
2018-05-10 02:33:10 +02:00
|
|
|
|
GetTimeLeft(actor.TimeOut)));
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
var target = await GetUser(user.Id);
|
2018-05-10 02:33:10 +02:00
|
|
|
|
target.Karma = target.Karma - 1;
|
|
|
|
|
SetUser(target);
|
|
|
|
|
|
|
|
|
|
actor.TimeOut = DateTimeOffset.Now;
|
|
|
|
|
SetUser(actor);
|
|
|
|
|
|
2018-05-14 18:57:07 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
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");
|
2018-05-10 02:33:10 +02:00
|
|
|
|
eb.AddInlineField(transDict["Current"], target.Karma);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-07-28 16:31:18 +02:00
|
|
|
|
await _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 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";
|
|
|
|
|
}
|
2018-05-10 02:33:10 +02:00
|
|
|
|
|
2018-06-13 22:18:57 +02:00
|
|
|
|
private async Task<KarmaModel> GetUser(ulong userId)
|
2018-05-10 02:33:10 +02:00
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? await CreateNewRow(userId);
|
2018-05-10 02:33:10 +02:00
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 22:18:57 +02:00
|
|
|
|
private void SetUser(KarmaModel user)
|
2018-05-10 02:33:10 +02:00
|
|
|
|
{
|
|
|
|
|
_database.Karma.Update(user);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 22:18:57 +02:00
|
|
|
|
private async Task<KarmaModel> CreateNewRow(ulong userId)
|
2018-05-10 02:33:10 +02:00
|
|
|
|
{
|
|
|
|
|
var user = new KarmaModel()
|
|
|
|
|
{
|
|
|
|
|
GuildId = Context.Guild.Id.AsLong(),
|
|
|
|
|
UserId = userId.AsLong(),
|
|
|
|
|
Karma = 0,
|
|
|
|
|
TimeOut = DateTimeOffset.MinValue
|
|
|
|
|
};
|
|
|
|
|
var newUser = _database.Karma.Add(user).Entity;
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _database.SaveChangesAsync();
|
2018-05-10 02:33:10 +02:00
|
|
|
|
return newUser;
|
|
|
|
|
}
|
2017-04-17 16:58:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|