diff --git a/Module/EntityCache/UserCacheItem.cs b/EntityCache/CacheUser.cs
similarity index 55%
rename from Module/EntityCache/UserCacheItem.cs
rename to EntityCache/CacheUser.cs
index d82ace5..56c58f6 100644
--- a/Module/EntityCache/UserCacheItem.cs
+++ b/EntityCache/CacheUser.cs
@@ -1,15 +1,18 @@
-using System;
+using Discord.WebSocket;
+using Npgsql;
+using System;
using System.Collections.Generic;
using System.Data.Common;
+using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
-namespace Noikoio.RegexBot.Module.EntityCache
+namespace Noikoio.RegexBot.EntityCache
{
///
- /// Represents a cached user.
+ /// Representation of a cached user.
///
- class UserCacheItem
+ class CacheUser
{
readonly ulong _userId;
readonly ulong _guildId;
@@ -57,8 +60,20 @@ namespace Noikoio.RegexBot.Module.EntityCache
///
public string AvatarUrl => _avatarUrl;
+ private CacheUser(SocketGuildUser u)
+ {
+ _userId = u.Id;
+ _guildId = u.Guild.Id;
+ _cacheDate = DateTime.UtcNow;
+ _username = u.Username;
+ _discriminator = u.Discriminator;
+ _nickname = u.Nickname;
+ _avatarUrl = u.GetAvatarUrl();
+ }
- private UserCacheItem(DbDataReader r)
+ // Double-check SqlHelper if making changes to this constant
+ const string QueryColumns = "user_id, guild_id, cache_date, username, discriminator, nickname, avatar_url";
+ private CacheUser(DbDataReader r)
{
// Double-check ordinals if making changes to QueryColumns
unchecked
@@ -77,19 +92,32 @@ namespace Noikoio.RegexBot.Module.EntityCache
public override string ToString() => DisplayName;
#region Queries
- const string QueryColumns = "user_id, guild_id, cache_date, username, discriminator, nickname, avatar_url";
-
- ///
- /// Attempts to query for an exact result with the given parameters.
- ///
- /// Null on no result.
- public static async Task QueryAsync(ulong guild, ulong user)
+ // Accessible by EntityCache. Documentation is there.
+ internal static async Task QueryAsync(DiscordSocketClient c, ulong guild, ulong user)
{
- using (var db = await RegexBot.Config.Database.GetOpenConnectionAsync())
+ // Local cache search
+ var lresult = LocalQueryAsync(c, guild, user);
+ if (lresult != null) return lresult;
+
+ // Database cache search
+ var db = await RegexBot.Config.Database.GetOpenConnectionAsync();
+ if (db == null) return null; // Database not available for query.
+ using (db) return await DbQueryAsync(db, guild, user);
+ }
+
+ private static CacheUser LocalQueryAsync(DiscordSocketClient c, ulong guild, ulong user)
+ {
+ var u = c.GetGuild(guild)?.GetUser(user);
+ if (u == null) return null;
+ return new CacheUser(u);
+ }
+ private static async Task DbQueryAsync(NpgsqlConnection db, ulong guild, ulong user)
+ {
+ using (db)
{
using (var c = db.CreateCommand())
{
- c.CommandText = $"SELECT {QueryColumns} FROM {Sql.TableUser} WHERE "
+ c.CommandText = $"SELECT {QueryColumns} FROM {SqlHelper.TableUser} WHERE "
+ "user_id = @Uid AND guild_id = @Gid";
c.Parameters.Add("@Uid", NpgsqlTypes.NpgsqlDbType.Bigint).Value = user;
c.Parameters.Add("@Gid", NpgsqlTypes.NpgsqlDbType.Bigint).Value = guild;
@@ -98,7 +126,7 @@ namespace Noikoio.RegexBot.Module.EntityCache
{
if (await r.ReadAsync())
{
- return new UserCacheItem(r);
+ return new CacheUser(r);
}
else
{
@@ -109,23 +137,21 @@ namespace Noikoio.RegexBot.Module.EntityCache
}
}
- private static Regex DiscriminatorSearch = new Regex(@"(.+)#(\d{4}(?!\d))", RegexOptions.Compiled);
+ // -----
- ///
- /// Attempts to look up the user given a search string.
- /// This string looks up case-insensitive, exact matches of nicknames and usernames.
- ///
- /// An containing zero or more query results, sorted by cache date.
- public static async Task> QueryAsync(ulong guild, string search)
+ private static Regex DiscriminatorSearch = new Regex(@"(.+)#(\d{4}(?!\d))", RegexOptions.Compiled);
+ // Accessible by EntityCache. Documentation is there.
+ internal static async Task> QueryAsync(DiscordSocketClient c, ulong guild, string search)
{
- // Is search just a number? It's an ID.
+ // Is search just a number? Assume ID, pass it on to the correct place.
if (ulong.TryParse(search, out var presult))
{
- var r = await QueryAsync(guild, presult);
- if (r == null) return new UserCacheItem[0];
- else return new UserCacheItem[] { r };
+ var r = await QueryAsync(c, guild, presult);
+ if (r == null) return new CacheUser[0];
+ else return new CacheUser[] { r };
}
+ // Split name/discriminator
string name;
string disc;
var split = DiscriminatorSearch.Match(search);
@@ -140,14 +166,52 @@ namespace Noikoio.RegexBot.Module.EntityCache
disc = null;
}
- // Storing in HashSet to enforce uniqueness
- HashSet result = new HashSet(_uc);
+ // Local cache search
+ var lresult = LocalQueryAsync(c, guild, name, disc);
+ if (lresult.Count() != 0) return lresult;
- using (var db = await RegexBot.Config.Database.GetOpenConnectionAsync())
+ // Database cache search
+ var db = await RegexBot.Config.Database.GetOpenConnectionAsync();
+ if (db == null) return null; // Database not available for query.
+ using (db) return await DbQueryAsync(db, guild, name, disc);
+ }
+
+ private static IEnumerable LocalQueryAsync(DiscordSocketClient c, ulong guild, string name, string disc)
+ {
+ var g = c.GetGuild(guild);
+ if (g == null) return new CacheUser[] { };
+
+ bool Filter(string iun, string inn, string idc)
+ {
+ // Same logic as in the SQL query in the method below this one
+ bool match =
+ string.Equals(iun, name, StringComparison.InvariantCultureIgnoreCase)
+ || string.Equals(inn, name, StringComparison.InvariantCultureIgnoreCase);
+
+ if (match && disc != null)
+ match = idc.Equals(disc);
+
+ return match;
+ }
+
+ var qresult = g.Users.Where(i => Filter(i.Username, i.Nickname, i.Discriminator));
+ var result = new List();
+ foreach (var item in qresult)
+ {
+ result.Add(new CacheUser(item));
+ }
+ return result;
+ }
+
+ private static async Task> DbQueryAsync(NpgsqlConnection db, ulong guild, string name, string disc)
+ {
+ var result = new List();
+
+ using (db = await RegexBot.Config.Database.GetOpenConnectionAsync())
{
using (var c = db.CreateCommand())
{
- c.CommandText = $"SELECT {QueryColumns} FROM {Sql.TableUser} WHERE "
+ c.CommandText = $"SELECT {QueryColumns} FROM {SqlHelper.TableUser} WHERE "
+ "( lower(username) = lower(@NameSearch) OR lower(nickname) = lower(@NameSearch) )";
c.Parameters.Add("@NameSearch", NpgsqlTypes.NpgsqlDbType.Text).Value = name;
if (disc != null)
@@ -161,21 +225,13 @@ namespace Noikoio.RegexBot.Module.EntityCache
{
while (await r.ReadAsync())
{
- result.Add(new UserCacheItem(r));
+ result.Add(new CacheUser(r));
}
}
}
}
return result;
}
-
- private static UniqueChecker _uc = new UniqueChecker();
- class UniqueChecker : IEqualityComparer
- {
- public bool Equals(UserCacheItem x, UserCacheItem y) => x.UserId == y.UserId && x.GuildId == y.GuildId;
-
- public int GetHashCode(UserCacheItem obj) => unchecked((int)(obj.UserId ^ obj.GuildId));
- }
#endregion
}
}
diff --git a/EntityCache/EntityCache.cs b/EntityCache/EntityCache.cs
new file mode 100644
index 0000000..5937f40
--- /dev/null
+++ b/EntityCache/EntityCache.cs
@@ -0,0 +1,38 @@
+using Discord.WebSocket;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace Noikoio.RegexBot.EntityCache
+{
+ ///
+ /// Static class for accessing the entity cache.
+ ///
+ static class EntityCache
+ {
+ /*
+ * The entity cache works by combining data known/cached by Discord.Net in addition to
+ * what has been stored in the database. If data does not exist in the former, it is
+ * retrieved from the latter.
+ * In either case, the resulting data is placed within a cache item object.
+ */
+
+ static DiscordSocketClient _client;
+ internal static void SetClient(DiscordSocketClient c) => _client = _client ?? c;
+
+ ///
+ /// Attempts to query for an exact result with the given parameters.
+ /// Does not handle exceptions that may occur.
+ ///
+ /// Null on no result.
+ internal static Task QueryAsync(ulong guild, ulong user)
+ => CacheUser.QueryAsync(_client, guild, user);
+
+ ///
+ /// Attempts to look up the user given a search string.
+ /// This string looks up case-insensitive, exact matches of nicknames and usernames.
+ ///
+ /// An containing zero or more query results, sorted by cache date.
+ internal static Task> QueryAsync(ulong guild, string search)
+ => CacheUser.QueryAsync(_client, guild, search);
+ }
+}
diff --git a/EntityCache/Module.cs b/EntityCache/Module.cs
new file mode 100644
index 0000000..f976632
--- /dev/null
+++ b/EntityCache/Module.cs
@@ -0,0 +1,124 @@
+using Discord.WebSocket;
+using Newtonsoft.Json.Linq;
+using Noikoio.RegexBot.ConfigItem;
+using Npgsql;
+using System.Threading.Tasks;
+
+namespace Noikoio.RegexBot.EntityCache
+{
+ ///
+ /// Bot module portion of the entity cache. 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 the entity cache.
+ ///
+ class Module : BotModule
+ {
+ private readonly DatabaseConfig _db;
+
+ public override string Name => nameof(EntityCache);
+
+ public Module(DiscordSocketClient client) : base(client)
+ {
+ _db = RegexBot.Config.Database;
+
+ if (_db.Available)
+ {
+ SqlHelper.CreateCacheTablesAsync().Wait();
+
+ client.GuildAvailable += Client_GuildAvailable;
+ client.GuildUpdated += Client_GuildUpdated;
+ client.GuildMemberUpdated += Client_GuildMemberUpdated;
+ client.UserJoined += Client_UserJoined;
+ client.UserLeft += Client_UserLeft;
+ }
+ else
+ {
+ Log("No database storage available.").Wait();
+ }
+ }
+
+ public override Task