Made reserved tags check permissions, fixed ulong to/from string differences breaking tags

This commit is contained in:
Deukhoofd 2018-08-25 17:28:09 +02:00
parent d2d6b7824f
commit b849c1b734
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 44 additions and 28 deletions

View File

@ -8,7 +8,7 @@ namespace DeukBot4.Database
public class ReminderHandler public class ReminderHandler
{ {
public static ReminderHandler Main = new ReminderHandler(); public static ReminderHandler Main = new ReminderHandler();
public static ConnectionMultiplexer Redis = ConnectionMultiplexer.Connect("127.0.0.1"); public static ConnectionMultiplexer Redis = ConnectionMultiplexer.Connect("localhost:6379");
public async Task AddReminder(TimeSpan time, string message, ulong channel, ulong author, ulong recipient) public async Task AddReminder(TimeSpan time, string message, ulong channel, ulong author, ulong recipient)
@ -21,10 +21,10 @@ namespace DeukBot4.Database
db.SortedSetAddAsync("deukbot_reminders", (RedisValue)id, expectedTime.ToBinary()); db.SortedSetAddAsync("deukbot_reminders", (RedisValue)id, expectedTime.ToBinary());
db.HashSetAsync((RedisKey) id, new[] db.HashSetAsync((RedisKey) id, new[]
{ {
new HashEntry("channel", channel.ToString()), new HashEntry("channel", channel.ToString("D")),
new HashEntry("message", message), new HashEntry("message", message),
new HashEntry("author", author.ToString()), new HashEntry("author", author.ToString("D")),
new HashEntry("recipient", recipient.ToString()), new HashEntry("recipient", recipient.ToString("D")),
}); });
} }
catch (Exception e) catch (Exception e)
@ -36,10 +36,13 @@ namespace DeukBot4.Database
public async Task CheckReminders() public async Task CheckReminders()
{ {
var checkTime = TimeSpan.FromSeconds(70); var checkTime = TimeSpan.FromSeconds(70);
try
{
var startTime = DateTime.UtcNow; var startTime = DateTime.UtcNow;
var desiredTopScore = (startTime + checkTime).ToBinary(); var desiredTopScore = (startTime + checkTime).ToBinary();
var db = Redis.GetDatabase(); var db = Redis.GetDatabase();
var reminders = db.SortedSetRangeByScoreWithScores("deukbot_reminders", stop: desiredTopScore); var reminders = db.SortedSetRangeByScoreWithScores("deukbot_reminders", start: Double.MinValue,
stop: desiredTopScore);
foreach (var sortedSetEntry in reminders) foreach (var sortedSetEntry in reminders)
{ {
var val = sortedSetEntry.Element.ToString(); var val = sortedSetEntry.Element.ToString();
@ -57,13 +60,18 @@ namespace DeukBot4.Database
else if (hashEntry.Name == "author") author = ulong.Parse(hashEntry.Value); else if (hashEntry.Name == "author") author = ulong.Parse(hashEntry.Value);
else if (hashEntry.Name == "recipient") recipient = ulong.Parse(hashEntry.Value); else if (hashEntry.Name == "recipient") recipient = ulong.Parse(hashEntry.Value);
} }
var diff = time - DateTime.UtcNow; var diff = time - DateTime.UtcNow;
FireReminderAtTime((int) diff.TotalSeconds, channel, message, author, recipient); FireReminderAtTime((int) diff.TotalSeconds, channel, message, author, recipient);
db.KeyDelete(val); db.KeyDelete(val);
} }
db.SortedSetRemoveRangeByScoreAsync("deukbot_reminders", Double.MinValue, desiredTopScore); db.SortedSetRemoveRangeByScoreAsync("deukbot_reminders", Double.MinValue, desiredTopScore);
}
catch (Exception e)
{
Logger.Main.LogError(e);
}
await Task.Delay(checkTime); await Task.Delay(checkTime);
await CheckReminders(); await CheckReminders();
} }

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DeukBot4.Database; using DeukBot4.Database;
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure; using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
using DeukBot4.MessageHandlers.Permissions;
using DeukBot4.Utilities; using DeukBot4.Utilities;
using Discord; using Discord;
using Npgsql; using Npgsql;
@ -199,6 +200,9 @@ namespace DeukBot4.MessageHandlers.CommandHandler
var first = split[0].ToLowerInvariant(); var first = split[0].ToLowerInvariant();
if (first == "create") if (first == "create")
{ {
var perms = await PermissionValidator.GetUserPermissionLevel(cmd.OriginalMessage);
if (perms < PermissionLevel.Helper)
return;
var keyword = split[1]; var keyword = split[1];
var rest = split.Skip(2).ToArray().Join(" "); var rest = split.Skip(2).ToArray().Join(" ");
var result = serverTags.AddTag(keyword, rest); var result = serverTags.AddTag(keyword, rest);
@ -214,6 +218,10 @@ namespace DeukBot4.MessageHandlers.CommandHandler
} }
if (first == "remove" || first == "delete") if (first == "remove" || first == "delete")
{ {
var perms = await PermissionValidator.GetUserPermissionLevel(cmd.OriginalMessage);
if (perms < PermissionLevel.Helper)
return;
var keyword = split[1]; var keyword = split[1];
var result = serverTags.RemoveTag(keyword); var result = serverTags.RemoveTag(keyword);
if (!result.success) if (!result.success)