karma commands now have a timeout

This commit is contained in:
dboerlage 2017-04-19 19:23:22 +02:00
parent 6aaf487e94
commit 4db152f304
No known key found for this signature in database
GPG key ID: BDA07B7D3FCF147F
4 changed files with 61 additions and 8 deletions

View file

@ -17,15 +17,23 @@ namespace Geekbot.net.Modules
[Command("good"), Summary("Increase Someones Karma")]
public async Task Good([Summary("The someone")] IUser user)
{
var lastKarma = GetLastKarma();
Console.WriteLine(lastKarma.ToString());
if (user.Id == Context.User.Id)
{
await ReplyAsync($"Sorry {Context.User.Username}, but you can't give yourself karma");
}
else if (lastKarma > GetUnixTimestamp())
{
await ReplyAsync($"Sorry {Context.User.Username}, but you have to wait {GetTimeLeft(lastKarma)} before you can give karma again...");
}
else
{
var key = Context.Guild.Id + "-" + user.Id + "-karma";
var badJokes = (int)redis.Client.StringGet(key);
redis.Client.StringSet(key, (badJokes + 1).ToString());
var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout";
redis.Client.StringSet(lastKey, GetNewLastKarma());
await ReplyAsync($"{Context.User.Username} gave {user.Mention} karma");
}
}
@ -33,17 +41,54 @@ namespace Geekbot.net.Modules
[Command("bad"), Summary("Decrease Someones Karma")]
public async Task Bad([Summary("The someone")] IUser user)
{
var lastKarma = GetLastKarma();
if (user.Id == Context.User.Id)
{
await ReplyAsync($"Sorry {Context.User.Username}, but you can't lower your own karma");
}
else if (lastKarma > GetUnixTimestamp())
{
await ReplyAsync($"Sorry {Context.User.Username}, but you have to wait {GetTimeLeft(lastKarma)} before you can take karma again...");
}
else
{
var key = Context.Guild.Id + "-" + user.Id + "-karma";
var badJokes = (int)redis.Client.StringGet(key);
redis.Client.StringSet(key, (badJokes - 1).ToString());
var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout";
redis.Client.StringSet(lastKey, GetNewLastKarma());
await ReplyAsync($"{Context.User.Username} lowered {user.Mention}'s karma");
}
}
private int GetLastKarma()
{
var lastKey = Context.Guild.Id + "-" + Context.User.Id + "-karma-timeout";
var redisReturn = redis.Client.StringGet(lastKey);
if (!int.TryParse(redisReturn.ToString(), out var i))
{
i = GetUnixTimestamp();
}
return i;
}
private int GetNewLastKarma()
{
var timeout = TimeSpan.FromMinutes(3);
return (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).Add(timeout)).TotalSeconds;
}
private int GetUnixTimestamp()
{
return (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
private string GetTimeLeft(int time)
{
DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds( time ).ToLocalTime();
var dt = dtDateTime.Subtract(DateTime.Now);
return $"{dt.Minutes} Minutes and {dt.Seconds} Seconds";
}
}
}

View file

@ -1,15 +1,15 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
namespace Geekbot.net.Modules
{
public class Ping : ModuleBase
{
[Command("ping"), Summary("Pong.")]
public async Task Say()
[Command("👀"), Summary("Look at the bot.")]
public async Task Eyes()
{
await Task.Delay(5000);
await ReplyAsync("Pong");
await ReplyAsync("S... Stop looking at me... baka!");
}
}
}

View file

@ -32,8 +32,8 @@ namespace Geekbot.net.Modules
.WithName(userInfo.Username));
eb.AddField("Discordian Since", $"{userInfo.CreatedAt.Day}/{userInfo.CreatedAt.Month}/{userInfo.CreatedAt.Year} ({age} days)");
eb.AddField("Level", level);
eb.AddField("Messages Sent", messages);
eb.AddInlineField("Level", level)
.AddInlineField("Messages Sent", messages);
var karma = redis.Client.StringGet(key + "-karma");
if (!karma.IsNullOrEmpty)

View file

@ -60,8 +60,16 @@ namespace Geekbot.net
await InstallCommands();
Console.WriteLine("Connecting to Discord...");
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
try
{
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
}
catch (AggregateException)
{
Console.WriteLine("Could not connect to discord...");
Environment.Exit(1);
}
Console.WriteLine("Done and ready for use...\n");
await Task.Delay(-1);