Port UserRepository and remove OW stuff
This commit is contained in:
parent
d2f31d0730
commit
15034d63a3
20 changed files with 176 additions and 328 deletions
|
@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
|
@ -17,17 +18,19 @@ namespace Geekbot.net.Commands.Admin
|
|||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IGeekbotLogger _logger;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler)
|
||||
public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database)
|
||||
{
|
||||
_redis = redis;
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
_userRepository = userRepositry;
|
||||
_errorHandler = errorHandler;
|
||||
_database = database;
|
||||
}
|
||||
|
||||
[Command("youtubekey", RunMode = RunMode.Async)]
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.UserRepository;
|
||||
|
||||
namespace Geekbot.net.Commands.Games
|
||||
{
|
||||
[Group("battletag")]
|
||||
public class BattleTag : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public BattleTag(IErrorHandler errorHandler, IUserRepository userRepository)
|
||||
{
|
||||
_errorHandler = errorHandler;
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
[Command(RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Games)]
|
||||
[Summary("Get your battletag")]
|
||||
public async Task BattleTagCmd()
|
||||
{
|
||||
try
|
||||
{
|
||||
var tag = _userRepository.GetUserSetting(Context.User.Id, "BattleTag");
|
||||
if (!string.IsNullOrEmpty(tag))
|
||||
await ReplyAsync($"Your BattleTag is {tag}");
|
||||
else
|
||||
await ReplyAsync("You haven't set your BattleTag, set it with `!battletag user#1234`");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
[Command(RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Games)]
|
||||
[Summary("Save your battletag")]
|
||||
public async Task BattleTagCmd([Summary("Battletag")] string tag)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsValidTag(tag))
|
||||
{
|
||||
_userRepository.SaveUserSetting(Context.User.Id, "BattleTag", tag);
|
||||
await ReplyAsync("Saved!");
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplyAsync("That doesn't seem to be a valid battletag");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsValidTag(string tag)
|
||||
{
|
||||
var splited = tag.Split("#");
|
||||
if (splited.Length != 2) return false;
|
||||
if (!int.TryParse(splited[1], out var discriminator)) return false;
|
||||
return splited[1].Length == 4 || splited[1].Length == 5;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,131 +0,0 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.UserRepository;
|
||||
using OverwatchAPI;
|
||||
using OverwatchAPI.Config;
|
||||
|
||||
namespace Geekbot.net.Commands.Games
|
||||
{
|
||||
[Group("ow")]
|
||||
public class Overwatch : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public Overwatch(IErrorHandler errorHandler, IUserRepository userRepository)
|
||||
{
|
||||
_errorHandler = errorHandler;
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
[Command("profile", RunMode = RunMode.Async)]
|
||||
[Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")]
|
||||
[Remarks(CommandCategories.Games)]
|
||||
public async Task OwProfile()
|
||||
{
|
||||
try
|
||||
{
|
||||
var tag = _userRepository.GetUserSetting(Context.User.Id, "BattleTag");
|
||||
if (string.IsNullOrEmpty(tag))
|
||||
{
|
||||
await ReplyAsync("You have no battle Tag saved, use `!battletag`");
|
||||
return;
|
||||
}
|
||||
|
||||
var profile = await CreateProfile(tag);
|
||||
if (profile == null)
|
||||
{
|
||||
await ReplyAsync("That player doesn't seem to exist");
|
||||
return;
|
||||
}
|
||||
|
||||
await ReplyAsync("", false, profile.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("profile", RunMode = RunMode.Async)]
|
||||
[Summary("Get someones overwatch profile. EU on PC only. Default battletag is your own (if set).")]
|
||||
[Remarks(CommandCategories.Games)]
|
||||
public async Task OwProfile([Summary("BattleTag")] string tag)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!BattleTag.IsValidTag(tag))
|
||||
{
|
||||
await ReplyAsync("That doesn't seem to be a valid battletag...");
|
||||
return;
|
||||
}
|
||||
|
||||
var profile = await CreateProfile(tag);
|
||||
if (profile == null)
|
||||
{
|
||||
await ReplyAsync("That player doesn't seem to exist");
|
||||
return;
|
||||
}
|
||||
|
||||
await ReplyAsync("", false, profile.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("profile", RunMode = RunMode.Async)]
|
||||
[Summary("Get someones overwatch profile. EU on PC only.")]
|
||||
[Remarks(CommandCategories.Games)]
|
||||
public async Task OwProfile([Summary("@someone")] IUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tag = _userRepository.GetUserSetting(user.Id, "BattleTag");
|
||||
if (string.IsNullOrEmpty(tag))
|
||||
{
|
||||
await ReplyAsync("This user didn't set a battletag");
|
||||
return;
|
||||
}
|
||||
|
||||
var profile = await CreateProfile(tag);
|
||||
if (profile == null)
|
||||
{
|
||||
await ReplyAsync("That player doesn't seem to exist");
|
||||
return;
|
||||
}
|
||||
|
||||
await ReplyAsync("", false, profile.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<EmbedBuilder> CreateProfile(string battletag)
|
||||
{
|
||||
var owConfig = new OverwatchConfig.Builder().WithPlatforms(Platform.Pc);
|
||||
using (var owClient = new OverwatchClient(owConfig))
|
||||
{
|
||||
var player = await owClient.GetPlayerAsync(battletag);
|
||||
if (player.Username == null) return null;
|
||||
var eb = new EmbedBuilder();
|
||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||
.WithIconUrl(player.ProfilePortraitUrl)
|
||||
.WithName(player.Username));
|
||||
eb.Url = player.ProfileUrl;
|
||||
eb.AddInlineField("Level", player.PlayerLevel);
|
||||
eb.AddInlineField("Current Rank",
|
||||
player.CompetitiveRank > 0 ? player.CompetitiveRank.ToString() : "Unranked");
|
||||
|
||||
return eb;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -81,7 +81,7 @@ namespace Geekbot.net.Commands.User.Ranking
|
|||
try
|
||||
{
|
||||
var guildUser = _userRepository.Get((ulong) user.Name);
|
||||
if (guildUser.Username != null)
|
||||
if (guildUser?.Username != null)
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfillDto
|
||||
{
|
||||
|
|
|
@ -18,11 +18,9 @@ namespace Geekbot.net.Commands.Utils.Quote
|
|||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IDatabase _redis;
|
||||
|
||||
public Quote(IDatabase redis, IErrorHandler errorHandler, DatabaseContext database)
|
||||
public Quote(IErrorHandler errorHandler, DatabaseContext database)
|
||||
{
|
||||
_redis = redis;
|
||||
_errorHandler = errorHandler;
|
||||
_database = database;
|
||||
}
|
||||
|
|
|
@ -6,12 +6,13 @@ namespace Geekbot.net.Database
|
|||
public class DatabaseContext : DbContext
|
||||
{
|
||||
public DbSet<QuoteModel> Quotes { get; set; }
|
||||
// public DbSet<UserModel> Users { get; set; }
|
||||
// public DbSet<GuildsModel> Guilds { get; set; }
|
||||
// public DbSet<GuildSettingsModel> GuildSettings { get; set; }
|
||||
// public DbSet<KarmaModel> Karma { get; set; }
|
||||
// public DbSet<ShipsModel> Ships { get; set; }
|
||||
public DbSet<UserModel> Users { get; set; }
|
||||
public DbSet<GuildsModel> Guilds { get; set; }
|
||||
public DbSet<GuildSettingsModel> GuildSettings { get; set; }
|
||||
public DbSet<KarmaModel> Karma { get; set; }
|
||||
public DbSet<ShipsModel> Ships { get; set; }
|
||||
public DbSet<SlapsModel> SlapsModels { get; set; }
|
||||
// public DbSet<UserSettingsModel> UserSettings { get; set; }
|
||||
// public DbSet<RoleSelfServiceModel> RoleSelfService { get; set; }
|
||||
// public DbSet<SlapsModel> SlapsModels { get; set; }
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
using Geekbot.net.Database.LoggingAdapter;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql.Logging;
|
||||
|
||||
namespace Geekbot.net.Database
|
||||
|
@ -39,6 +40,7 @@ namespace Geekbot.net.Database
|
|||
});
|
||||
}
|
||||
database.Database.EnsureCreated();
|
||||
database.Database.Migrate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
|
@ -1,7 +1,29 @@
|
|||
namespace Geekbot.net.Database.Models
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class GuildSettingsModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long GuildId { get; set; }
|
||||
|
||||
public bool Ping { get; set; }
|
||||
|
||||
public bool Hui { get; set; }
|
||||
|
||||
public long ModChannel { get; set; }
|
||||
|
||||
public string WelcomeMessage { get; set; }
|
||||
|
||||
public bool ShowDelete { get; set; }
|
||||
|
||||
public bool ShowLeave { get; set; }
|
||||
|
||||
public string WikiLang { get; set; }
|
||||
|
||||
public string Language { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,22 @@
|
|||
namespace Geekbot.net.Database.Models
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class GuildsModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long GuildId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Owner { get; set; }
|
||||
|
||||
[Required]
|
||||
public string IconUrl { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,21 @@
|
|||
namespace Geekbot.net.Database.Models
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class KarmaModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long GuildId { get; set; }
|
||||
|
||||
[Required]
|
||||
public long UserId { get; set; }
|
||||
|
||||
public int Karma { get; set; }
|
||||
|
||||
public DateTimeOffset TimeOut { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,16 @@
|
|||
namespace Geekbot.net.Database.Models
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class ShipsModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
public long FirstUserId { get; set; }
|
||||
|
||||
public long SecondUserId { get; set; }
|
||||
|
||||
public int Strength { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,20 @@
|
|||
namespace Geekbot.net.Database.Models
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class SlapsModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long GuildId { get; set; }
|
||||
|
||||
[Required]
|
||||
public long UserId { get; set; }
|
||||
|
||||
public int Given { get; set; }
|
||||
|
||||
public int Recieved { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,30 @@
|
|||
namespace Geekbot.net.Database.Models
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class UserModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long UserId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Username { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Discriminator { get; set; }
|
||||
|
||||
public string AvatarUrl { get; set; }
|
||||
|
||||
[Required]
|
||||
public bool IsBot { get; set; }
|
||||
|
||||
public DateTimeOffset Joined { get; set; }
|
||||
|
||||
public string[] UsedNames { get; set; }
|
||||
}
|
||||
}
|
15
Geekbot.net/Database/Models/UserSettingsModel.cs
Normal file
15
Geekbot.net/Database/Models/UserSettingsModel.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.net.Database.Models
|
||||
{
|
||||
public class UserSettingsModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long UserId { get; set; }
|
||||
|
||||
// stuff to be added in the future
|
||||
}
|
||||
}
|
|
@ -34,7 +34,6 @@
|
|||
<PackageReference Include="NLog.Config" Version="4.5.3" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
|
||||
<PackageReference Include="Overwatch.Net" Version="3.1.0" />
|
||||
<PackageReference Include="PokeApi.NET" Version="1.1.0" />
|
||||
<PackageReference Include="SharpRaven" Version="2.3.2" />
|
||||
<PackageReference Include="StackExchange.Redis">
|
||||
|
|
|
@ -103,7 +103,7 @@ namespace Geekbot.net
|
|||
// User Stuff
|
||||
//
|
||||
|
||||
public Task UserJoined(SocketGuildUser user)
|
||||
public async Task UserJoined(SocketGuildUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -113,23 +113,21 @@ namespace Geekbot.net
|
|||
if (!message.IsNullOrEmpty)
|
||||
{
|
||||
message = message.ToString().Replace("$user", user.Mention);
|
||||
user.Guild.DefaultChannel.SendMessageAsync(message);
|
||||
await user.Guild.DefaultChannel.SendMessageAsync(message);
|
||||
}
|
||||
}
|
||||
_userRepository.Update(user);
|
||||
await _userRepository.Update(user);
|
||||
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(LogSource.Geekbot, "Failed to send welcome message", e);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task UserUpdated(SocketUser oldUser, SocketUser newUser)
|
||||
public async Task UserUpdated(SocketUser oldUser, SocketUser newUser)
|
||||
{
|
||||
_userRepository.Update(newUser);
|
||||
return Task.CompletedTask;
|
||||
await _userRepository.Update(newUser);
|
||||
}
|
||||
|
||||
public async Task UserLeft(SocketGuildUser user)
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
using System.Threading.Tasks;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database.Models;
|
||||
|
||||
namespace Geekbot.net.Lib.UserRepository
|
||||
{
|
||||
public interface IUserRepository
|
||||
{
|
||||
Task<bool> Update(SocketUser user);
|
||||
UserRepositoryUser Get(ulong userId);
|
||||
string GetUserSetting(ulong userId, string setting);
|
||||
bool SaveUserSetting(ulong userId, string setting, string value);
|
||||
UserModel Get(ulong userId);
|
||||
}
|
||||
}
|
|
@ -1,125 +1,81 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using StackExchange.Redis;
|
||||
using Utf8Json;
|
||||
|
||||
namespace Geekbot.net.Lib.UserRepository
|
||||
{
|
||||
public class UserRepository : IUserRepository
|
||||
{
|
||||
private readonly IDatabase _redis;
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IGeekbotLogger _logger;
|
||||
public UserRepository(IDatabase redis, IGeekbotLogger logger)
|
||||
public UserRepository(DatabaseContext database, IGeekbotLogger logger)
|
||||
{
|
||||
_redis = redis;
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<bool> Update(SocketUser user)
|
||||
public async Task<bool> Update(SocketUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var savedUser = Get(user.Id);
|
||||
savedUser.Id = user.Id;
|
||||
var isNew = false;
|
||||
if (savedUser == null)
|
||||
{
|
||||
savedUser = new UserModel();
|
||||
isNew = true;
|
||||
}
|
||||
savedUser.UserId = user.Id.AsLong();
|
||||
savedUser.Username = user.Username;
|
||||
savedUser.Discriminator = user.Discriminator;
|
||||
savedUser.AvatarUrl = user.GetAvatarUrl() ?? "0";
|
||||
savedUser.AvatarUrl = user.GetAvatarUrl() ?? "";
|
||||
savedUser.IsBot = user.IsBot;
|
||||
savedUser.Joined = user.CreatedAt;
|
||||
if(savedUser.UsedNames == null) savedUser.UsedNames = new List<string>();
|
||||
if (savedUser.UsedNames == null) savedUser.UsedNames = Enumerable.Empty<string>().ToArray();
|
||||
if (!savedUser.UsedNames.Contains(user.Username))
|
||||
{
|
||||
savedUser.UsedNames.Add(user.Username);
|
||||
savedUser.UsedNames = savedUser.UsedNames.Concat(new[] {user.Username}).ToArray();
|
||||
}
|
||||
Store(savedUser);
|
||||
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
await _database.Users.AddAsync(savedUser);
|
||||
}
|
||||
else
|
||||
{
|
||||
_database.Users.Update(savedUser);
|
||||
}
|
||||
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
_logger.Information(LogSource.UserRepository, "Updated User", savedUser);
|
||||
return Task.FromResult(true);
|
||||
await Task.Delay(500);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(LogSource.UserRepository, $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e);
|
||||
return Task.FromResult(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Store(UserRepositoryUser user)
|
||||
{
|
||||
_redis.HashSetAsync($"Users:{user.Id.ToString()}", new[]
|
||||
{
|
||||
new HashEntry("Id", user.Id.ToString()),
|
||||
new HashEntry("Username", user.Username),
|
||||
new HashEntry("Discriminator", user.Discriminator),
|
||||
new HashEntry("AvatarUrl", user.AvatarUrl),
|
||||
new HashEntry("IsBot", user.IsBot),
|
||||
new HashEntry("Joined", user.Joined.ToString()),
|
||||
new HashEntry("UsedNames", JsonSerializer.Serialize(user.UsedNames))
|
||||
});
|
||||
}
|
||||
|
||||
public UserRepositoryUser Get(ulong userId)
|
||||
public UserModel Get(ulong userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = _redis.HashGetAll($"Users:{userId.ToString()}");
|
||||
for (var i = 1; i < 11; i++)
|
||||
{
|
||||
if (user.Length != 0) break;
|
||||
user = _redis.HashGetAll($"Users:{(userId + (ulong) i).ToString()}");
|
||||
|
||||
}
|
||||
var dto = new UserRepositoryUser();
|
||||
foreach (var a in user.ToDictionary())
|
||||
{
|
||||
switch (a.Key)
|
||||
{
|
||||
case "Id":
|
||||
dto.Id = ulong.Parse(a.Value);
|
||||
break;
|
||||
case "Username":
|
||||
dto.Username = a.Value.ToString();
|
||||
break;
|
||||
case "Discriminator":
|
||||
dto.Discriminator = a.Value.ToString();
|
||||
break;
|
||||
case "AvatarUrl":
|
||||
dto.AvatarUrl = a.Value != "0" ? a.Value.ToString() : null;
|
||||
break;
|
||||
case "IsBot":
|
||||
dto.IsBot = a.Value == 1;
|
||||
break;
|
||||
case "Joined":
|
||||
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;
|
||||
return _database.Users.FirstOrDefault(u => u.UserId.Equals(userId.AsLong()));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(LogSource.UserRepository, $"Failed to get {userId} from repository", e);
|
||||
return new UserRepositoryUser();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetUserSetting(ulong userId, string setting)
|
||||
{
|
||||
return _redis.HashGet($"Users:{userId}", setting);
|
||||
}
|
||||
|
||||
public bool SaveUserSetting(ulong userId, string setting, string value)
|
||||
{
|
||||
_redis.HashSet($"Users:{userId}", new[]
|
||||
{
|
||||
new HashEntry(setting, value)
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Geekbot.net.Lib.UserRepository
|
||||
{
|
||||
public class UserRepositoryUser
|
||||
{
|
||||
public ulong Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Discriminator { get; set; }
|
||||
public string AvatarUrl { get; set; }
|
||||
public bool IsBot { get; set; }
|
||||
public DateTimeOffset Joined { get; set; }
|
||||
public List<string> UsedNames { get; set; }
|
||||
}
|
||||
}
|
|
@ -111,7 +111,7 @@ namespace Geekbot.net
|
|||
|
||||
_services = new ServiceCollection();
|
||||
|
||||
_userRepository = new UserRepository(_redis, logger);
|
||||
_userRepository = new UserRepository(database, logger);
|
||||
var fortunes = new FortunesProvider(logger);
|
||||
var mediaProvider = new MediaProvider(logger);
|
||||
var malClient = new MalClient(_redis, logger);
|
||||
|
|
Loading…
Reference in a new issue