Abstract away redis even more with AlmostRedis.cs
This commit is contained in:
parent
f53258e348
commit
35f0a5c8f8
10 changed files with 153 additions and 78 deletions
|
@ -3,11 +3,11 @@ using System.Threading.Tasks;
|
|||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Lib.AlmostRedis;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.GlobalSettings;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.Lib.UserRepository;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.Admin
|
||||
{
|
||||
|
@ -20,10 +20,10 @@ namespace Geekbot.net.Commands.Admin
|
|||
private readonly DatabaseContext _database;
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
private readonly IGeekbotLogger _logger;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IAlmostRedis _redis;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings)
|
||||
public Owner(IAlmostRedis redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings)
|
||||
{
|
||||
_redis = redis;
|
||||
_client = client;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib.AlmostRedis;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Localization;
|
||||
using StackExchange.Redis;
|
||||
|
@ -10,10 +11,10 @@ namespace Geekbot.net.Commands.Games
|
|||
public class Roll : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IAlmostRedis _redis;
|
||||
private readonly ITranslationHandler _translation;
|
||||
|
||||
public Roll(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation)
|
||||
public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation)
|
||||
{
|
||||
_redis = redis;
|
||||
_translation = translation;
|
||||
|
@ -32,20 +33,20 @@ namespace Geekbot.net.Commands.Games
|
|||
var transDict = _translation.GetDict(Context);
|
||||
if (guess <= 100 && guess > 0)
|
||||
{
|
||||
var prevRoll = _redis.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id);
|
||||
var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious", Context.Message.Author.Id);
|
||||
if (!prevRoll.IsNullOrEmpty && prevRoll.ToString() == guess.ToString())
|
||||
{
|
||||
await ReplyAsync(string.Format(transDict["NoPrevGuess"], Context.Message.Author.Mention));
|
||||
return;
|
||||
}
|
||||
|
||||
_redis.HashSet($"{Context.Guild.Id}:RollsPrevious",
|
||||
_redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious",
|
||||
new[] {new HashEntry(Context.Message.Author.Id, guess)});
|
||||
await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess));
|
||||
if (guess == number)
|
||||
{
|
||||
await ReplyAsync(string.Format(transDict["Gratz"], Context.Message.Author));
|
||||
_redis.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
|
||||
_redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
@ -5,6 +5,7 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Lib.AlmostRedis;
|
||||
using Geekbot.net.Lib.Converters;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
|
@ -19,10 +20,10 @@ namespace Geekbot.net.Commands.User.Ranking
|
|||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IAlmostRedis _redis;
|
||||
|
||||
public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository,
|
||||
IEmojiConverter emojiConverter, IDatabase redis)
|
||||
IEmojiConverter emojiConverter, IAlmostRedis redis)
|
||||
{
|
||||
_database = database;
|
||||
_errorHandler = errorHandler;
|
||||
|
@ -138,7 +139,7 @@ namespace Geekbot.net.Commands.User.Ranking
|
|||
|
||||
private Dictionary<ulong, int> GetMessageList(int amount)
|
||||
{
|
||||
return _redis
|
||||
return _redis.Db
|
||||
.HashGetAll($"{Context.Guild.Id}:Messages").ToDictionary().Take(amount + 1)
|
||||
.Where(user => !user.Key.Equals(0))
|
||||
.ToDictionary(user => ulong.Parse(user.Key), user => int.Parse(user.Value));
|
||||
|
|
|
@ -4,10 +4,10 @@ using System.Threading.Tasks;
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Lib.AlmostRedis;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
using Geekbot.net.Lib.Levels;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.User
|
||||
{
|
||||
|
@ -15,10 +15,10 @@ namespace Geekbot.net.Commands.User
|
|||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly ILevelCalc _levelCalc;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IAlmostRedis _redis;
|
||||
private readonly DatabaseContext _database;
|
||||
|
||||
public Stats(IDatabase redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc)
|
||||
public Stats(IAlmostRedis redis, DatabaseContext database, IErrorHandler errorHandler, ILevelCalc levelCalc)
|
||||
{
|
||||
_redis = redis;
|
||||
_database = database;
|
||||
|
@ -39,8 +39,8 @@ namespace Geekbot.net.Commands.User
|
|||
var age = Math.Floor((DateTime.Now - createdAt).TotalDays);
|
||||
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays);
|
||||
|
||||
var messages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
|
||||
var guildMessages = (int) _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
|
||||
var messages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
|
||||
var guildMessages = (int) _redis.Db.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
|
||||
var level = _levelCalc.GetLevel(messages);
|
||||
|
||||
var percent = Math.Round((double) (100 * messages) / guildMessages, 2);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue