Split DBCache class into two separate classes

This commit is contained in:
Noikoio 2017-11-05 12:01:32 -08:00
parent 244debadf2
commit 97b3bddf20
3 changed files with 99 additions and 53 deletions

View file

@ -8,16 +8,16 @@ using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.DBCache namespace Noikoio.RegexBot.Feature.DBCache
{ {
/// <summary> /// <summary>
/// Caches information regarding all incoming messages and all known guilds, channels, and users. /// Caches information regarding all known guilds, channels, and users.
/// The function of this feature should be transparent to the user, and thus no configuration is needed. /// The function of this feature should be transparent to the user, and thus no configuration is needed.
/// </summary> /// </summary>
class DBCache : BotFeature class EntityCache : BotFeature
{ {
private readonly DatabaseConfig _db; private readonly DatabaseConfig _db;
public override string Name => "Database cache"; public override string Name => nameof(EntityCache);
public DBCache(DiscordSocketClient client) : base(client) public EntityCache(DiscordSocketClient client) : base(client)
{ {
_db = RegexBot.Config.Database; _db = RegexBot.Config.Database;
@ -25,8 +25,6 @@ namespace Noikoio.RegexBot.Feature.DBCache
client.GuildUpdated += Client_GuildUpdated; client.GuildUpdated += Client_GuildUpdated;
client.GuildMemberUpdated += Client_GuildMemberUpdated; client.GuildMemberUpdated += Client_GuildMemberUpdated;
// it may not be necessary to handle JoinedGuild, as GuildAvailable still provides info // it may not be necessary to handle JoinedGuild, as GuildAvailable still provides info
client.MessageReceived += Client_MessageReceived;
//client.MessageUpdated += Client_MessageUpdated;
} }
public override Task<object> ProcessConfiguration(JToken configSection) => Task.FromResult<object>(null); public override Task<object> ProcessConfiguration(JToken configSection) => Task.FromResult<object>(null);
@ -35,7 +33,6 @@ namespace Noikoio.RegexBot.Feature.DBCache
// Guild _and_ guild member information has become available // Guild _and_ guild member information has become available
private async Task Client_GuildAvailable(SocketGuild arg) private async Task Client_GuildAvailable(SocketGuild arg)
{ {
if (!_db.Enabled) return; if (!_db.Enabled) return;
await CreateCacheTables(arg.Id); await CreateCacheTables(arg.Id);
@ -47,7 +44,7 @@ namespace Noikoio.RegexBot.Feature.DBCache
private async Task Client_GuildUpdated(SocketGuild arg1, SocketGuild arg2) private async Task Client_GuildUpdated(SocketGuild arg1, SocketGuild arg2)
{ {
if (!_db.Enabled) return; if (!_db.Enabled) return;
throw new NotImplementedException(); await Task.Run(() => UpdateGuild(arg2));
} }
// Guild member information has changed // Guild member information has changed
@ -56,28 +53,11 @@ namespace Noikoio.RegexBot.Feature.DBCache
if (!_db.Enabled) return; if (!_db.Enabled) return;
await Task.Run(() => UpdateGuildMember(arg2)); await Task.Run(() => UpdateGuildMember(arg2));
} }
// A new message has been created
private async Task Client_MessageReceived(SocketMessage arg)
{
if (!_db.Enabled) return;
throw new NotImplementedException();
}
//private Task Client_MessageUpdated(Discord.Cacheable<Discord.IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
/*
* Edited messages seem to retain their ID. This is a problem.
* The point of this message cache was to have another feature be able to relay
* both the previous and current message at once.
* For now: Do nothing on updated messages.
*/
#endregion #endregion
#region Table setup #region Table setup
const string TableGuild = "cache_guild"; const string TableGuild = "cache_guild";
const string TableUser = "cache_users"; const string TableUser = "cache_users";
const string TableMessage = "cache_messages";
private async Task CreateCacheTables(ulong gid) private async Task CreateCacheTables(ulong gid)
{ {
@ -93,7 +73,7 @@ namespace Noikoio.RegexBot.Feature.DBCache
using (var db = await _db.OpenConnectionAsync(gid)) using (var db = await _db.OpenConnectionAsync(gid))
{ {
Task<int> c1, c2, c3; Task<int> c1, c2;
// Uh... didn't think this through. For now this is a table that'll only ever have one column. // Uh... didn't think this through. For now this is a table that'll only ever have one column.
// Got to rethink this in particular. // Got to rethink this in particular.
@ -120,24 +100,12 @@ namespace Noikoio.RegexBot.Feature.DBCache
c2 = c.ExecuteNonQueryAsync(); c2 = c.ExecuteNonQueryAsync();
} }
using (var c = db.CreateCommand())
{
c.CommandText = "CREATE TABLE IF NOT EXISTS " + TableMessage + "("
+ "snowflake bigint primary key, "
+ "cache_date timestamptz not null, "
+ "author bigint not null"
+ ")";
c3 = c.ExecuteNonQueryAsync();
}
await c1; await c1;
await c2; await c2;
await c3;
} }
} }
#endregion #endregion
#region Guild and user cache updates
private async Task UpdateGuild(SocketGuild g) private async Task UpdateGuild(SocketGuild g)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -154,18 +122,5 @@ namespace Noikoio.RegexBot.Feature.DBCache
var ml = new SocketGuildUser[] { user }; var ml = new SocketGuildUser[] { user };
return UpdateGuildMember(gid, ml); return UpdateGuildMember(gid, ml);
} }
#endregion
#region Message cache
private async Task CacheMessage(SocketMessage msg)
{
throw new NotImplementedException();
}
private async Task UpdateMessage(SocketMessage msg)
{
throw new NotImplementedException();
}
#endregion
} }
} }

