Fixing the redis bug, almost ready for prod

This commit is contained in:
dboerlage 2017-04-18 11:00:38 +02:00
parent 735a4a81d4
commit 5f437d74ea
No known key found for this signature in database
GPG key ID: BDA07B7D3FCF147F
6 changed files with 44 additions and 59 deletions

View file

@ -3,42 +3,6 @@ using StackExchange.Redis;
namespace Geekbot.net.Lib
{
// public class RedisClient
// {
// private static readonly Lazy<RedisClient> _instance
// = new Lazy<RedisClient>(() => 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);
}
}

View file

@ -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()

View file

@ -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);

View file

@ -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");
}
}

View file

@ -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);

View file

@ -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<ICatClient>(new CatClient());
map.Add<IRedisClient>(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);