From 5f437d74ea35fa610fe12980105fdd16aa2512cc Mon Sep 17 00:00:00 2001 From: dboerlage Date: Tue, 18 Apr 2017 11:00:38 +0200 Subject: [PATCH] Fixing the redis bug, almost ready for prod --- Geekbot.net/Lib/RedisClient.cs | 40 ++------------------------------ Geekbot.net/Lib/StatsRecorder.cs | 4 ++-- Geekbot.net/Modules/AdminCmd.cs | 9 +++++-- Geekbot.net/Modules/Counters.cs | 16 ++++++++----- Geekbot.net/Modules/UserInfo.cs | 11 ++++++--- Geekbot.net/Program.cs | 23 +++++++++++------- 6 files changed, 44 insertions(+), 59 deletions(-) diff --git a/Geekbot.net/Lib/RedisClient.cs b/Geekbot.net/Lib/RedisClient.cs index f0f051a..801310a 100644 --- a/Geekbot.net/Lib/RedisClient.cs +++ b/Geekbot.net/Lib/RedisClient.cs @@ -3,42 +3,6 @@ using StackExchange.Redis; namespace Geekbot.net.Lib { - -// public class RedisClient -// { -// private static readonly Lazy _instance -// = new Lazy(() => new RedisClient()); -// private static readonly object ThreadLock = new object(); -// public static IDatabase Client; -// -// private RedisClient() -// { } -// -// public static RedisClient Instance -// { -// get -// { -// lock (ThreadLock) -// { -// if (Client == null) -// { -// try -// { -// var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"); -// Client = redis.GetDatabase(); -// } -// catch (Exception) -// { -// Console.WriteLine("Start Reids already you fucking faggot!"); -// Environment.Exit(69); -// } -// } -// } -// return _instance.Value; -// } -// } -// } - public interface IRedisClient { IDatabase Client { get; set; } @@ -55,8 +19,8 @@ namespace Geekbot.net.Lib } catch (Exception) { - Console.WriteLine("Start Redis already you fucking faggot!"); - Environment.Exit(69); + Console.WriteLine("Start Redis pls..."); + Environment.Exit(1); } } diff --git a/Geekbot.net/Lib/StatsRecorder.cs b/Geekbot.net/Lib/StatsRecorder.cs index 2016944..74a2db9 100644 --- a/Geekbot.net/Lib/StatsRecorder.cs +++ b/Geekbot.net/Lib/StatsRecorder.cs @@ -11,10 +11,10 @@ namespace Geekbot.net.Lib private readonly SocketMessage message; private readonly IDatabase redis; - public StatsRecorder(SocketMessage message) + public StatsRecorder(SocketMessage message, IRedisClient redisClient) { this.message = message; - redis = new RedisClient().Client; + redis = redisClient.Client; } public async Task UpdateUserRecordAsync() diff --git a/Geekbot.net/Modules/AdminCmd.cs b/Geekbot.net/Modules/AdminCmd.cs index cf5d71f..ffc2719 100644 --- a/Geekbot.net/Modules/AdminCmd.cs +++ b/Geekbot.net/Modules/AdminCmd.cs @@ -7,13 +7,18 @@ namespace Geekbot.net.Modules [Group("admin")] public class AdminCmd : ModuleBase { + private readonly IRedisClient redis; + public AdminCmd(IRedisClient redisClient) + { + redis = redisClient; + } + [RequireUserPermission(Discord.GuildPermission.Administrator)] [Command("welcome"), Summary("Set a Welcome Message (use '$user' to mention the new joined user).")] public async Task SetWelcomeMessage([Remainder, Summary("The message")] string welcomeMessage) { - var redis = new RedisClient().Client; var key = Context.Guild.Id + "-welcomeMsg"; - redis.StringSet(key, welcomeMessage); + redis.Client.StringSet(key, welcomeMessage); var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention); await ReplyAsync("Welcome message has been changed\r\nHere is an example of how it would look:\r\n" + formatedMessage); diff --git a/Geekbot.net/Modules/Counters.cs b/Geekbot.net/Modules/Counters.cs index 506090b..2f3b8b0 100644 --- a/Geekbot.net/Modules/Counters.cs +++ b/Geekbot.net/Modules/Counters.cs @@ -8,6 +8,12 @@ namespace Geekbot.net.Modules { public class Counters : ModuleBase { + private readonly IRedisClient redis; + public Counters(IRedisClient redisClient) + { + redis = redisClient; + } + [Command("good"), Summary("Increase Someones Karma")] public async Task Good([Summary("The someone")] IUser user) { @@ -17,10 +23,9 @@ namespace Geekbot.net.Modules } else { - var redis = new RedisClient().Client; var key = Context.Guild.Id + "-" + user.Id + "-karma"; - var badJokes = (int)redis.StringGet(key); - redis.StringSet(key, (badJokes + 1).ToString()); + var badJokes = (int)redis.Client.StringGet(key); + redis.Client.StringSet(key, (badJokes + 1).ToString()); await ReplyAsync($"{Context.User.Username} gave {user.Mention} karma"); } } @@ -34,10 +39,9 @@ namespace Geekbot.net.Modules } else { - var redis = new RedisClient().Client; var key = Context.Guild.Id + "-" + user.Id + "-karma"; - var badJokes = (int)redis.StringGet(key); - redis.StringSet(key, (badJokes - 1).ToString()); + var badJokes = (int)redis.Client.StringGet(key); + redis.Client.StringSet(key, (badJokes - 1).ToString()); await ReplyAsync($"{Context.User.Username} lowered {user.Mention}'s karma"); } } diff --git a/Geekbot.net/Modules/UserInfo.cs b/Geekbot.net/Modules/UserInfo.cs index f02f1c8..c207510 100644 --- a/Geekbot.net/Modules/UserInfo.cs +++ b/Geekbot.net/Modules/UserInfo.cs @@ -8,6 +8,12 @@ namespace Geekbot.net.Modules { public class UserInfo : ModuleBase { + private readonly IRedisClient redis; + public UserInfo(IRedisClient redisClient) + { + redis = redisClient; + } + [Alias("stats")] [Command("user"), Summary("Get information about this user")] public async Task User([Summary("The (optional) user to get info for")] IUser user = null) @@ -16,9 +22,8 @@ namespace Geekbot.net.Modules var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays); - var redis = new RedisClient().Client; var key = Context.Guild.Id + "-" + userInfo.Id; - var messages = (int)redis.StringGet(key + "-messages"); + var messages = (int)redis.Client.StringGet(key + "-messages"); var level = GetLevelAtExperience(messages); var eb = new EmbedBuilder(); @@ -30,7 +35,7 @@ namespace Geekbot.net.Modules eb.AddField("Level", level); eb.AddField("Messages Sent", messages); - var karma = redis.StringGet(key + "-karma"); + var karma = redis.Client.StringGet(key + "-karma"); if (!karma.IsNullOrEmpty) { eb.AddField("Karma", karma); diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index ae9abc3..d47ca10 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using Discord; @@ -15,7 +17,7 @@ namespace Geekbot.net private CommandService commands; private DiscordSocketClient client; private DependencyMap map; - private IDatabase redis; + private IRedisClient redis; private static void Main(string[] args) { @@ -35,29 +37,29 @@ namespace Geekbot.net { client = new DiscordSocketClient(); commands = new CommandService(); - redis = new RedisClient().Client; + redis = new RedisClient(); - var token = redis.StringGet("discordToken"); + var token = redis.Client.StringGet("discordToken"); if (token.IsNullOrEmpty) { Console.Write("Your bot Token: "); var newToken = Console.ReadLine(); - redis.StringSet("discordToken", newToken); + redis.Client.StringSet("discordToken", newToken); token = newToken; Console.Write("Bot Owner User ID: "); var ownerId = Console.ReadLine(); - redis.StringSet("botOwner", ownerId); + redis.Client.StringSet("botOwner", ownerId); } map = new DependencyMap(); map.Add(new CatClient()); + map.Add(redis); await InstallCommands(); Console.WriteLine("Connecting to Discord..."); await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); - redis = new RedisClient().Client; Console.WriteLine("Done and ready for use...\n"); await Task.Delay(-1); @@ -107,16 +109,21 @@ namespace Geekbot.net Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + message.Content); - var statsRecorder = new StatsRecorder(message); + var statsRecorder = new StatsRecorder(message, redis); await statsRecorder.UpdateUserRecordAsync(); await statsRecorder.UpdateGuildRecordAsync(); } public async Task HandleUserJoined(SocketGuildUser user) { +// var list = Directory.EnumerateFiles("", "", SearchOption.AllDirectories).ToList(); +// foreach (var file in list) +// { +// +// } if (!user.IsBot) { - var message = redis.StringGet(user.Guild.Id + "-welcomeMsg"); + var message = redis.Client.StringGet(user.Guild.Id + "-welcomeMsg"); if (!message.IsNullOrEmpty) { message = message.ToString().Replace("$user", user.Mention);