Added system to give/take points from users per server, added new joke controller that removes points from users if they say owo, uwu, etc

This commit is contained in:
Deukhoofd 2019-01-29 14:28:47 +01:00
parent 67e793de47
commit 7b4967f1ca
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
6 changed files with 237 additions and 1 deletions

View File

@ -19,7 +19,11 @@ namespace DeukBot4.Database
DateTime bday = DateTime.MinValue;
foreach (var bdayEntry in bdayEntries)
{
if (bdayEntry.Name == "birthday") bday = DateTime.FromBinary(long.Parse(bdayEntry.Value));
if (bdayEntry.Name == "birthday")
{
bday = DateTime.FromBinary(long.Parse(bdayEntry.Value));
break;
}
}
return bday;
}

View File

@ -0,0 +1,58 @@
using System;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace DeukBot4.Database
{
public static class PointHandler
{
public static async Task<long?> GetPoints(ulong serverId, ulong userId)
{
var key = $"{serverId:D}-{userId:D}";
try
{
var db = ReminderHandler.Redis.GetDatabase();
var exists = db.HashGet("points", (RedisValue) key);
if (!exists.HasValue)
{
return 0;
}
var points = long.Parse(exists);
return points;
}
catch ( Exception e)
{
Logger.Main.LogError(e);
return null;
}
}
public static async Task<long?> ChangePoints(ulong serverId, ulong userId, long deltaPoints)
{
var key = $"{serverId:D}-{userId:D}";
try
{
var db = ReminderHandler.Redis.GetDatabase();
var exists = db.HashGet("points", (RedisValue) key);
if (!exists.HasValue)
{
db.HashSet("points", (RedisValue) key, deltaPoints);
return deltaPoints;
}
var points = long.Parse(exists);
points += deltaPoints;
if (points < -100)
points = -100;
db.HashSet("points", (RedisValue) key, points);
return points;
}
catch ( Exception e)
{
Logger.Main.LogError(e);
return null;
}
}
}
}

View File

@ -321,5 +321,28 @@ namespace DeukBot4.MessageHandlers.CommandHandler
}
request.SendSimpleEmbed("Hug!", message);
}
[Command("points", PermissionLevel.Everyone)]
[CommandParameters(ParameterMatcher.ParameterType.User)]
[BlockUsageInPm]
public async Task GetPoints(CommandRequest request)
{
if (!(request.OriginalMessage.Channel is IGuildChannel guildChannel))
return;
var user = (IGuildUser) request.OriginalMessage.Author;
if (request.Parameters.Length > 0)
{
var u = await request.Parameters[0].AsDiscordGuildUser(guildChannel.Guild);
if (u != null)
user = u;
}
var points = await PointHandler.GetPoints(guildChannel.GuildId, user.Id);
if (points.HasValue)
{
request.SendSimpleEmbed("Points", $"You currently have {points.Value} points.");
}
}
}
}

View File

@ -0,0 +1,33 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DeukBot4.Database;
using DeukBot4.MessageHandlers.CommandHandler;
using Discord;
using Discord.WebSocket;
namespace DeukBot4.MessageHandlers.JokeHandling
{
public class OwoHandler : IJokeController
{
public string Id => "owo-warn";
public string Name => "Owo Warnings";
private static readonly Regex Matcher = new Regex(@"(?:^|\s).w.(?:\s|$)", RegexOptions.IgnoreCase);
public async Task Run(SocketMessage message)
{
if (!(message.Channel is IGuildChannel serverChannel))
return;
if (!Matcher.Match(message.Content).Success)
return;
var changePoints = await PointHandler.ChangePoints(serverChannel.GuildId, message.Author.Id, -100);
var embed = EmbedFactory.GetStandardEmbedBuilder();
embed.Title = "No Owoing";
embed.Description =
$"{message.Author.Mention}, you were fined by -100 points. Your new score is {changePoints}.";
message.Channel.SendMessageAsync("", embed: embed.Build());
}
}
}

View File

@ -24,6 +24,7 @@ namespace DeukBot4.MessageHandlers
ReminderHandler.HandleReminder(message);
ImageBackupHandler.Backup(message);
JokeHandler.RunJokes(message);
PointsMessageHandler.HandleMessage(message);
#pragma warning restore 4014
}
catch (Exception e)

View File

@ -0,0 +1,117 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DeukBot4.Database;
using DeukBot4.MessageHandlers.CommandHandler;
using DeukBot4.MessageHandlers.Permissions;
using Discord;
using Discord.WebSocket;
namespace DeukBot4.MessageHandlers
{
public static class PointsMessageHandler
{
private static readonly Regex Matcher =
new Regex(@"(?'amount'-?\d+) *points? *(?'unary'to|from) *(?:<@!*(?'id'\d+)>|(?'username'\w+))", RegexOptions.IgnoreCase);
private static long MaxPoints(PermissionLevel level)
{
switch (level)
{
case PermissionLevel.Banned:
case PermissionLevel.Bot:
case PermissionLevel.Everyone:
return 0;
case PermissionLevel.Helper:
return 10;
case PermissionLevel.Moderator:
return 50;
case PermissionLevel.Admin:
return 100;
case PermissionLevel.ServerOwner:
return 1000;
case PermissionLevel.BotCreator:
return long.MaxValue;
default:
throw new ArgumentOutOfRangeException(nameof(level), level, null);
}
}
public static async Task HandleMessage(SocketMessage message)
{
if (!(message.Channel is IGuildChannel serverChannel))
return;
var permission = await PermissionValidator.GetUserPermissionLevel(message);
if (permission < PermissionLevel.Helper)
return;
var match = Matcher.Match(message.Content);
if (!match.Success)
return;
var points = long.Parse(match.Groups["amount"].Value);
var embed = EmbedFactory.GetStandardEmbedBuilder();
embed.Title = "Points";
var maxPoints = MaxPoints(permission);
if (points > maxPoints)
{
embed.Description = $"You're not allowed to give that many points. Your max is {maxPoints}";
message.Channel.SendMessageAsync("", embed: embed.Build());
return;
}
var unary = match.Groups["unary"].Value;
if (string.Equals(unary, "from"))
points = -points;
if (points == 0)
return;
IGuildUser user;
if (match.Groups["id"].Success)
{
var userId = ulong.Parse(match.Groups["id"].Value);
user = await serverChannel.Guild.GetUserAsync(userId);
}
else if (match.Groups["username"].Success)
{
var username = match.Groups["username"].Value;
var users = await serverChannel.Guild.GetUsersAsync();
user = users.FirstOrDefault(x =>
string.Equals(x.Username, username, StringComparison.InvariantCultureIgnoreCase));
}
else
{
Logger.Main.LogError("Neither id nor username set");
return;
}
if (user == null)
{
embed.Description = "Can't find that user";
message.Channel.SendMessageAsync("", embed: embed.Build());
return;
}
if (user.Id == message.Author.Id)
{
embed.Description = $"You're not allowed to give yourself points.";
message.Channel.SendMessageAsync("", embed: embed.Build());
return;
}
var newPoints = await PointHandler.ChangePoints(serverChannel.GuildId, user.Id, points);
if (!newPoints.HasValue)
{
Logger.Main.LogError("Error");
return;
}
embed.Description = points < 0
? $"{-points} points from {user.Mention}! Their total points is now {newPoints}!"
: $"{points} points to {user.Mention}! Their total points is now {newPoints}!";
message.Channel.SendMessageAsync("", embed: embed.Build());
}
}
}