RegexBot/EntityCache/Module.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2017-11-01 23:24:22 +00:00
using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using Noikoio.RegexBot.ConfigItem;
2017-11-06 04:55:57 +00:00
using Npgsql;
using System.Threading.Tasks;
namespace Noikoio.RegexBot.EntityCache
{
/// <summary>
/// Bot module portion of the entity cache. Caches information regarding all known guilds, channels, and users.
2017-11-12 03:12:24 +00:00
/// The function of this module should be transparent to the user, and thus no configuration is needed.
2017-12-23 03:17:36 +00:00
/// This module should be initialized BEFORE any other modules that make use of the entity cache.
/// </summary>
class Module : BotModule
{
2017-11-01 23:24:22 +00:00
private readonly DatabaseConfig _db;
public override string Name => nameof(EntityCache);
public Module(DiscordSocketClient client) : base(client)
{
2017-11-01 23:24:22 +00:00
_db = RegexBot.Config.Database;
2017-11-12 03:12:24 +00:00
if (_db.Available)
{
2017-12-23 03:17:36 +00:00
SqlHelper.CreateCacheTablesAsync().Wait();
2017-11-06 04:55:57 +00:00
client.GuildAvailable += Client_GuildAvailable;
client.GuildUpdated += Client_GuildUpdated;
client.GuildMemberUpdated += Client_GuildMemberUpdated;
2017-12-08 21:52:32 +00:00
client.UserJoined += Client_UserJoined;
}
else
{
Log("No database storage available.").Wait();
}
}
public override Task<object> ProcessConfiguration(JToken configSection) => Task.FromResult<object>(null);
2017-11-01 23:24:22 +00:00
#region Event handling
2017-12-08 21:52:32 +00:00
// Guild and guild member information has become available.
// This is a very expensive operation, especially when joining larger guilds.
2017-11-01 23:24:22 +00:00
private async Task Client_GuildAvailable(SocketGuild arg)
{
2017-11-06 04:55:57 +00:00
await Task.Run(async () =>
{
2017-12-23 03:17:36 +00:00
try
{
await SqlHelper.UpdateGuildAsync(arg);
await SqlHelper.UpdateGuildMemberAsync(arg.Users);
}
catch (NpgsqlException ex)
{
await Log($"SQL error in {nameof(Client_GuildAvailable)}: {ex.Message}");
}
});
2017-11-01 23:24:22 +00:00
}
// Guild information has changed
2017-11-01 23:24:22 +00:00
private async Task Client_GuildUpdated(SocketGuild arg1, SocketGuild arg2)
{
2017-12-23 03:17:36 +00:00
await Task.Run(async () =>
{
try
{
await SqlHelper.UpdateGuildAsync(arg2);
}
catch (NpgsqlException ex)
{
await Log($"SQL error in {nameof(Client_GuildUpdated)}: {ex.Message}");
}
});
}
2017-11-01 23:24:22 +00:00
// Guild member information has changed
private async Task Client_GuildMemberUpdated(SocketGuildUser arg1, SocketGuildUser arg2)
{
2017-12-23 03:17:36 +00:00
await Task.Run(async () =>
{
try
{
await SqlHelper.UpdateGuildMemberAsync(arg2);
}
catch (NpgsqlException ex)
{
await Log($"SQL error in {nameof(Client_GuildMemberUpdated)}: {ex.Message}");
}
});
}
2017-12-08 21:52:32 +00:00
// A new guild member has appeared
private async Task Client_UserJoined(SocketGuildUser arg)
{
2017-12-23 03:17:36 +00:00
await Task.Run(async () =>
{
2017-12-23 03:17:36 +00:00
try
{
2017-12-23 03:17:36 +00:00
await SqlHelper.UpdateGuildMemberAsync(arg);
}
2017-12-23 03:17:36 +00:00
catch (NpgsqlException ex)
{
2017-12-23 03:17:36 +00:00
await Log($"SQL error in {nameof(Client_UserJoined)}: {ex.Message}");
}
2017-12-23 03:17:36 +00:00
});
2017-11-01 23:24:22 +00:00
}
2017-12-23 03:17:36 +00:00
#endregion
}
}