Adding username history to user repo
This commit is contained in:
parent
9efac29956
commit
14dfbca389
2 changed files with 75 additions and 41 deletions
|
@ -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");
|
||||
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");
|
||||
}
|
||||
catch (Exception e)
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AngleSharp.Dom.Css;
|
||||
using Discord.WebSocket;
|
||||
using Serilog;
|
||||
using StackExchange.Redis;
|
||||
using Utf8Json;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
|
@ -20,33 +23,54 @@ namespace Geekbot.net.Lib
|
|||
public Task<bool> Update(SocketUser user)
|
||||
{
|
||||
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[]
|
||||
{
|
||||
new HashEntry("Id", user.Id.ToString()),
|
||||
new HashEntry("Username", user.Username),
|
||||
new HashEntry("Discriminator", user.Discriminator),
|
||||
new HashEntry("AvatarUrl", user.GetAvatarUrl() ?? "0"),
|
||||
new HashEntry("AvatarUrl", user.AvatarUrl),
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = _redis.HashGetAll($"Users:{userId.ToString()}");
|
||||
for (int i = 1; i < 11; i++)
|
||||
{
|
||||
if (user.Length != 0) break;
|
||||
user = _redis.HashGetAll($"Users:{(userId + (ulong)i).ToString()}");
|
||||
user = _redis.HashGetAll($"Users:{(userId + (ulong) i).ToString()}");
|
||||
|
||||
}
|
||||
var dto = new UserRepositoryUser();
|
||||
|
@ -70,12 +94,21 @@ namespace Geekbot.net.Lib
|
|||
dto.IsBot = a.Value == 1;
|
||||
break;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
|
@ -100,6 +133,7 @@ namespace Geekbot.net.Lib
|
|||
public string AvatarUrl { get; set; }
|
||||
public bool IsBot { get; set; }
|
||||
public DateTimeOffset Joined { get; set; }
|
||||
public List<string> UsedNames { get; set; }
|
||||
}
|
||||
|
||||
public interface IUserRepository
|
||||
|
|
Loading…
Reference in a new issue