Move member cache updating off main thread
This commit is contained in:
parent
a707792fe4
commit
12f8359b90
1 changed files with 16 additions and 9 deletions
|
@ -18,15 +18,22 @@ class UserCachingSubservice {
|
|||
bot.DiscordClient.GuildMemberUpdated += DiscordClient_GuildMemberUpdated;
|
||||
bot.DiscordClient.UserUpdated += DiscordClient_UserUpdated;
|
||||
}
|
||||
private async Task DiscordClient_GuildMembersDownloaded(SocketGuild arg) {
|
||||
_log($"{arg.Name}: Full member list obtained. Cache update in progress.");
|
||||
private Task DiscordClient_GuildMembersDownloaded(SocketGuild arg) {
|
||||
var userlist = arg.Users.ToList();
|
||||
_ = Task.Run(async () => {
|
||||
try {
|
||||
using var db = new BotDatabaseContext();
|
||||
foreach (var user in arg.Users) {
|
||||
foreach (var user in userlist) {
|
||||
UpdateUser(user, db);
|
||||
UpdateGuildUser(user, db);
|
||||
}
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue