Move member cache updating off main thread

This commit is contained in:
Noi 2022-12-03 17:21:20 -08:00
parent a707792fe4
commit 12f8359b90

View file

@ -18,15 +18,22 @@ class UserCachingSubservice {
bot.DiscordClient.GuildMemberUpdated += DiscordClient_GuildMemberUpdated; bot.DiscordClient.GuildMemberUpdated += DiscordClient_GuildMemberUpdated;
bot.DiscordClient.UserUpdated += DiscordClient_UserUpdated; bot.DiscordClient.UserUpdated += DiscordClient_UserUpdated;
} }
private async Task DiscordClient_GuildMembersDownloaded(SocketGuild arg) { private Task DiscordClient_GuildMembersDownloaded(SocketGuild arg) {
_log($"{arg.Name}: Full member list obtained. Cache update in progress."); var userlist = arg.Users.ToList();
using var db = new BotDatabaseContext(); _ = Task.Run(async () => {
foreach (var user in arg.Users) { try {
UpdateUser(user, db); using var db = new BotDatabaseContext();
UpdateGuildUser(user, db); foreach (var user in userlist) {
} UpdateUser(user, db);
var changes = await db.SaveChangesAsync(); UpdateGuildUser(user, db);
_log($"{arg.Name}: Member caches updated ({changes} database writes)."); }
var changes = await db.SaveChangesAsync();
_log($"{arg.Name}: Member caches updated ({changes} database writes).");
} catch (Exception ex) {
_log($"{arg.Name}: {ex}");
}
});
return Task.CompletedTask;
} }
private async Task DiscordClient_GuildMemberUpdated(Discord.Cacheable<SocketGuildUser, ulong> old, SocketGuildUser current) { private async Task DiscordClient_GuildMemberUpdated(Discord.Cacheable<SocketGuildUser, ulong> old, SocketGuildUser current) {