Stats Recorder, Redis, welcome on join
This commit is contained in:
parent
3ce207749f
commit
1fdbcaa471
4 changed files with 53 additions and 28 deletions
|
@ -3,28 +3,24 @@ using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Geekbot.net.Lib
|
namespace Geekbot.net.Lib
|
||||||
{
|
{
|
||||||
public sealed class RedisSingleton
|
public interface IRedisClient
|
||||||
{
|
{
|
||||||
private RedisSingleton()
|
IDatabase Client { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class RedisClient : IRedisClient
|
||||||
|
{
|
||||||
|
public RedisClient()
|
||||||
{
|
{
|
||||||
var redis = ConnectionMultiplexer.Connect("localhost:6379");
|
var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
||||||
if (redis.IsConnected)
|
if (!redis.IsConnected)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Connection to Redis Enstablished");
|
Console.WriteLine("Could not Connect to the Server...");
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("Connection to Redis Failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private static readonly Lazy<RedisSingleton> lazy = new Lazy<RedisSingleton>(() => new RedisSingleton());
|
|
||||||
public static RedisSingleton Instance
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return lazy.Value;
|
|
||||||
}
|
}
|
||||||
|
Client = redis.GetDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IDatabase Client { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,31 +1,37 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Geekbot.net.Lib;
|
using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Geekbot.net.Lib
|
namespace Geekbot.net.Lib
|
||||||
{
|
{
|
||||||
public class StatsRecorder
|
public class StatsRecorder
|
||||||
{
|
{
|
||||||
|
|
||||||
private SocketMessage message;
|
private readonly SocketMessage message;
|
||||||
|
private readonly IDatabase redis;
|
||||||
|
|
||||||
public StatsRecorder(SocketMessage message)
|
public StatsRecorder(SocketMessage message)
|
||||||
{
|
{
|
||||||
this.message = message;
|
this.message = message;
|
||||||
var db = new RedisSingleton();
|
redis = new RedisClient().Client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateUserRecordAsync()
|
public async Task UpdateUserRecordAsync()
|
||||||
{
|
{
|
||||||
// Console.WriteLine(message.Author.Username + " earned a point");
|
var guildId = ((SocketGuildChannel) message.Channel).Guild.Id;
|
||||||
|
var key = guildId + "-" + message.Author.Id + "-messages";
|
||||||
|
var messages = (int)redis.StringGet(key);
|
||||||
|
redis.StringSet(key, (messages + 1).ToString());
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateGuildRecordAsync()
|
public async Task UpdateGuildRecordAsync()
|
||||||
{
|
{
|
||||||
var channel = (SocketGuildChannel) message.Channel;
|
var guildId = ((SocketGuildChannel) message.Channel).Guild.Id;
|
||||||
// Console.WriteLine(channel.Guild.Name + " earned a point");
|
var key = guildId + "-messages";
|
||||||
|
var messages = (int)redis.StringGet(key);
|
||||||
|
redis.StringSet(key, (messages + 1).ToString());
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
|
|
||||||
namespace Geekbot.net.Modules
|
namespace Geekbot.net.Modules
|
||||||
{
|
{
|
||||||
|
@ -15,9 +16,17 @@ namespace Geekbot.net.Modules
|
||||||
|
|
||||||
var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays);
|
var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays);
|
||||||
|
|
||||||
await ReplyAsync($"{userInfo.Username}#{userInfo.Discriminator}\r\n" +
|
var redis = new RedisClient().Client;
|
||||||
$"Account created at {userInfo.CreatedAt.Day}.{userInfo.CreatedAt.Month}.{userInfo.CreatedAt.Year}, that is {age} days ago\r\n" +
|
var key = Context.Guild.Id + "-" + userInfo.Id + "-messages";
|
||||||
$"Currently {userInfo.Status}");
|
var messages = (int)redis.StringGet(key);
|
||||||
|
var level = GetLevelAtExperience(messages);
|
||||||
|
|
||||||
|
await ReplyAsync($"```\r\n" +
|
||||||
|
$"{userInfo.Username}#{userInfo.Discriminator}\r\n" +
|
||||||
|
$"Messages Sent: {messages}\r\n" +
|
||||||
|
$"Level: {level}\r\n" +
|
||||||
|
$"Discordian Since: {userInfo.CreatedAt.Day}/{userInfo.CreatedAt.Month}/{userInfo.CreatedAt.Year} ({age} days)" +
|
||||||
|
$"```");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("level"), Summary("Get a level based on a number")]
|
[Command("level"), Summary("Get a level based on a number")]
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Discord.Net.Queue;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Geekbot.net.Lib;
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Modules;
|
using Geekbot.net.Modules;
|
||||||
|
@ -51,6 +52,8 @@ namespace Geekbot.net
|
||||||
client.MessageReceived += HandleCommand;
|
client.MessageReceived += HandleCommand;
|
||||||
client.MessageReceived += HandleMessageReceived;
|
client.MessageReceived += HandleMessageReceived;
|
||||||
|
|
||||||
|
client.UserJoined += HandleUserJoined;
|
||||||
|
|
||||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||||
}
|
}
|
||||||
public async Task HandleCommand(SocketMessage messageParam)
|
public async Task HandleCommand(SocketMessage messageParam)
|
||||||
|
@ -78,8 +81,19 @@ namespace Geekbot.net
|
||||||
Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + message.Content);
|
Console.WriteLine(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " + message.Content);
|
||||||
|
|
||||||
var statsRecorder = new StatsRecorder(message);
|
var statsRecorder = new StatsRecorder(message);
|
||||||
var updateUserRecordAsync = statsRecorder.UpdateUserRecordAsync();
|
#pragma warning disable 4014
|
||||||
var updateGuildRecordAsync = statsRecorder.UpdateGuildRecordAsync();
|
statsRecorder.UpdateUserRecordAsync();
|
||||||
|
statsRecorder.UpdateGuildRecordAsync();
|
||||||
|
#pragma warning restore 4014
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task HandleUserJoined(SocketGuildUser user)
|
||||||
|
{
|
||||||
|
if (!user.IsBot)
|
||||||
|
{
|
||||||
|
var message = $"Sali und wilkomme {user.Mention}";
|
||||||
|
await user.Guild.DefaultChannel.SendMessageAsync(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue