using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
using Npgsql;
using NpgsqlTypes;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Noikoio.RegexBot.Module.EntityCache
{
///
/// Caches information regarding all known guilds, channels, and users.
/// The function of this module should be transparent to the user, and thus no configuration is needed.
/// This module should be initialized BEFORE any other modules that make use of guild and user cache.
///
class EntityCache : BotModule
{
/*
* Future plans:
* Have this, or something connected to this class, be accessible throughout the bot.
*
* There should be a system that holds a small in-memory cache of users (as EntityCache objects)
* for quick lookups by other parts of the bot.
* Without this system, we'll be having future bot features constantly querying the database
* on their own to look up entity cache records, which (among other things) could result in
* a race conditions where an event becomes aware of a user before it has been recorded.
*/
private readonly DatabaseConfig _db;
public override string Name => nameof(EntityCache);
public EntityCache(DiscordSocketClient client) : base(client)
{
_db = RegexBot.Config.Database;
if (_db.Available)
{
Sql.CreateCacheTables();
client.GuildAvailable += Client_GuildAvailable;
client.GuildUpdated += Client_GuildUpdated;
client.GuildMemberUpdated += Client_GuildMemberUpdated;
client.UserJoined += Client_UserJoined;
}
else
{
Log("No database storage available.").Wait();
}
}
public override Task