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
|
||||
{
|
||||
public sealed class RedisSingleton
|
||||
public interface IRedisClient
|
||||
{
|
||||
private RedisSingleton()
|
||||
{
|
||||
var redis = ConnectionMultiplexer.Connect("localhost:6379");
|
||||
if (redis.IsConnected)
|
||||
{
|
||||
Console.WriteLine("Connection to Redis Enstablished");
|
||||
IDatabase Client { get; set; }
|
||||
}
|
||||
else
|
||||
|
||||
public sealed class RedisClient : IRedisClient
|
||||
{
|
||||
Console.WriteLine("Connection to Redis Failed");
|
||||
}
|
||||
}
|
||||
private static readonly Lazy<RedisSingleton> lazy = new Lazy<RedisSingleton>(() => new RedisSingleton());
|
||||
public static RedisSingleton Instance
|
||||
public RedisClient()
|
||||
{
|
||||
get
|
||||
var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
||||
if (!redis.IsConnected)
|
||||
{
|
||||
return lazy.Value;
|
||||
Console.WriteLine("Could not Connect to the Server...");
|
||||
}
|
||||
Client = redis.GetDatabase();
|
||||
}
|
||||
|
||||
public IDatabase Client { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,31 +1,37 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
public class StatsRecorder
|
||||
{
|
||||
|
||||
private SocketMessage message;
|
||||
private readonly SocketMessage message;
|
||||
private readonly IDatabase redis;
|
||||
|
||||
public StatsRecorder(SocketMessage message)
|
||||
{
|
||||
this.message = message;
|
||||
var db = new RedisSingleton();
|
||||
redis = new RedisClient().Client;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public async Task UpdateGuildRecordAsync()
|
||||
{
|
||||
var channel = (SocketGuildChannel) message.Channel;
|
||||
// Console.WriteLine(channel.Guild.Name + " earned a point");
|
||||
var guildId = ((SocketGuildChannel) message.Channel).Guild.Id;
|
||||
var key = guildId + "-messages";
|
||||
var messages = (int)redis.StringGet(key);
|
||||
redis.StringSet(key, (messages + 1).ToString());
|
||||
await Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
|
||||
namespace Geekbot.net.Modules
|
||||
{
|
||||
|
@ -15,9 +16,17 @@ namespace Geekbot.net.Modules
|
|||
|
||||
var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays);
|
||||
|
||||
await ReplyAsync($"{userInfo.Username}#{userInfo.Discriminator}\r\n" +
|
||||
$"Account created at {userInfo.CreatedAt.Day}.{userInfo.CreatedAt.Month}.{userInfo.CreatedAt.Year}, that is {age} days ago\r\n" +
|
||||
$"Currently {userInfo.Status}");
|
||||
var redis = new RedisClient().Client;
|
||||
var key = Context.Guild.Id + "-" + userInfo.Id + "-messages";
|
||||
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")]
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Reflection;
|
|||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.Net.Queue;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Modules;
|
||||
|
@ -51,6 +52,8 @@ namespace Geekbot.net
|
|||
client.MessageReceived += HandleCommand;
|
||||
client.MessageReceived += HandleMessageReceived;
|
||||
|
||||
client.UserJoined += HandleUserJoined;
|
||||
|
||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||
}
|
||||
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);
|
||||
|
||||
var statsRecorder = new StatsRecorder(message);
|
||||
var updateUserRecordAsync = statsRecorder.UpdateUserRecordAsync();
|
||||
var updateGuildRecordAsync = statsRecorder.UpdateGuildRecordAsync();
|
||||
#pragma warning disable 4014
|
||||
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