Adding username history to user repo

This commit is contained in:
Runebaas 2017-10-26 23:31:51 +02:00
parent 9efac29956
commit 14dfbca389
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
2 changed files with 75 additions and 41 deletions

View file

@ -107,7 +107,7 @@ namespace Geekbot.net.Commands
await ReplyAsync("Woah, i don't think you want to add that role to self service as it contains some dangerous permissions"); await ReplyAsync("Woah, i don't think you want to add that role to self service as it contains some dangerous permissions");
return; return;
} }
_redis.HashSet($"{Context.Guild.Id}:RoleWhitelist", new HashEntry[] { new HashEntry(roleName, role.Id.ToString()) }); _redis.HashSet($"{Context.Guild.Id}:RoleWhitelist", new HashEntry[] { new HashEntry(roleName.ToLower(), role.Id.ToString()) });
await ReplyAsync($"Added {role.Name} to the whitelist"); await ReplyAsync($"Added {role.Name} to the whitelist");
} }
catch (Exception e) catch (Exception e)

View file

@ -1,9 +1,12 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AngleSharp.Dom.Css;
using Discord.WebSocket; using Discord.WebSocket;
using Serilog; using Serilog;
using StackExchange.Redis; using StackExchange.Redis;
using Utf8Json;
namespace Geekbot.net.Lib namespace Geekbot.net.Lib
{ {
@ -20,27 +23,48 @@ namespace Geekbot.net.Lib
public Task<bool> Update(SocketUser user) public Task<bool> Update(SocketUser user)
{ {
try try
{
var savedUser = Get(user.Id);
savedUser.Id = user.Id;
savedUser.Username = user.Username;
savedUser.Discriminator = user.Discriminator;
savedUser.AvatarUrl = user.GetAvatarUrl() ?? "0";
savedUser.IsBot = user.IsBot;
savedUser.Joined = user.CreatedAt;
if(savedUser.UsedNames == null) savedUser.UsedNames = new List<string>();
if (!savedUser.UsedNames.Contains(user.Username))
{
savedUser.UsedNames.Add(user.Username);
}
Store(savedUser);
_logger.Information($"[UserRepository] Updated User {user.Username}#{user.Discriminator} ({user.Id})");
return Task.FromResult(true);
}
catch (Exception e)
{
_logger.Warning(e, $"[UserRepository] Failed to update {user.Username}#{user.Discriminator} ({user.Id})");
return Task.FromResult(false);
}
}
private void Store(UserRepositoryUser user)
{ {
_redis.HashSetAsync($"Users:{user.Id.ToString()}", new HashEntry[] _redis.HashSetAsync($"Users:{user.Id.ToString()}", new HashEntry[]
{ {
new HashEntry("Id", user.Id.ToString()), new HashEntry("Id", user.Id.ToString()),
new HashEntry("Username", user.Username), new HashEntry("Username", user.Username),
new HashEntry("Discriminator", user.Discriminator), new HashEntry("Discriminator", user.Discriminator),
new HashEntry("AvatarUrl", user.GetAvatarUrl() ?? "0"), new HashEntry("AvatarUrl", user.AvatarUrl),
new HashEntry("IsBot", user.IsBot), new HashEntry("IsBot", user.IsBot),
new HashEntry("Joined", user.CreatedAt.ToString()), new HashEntry("Joined", user.Joined.ToString()),
new HashEntry("UsedNames", JsonSerializer.Serialize(user.UsedNames)),
}); });
_logger.Information($"[UserRepository] Updated User {user.Id}");
return Task.FromResult(true);
}
catch (Exception e)
{
_logger.Warning(e, $"[UserRepository] Failed to update {user.Id}");
return Task.FromResult(false);
}
} }
public UserRepositoryUser Get(ulong userId) public UserRepositoryUser Get(ulong userId)
{
try
{ {
var user = _redis.HashGetAll($"Users:{userId.ToString()}"); var user = _redis.HashGetAll($"Users:{userId.ToString()}");
for (int i = 1; i < 11; i++) for (int i = 1; i < 11; i++)
@ -70,12 +94,21 @@ namespace Geekbot.net.Lib
dto.IsBot = a.Value == 1; dto.IsBot = a.Value == 1;
break; break;
case "Joined": case "Joined":
dto.Joined = DateTimeOffset.Parse(a.Value); dto.Joined = DateTimeOffset.Parse(a.Value.ToString());
break;
case "UsedNames":
dto.UsedNames = JsonSerializer.Deserialize<List<string>>(a.Value.ToString()) ?? new List<string>();
break; break;
} }
} }
return dto; return dto;
} }
catch (Exception e)
{
_logger.Warning(e, $"[UserRepository] Failed to get {userId} from repository");
return new UserRepositoryUser();
}
}
public string getUserSetting(ulong userId, string setting) public string getUserSetting(ulong userId, string setting)
{ {
@ -100,6 +133,7 @@ namespace Geekbot.net.Lib
public string AvatarUrl { get; set; } public string AvatarUrl { get; set; }
public bool IsBot { get; set; } public bool IsBot { get; set; }
public DateTimeOffset Joined { get; set; } public DateTimeOffset Joined { get; set; }
public List<string> UsedNames { get; set; }
} }
public interface IUserRepository public interface IUserRepository