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;
|
2017-10-30 03:04:57 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-12-23 01:53:46 +00:00
|
|
|
|
namespace Noikoio.RegexBot.EntityCache
|
2017-10-30 03:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-12-23 01:53:46 +00:00
|
|
|
|
/// 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.
|
2017-10-30 03:04:57 +00:00
|
|
|
|
/// </summary>
|
2017-12-23 01:53:46 +00:00
|
|
|
|
class Module : BotModule
|
2017-10-30 03:04:57 +00:00
|
|
|
|
{
|
2017-11-01 23:24:22 +00:00
|
|
|
|
private readonly DatabaseConfig _db;
|
|
|
|
|
|
2017-12-23 01:53:46 +00:00
|
|
|
|
public Module(DiscordSocketClient client) : base(client)
|
2017-10-30 03:04:57 +00:00
|
|
|
|
{
|
2018-02-20 08:58:55 +00:00
|
|
|
|
if (RegexBot.Config.DatabaseAvailable)
|
2017-11-05 22:58:50 +00:00
|
|
|
|
{
|
2017-12-23 03:17:36 +00:00
|
|
|
|
SqlHelper.CreateCacheTablesAsync().Wait();
|
2017-11-06 04:55:57 +00:00
|
|
|
|
|
2017-11-05 22:58:50 +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;
|
2017-12-23 06:28:58 +00:00
|
|
|
|
client.UserLeft += Client_UserLeft;
|
2017-11-05 22:58:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log("No database storage available.").Wait();
|
|
|
|
|
}
|
2017-10-30 03:04:57 +00:00
|
|
|
|
}
|
2017-12-23 06:28:58 +00:00
|
|
|
|
|
2017-12-08 21:52:32 +00:00
|
|
|
|
// Guild and guild member information has become available.
|
2017-12-23 01:53:46 +00:00
|
|
|
|
// 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
|
|
|
|
}
|
2017-10-30 03:04:57 +00:00
|
|
|
|
|
|
|
|
|
// Guild information has changed
|
2017-11-01 23:24:22 +00:00
|
|
|
|
private async Task Client_GuildUpdated(SocketGuild arg1, SocketGuild arg2)
|
2017-10-30 03:04:57 +00:00
|
|
|
|
{
|
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-10-30 03:04:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 23:24:22 +00:00
|
|
|
|
// Guild member information has changed
|
|
|
|
|
private async Task Client_GuildMemberUpdated(SocketGuildUser arg1, SocketGuildUser arg2)
|
2017-10-30 03:04:57 +00:00
|
|
|
|
{
|
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-10-30 03:04:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-11-05 22:58:50 +00:00
|
|
|
|
{
|
2017-12-23 03:17:36 +00:00
|
|
|
|
try
|
2017-11-05 22:58:50 +00:00
|
|
|
|
{
|
2017-12-23 03:17:36 +00:00
|
|
|
|
await SqlHelper.UpdateGuildMemberAsync(arg);
|
2017-11-05 22:58:50 +00:00
|
|
|
|
}
|
2017-12-23 03:17:36 +00:00
|
|
|
|
catch (NpgsqlException ex)
|
2017-11-05 22:58:50 +00:00
|
|
|
|
{
|
2017-12-23 03:17:36 +00:00
|
|
|
|
await Log($"SQL error in {nameof(Client_UserJoined)}: {ex.Message}");
|
2017-11-05 22:58:50 +00:00
|
|
|
|
}
|
2017-12-23 03:17:36 +00:00
|
|
|
|
});
|
2017-11-01 23:24:22 +00:00
|
|
|
|
}
|
2017-12-23 06:28:58 +00:00
|
|
|
|
|
|
|
|
|
// User left the guild. No new data, but gives an excuse to update the cache date.
|
|
|
|
|
private async Task Client_UserLeft(SocketGuildUser arg)
|
|
|
|
|
{
|
|
|
|
|
await Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await SqlHelper.UpdateGuildMemberAsync(arg);
|
|
|
|
|
}
|
|
|
|
|
catch (NpgsqlException ex)
|
|
|
|
|
{
|
|
|
|
|
await Log($"SQL error in {nameof(Client_UserLeft)}: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-10-30 03:04:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|