geekbot/Geekbot.net/Commands/User/Karma.cs

159 lines
5.6 KiB
C#
Raw Normal View History

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;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
2018-05-10 02:33:10 +02:00
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Localization;
2017-04-17 16:58:48 +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
{
private readonly IErrorHandler _errorHandler;
2018-05-10 02:33:10 +02:00
private readonly DatabaseContext _database;
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)
{
2018-05-10 02:33:10 +02:00
_database = database;
_errorHandler = errorHandler;
_translation = translation;
}
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
{
try
2017-04-17 16:58:48 +02:00
{
var transDict = _translation.GetDict(Context);
2018-05-10 02:33:10 +02:00
var actor = GetUser(Context.User.Id);
if (user.Id == Context.User.Id)
{
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
}
2018-05-10 02:33:10 +02:00
else if (TimeoutFinished(actor.TimeOut))
{
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-12-29 01:53:50 +01:00
else
{
2018-05-10 02:33:10 +02:00
var target = GetUser(user.Id);
target.Karma = target.Karma + 1;
SetUser(target);
actor.TimeOut = DateTimeOffset.Now;
SetUser(actor);
_database.SaveChanges();
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()
.WithIconUrl(user.GetAvatarUrl())
.WithName(user.Username));
eb.WithColor(new Color(138, 219, 146));
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);
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)]
[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 transDict = _translation.GetDict(Context);
2018-05-10 02:33:10 +02:00
var actor = GetUser(Context.User.Id);
if (user.Id == Context.User.Id)
{
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
}
2018-05-10 02:33:10 +02:00
else if (TimeoutFinished(actor.TimeOut))
{
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)));
}
else
{
2018-05-10 02:33:10 +02:00
var target = GetUser(user.Id);
target.Karma = target.Karma - 1;
SetUser(target);
actor.TimeOut = DateTimeOffset.Now;
SetUser(actor);
_database.SaveChanges();
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 = 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);
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 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
}
2017-12-29 01:53:50 +01: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";
}
2018-05-10 02:33:10 +02:00
private KarmaModel GetUser(ulong userId)
{
var user = _database.Karma.FirstOrDefault(u =>u.GuildId.Equals(Context.Guild.Id.AsLong()) && u.UserId.Equals(userId.AsLong())) ?? CreateNewRow(userId);
return user;
}
private bool SetUser(KarmaModel user)
{
_database.Karma.Update(user);
return true;
}
private KarmaModel CreateNewRow(ulong userId)
{
var user = new KarmaModel()
{
GuildId = Context.Guild.Id.AsLong(),
UserId = userId.AsLong(),
Karma = 0,
TimeOut = DateTimeOffset.MinValue
};
var newUser = _database.Karma.Add(user).Entity;
_database.SaveChanges();
return newUser;
}
2017-04-17 16:58:48 +02:00
}
}