View file

@ -0,0 +1,90 @@
using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Noikoio.RegexBot.Feature.DBCaches
{
/// <summary>
/// Caches information regarding all incoming messages.
/// The function of this feature should be transparent to the user, and thus no configuration is needed.
/// </summary>
class MessageCache : BotFeature
{
private readonly DatabaseConfig _db;
public override string Name => nameof(MessageCache);
#region Table setup
const string TableGuild = "cache_guild";
const string TableUser = "cache_users";
const string TableMessage = "cache_messages";
public MessageCache(DiscordSocketClient client) : base(client)
{
_db = RegexBot.Config.Database;
client.MessageReceived += Client_MessageReceived;
//client.MessageUpdated += Client_MessageUpdated;
}
public override Task<object> ProcessConfiguration(JToken configSection) => Task.FromResult<object>(null);
#region Event handling
// A new message has been created
private async Task Client_MessageReceived(SocketMessage arg)
{
if (!_db.Enabled) return;
throw new NotImplementedException();
}
//private Task Client_MessageUpdated(Discord.Cacheable<Discord.IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
/*
* Edited messages seem to retain their ID. This is a problem.
* The point of this message cache was to have another feature be able to relay
* both the previous and current message at once.
* For now: Do nothing on updated messages.
*/
#endregion
private async Task CreateCacheTables(ulong gid)
{
/* Note:
* We save information per guild in their own schemas named "g_NUM", where NUM is the Guild ID.
*
* The creation of these schemas is handled within here, but we're possibly facing a short delay
* in the event that other events that we're listening for come in without a schema having been
* created yet in which to put them in.
* Got to figure that out.
*/
await _db.CreateGuildSchemaAsync(gid);
using (var db = await _db.OpenConnectionAsync(gid))
{
using (var c = db.CreateCommand())
{
c.CommandText = "CREATE TABLE IF NOT EXISTS " + TableMessage + "("
+ "snowflake bigint primary key, "
+ "cache_date timestamptz not null, "
+ "author bigint not null"
+ ")";
await c.ExecuteNonQueryAsync();
}
}
}
#endregion
private async Task CacheMessage(SocketMessage msg)
{
throw new NotImplementedException();
}
private async Task UpdateMessage(SocketMessage msg)
{
throw new NotImplementedException();
}
}
}

View file

@ -48,7 +48,8 @@ namespace Noikoio.RegexBot
new Feature.AutoMod.AutoMod(_client), new Feature.AutoMod.AutoMod(_client),
new Feature.ModTools.ModTools(_client), new Feature.ModTools.ModTools(_client),
new Feature.AutoRespond.AutoRespond(_client), new Feature.AutoRespond.AutoRespond(_client),
new Feature.DBCache.DBCache(_client) new Feature.DBCache.EntityCache(_client),
new Feature.DBCache.MessageCache(_client)
}; };
var dlog = Logger.GetLogger("Discord.Net"); var dlog = Logger.GetLogger("Discord.Net");
_client.Log += async (arg) => _client.Log += async (arg) =>