From 45b654eab1d087bcdcc059d7a72462415e64e4fa Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 20 Jan 2024 11:40:15 +0000 Subject: [PATCH] fixed build errors --- .../Nadeko.Bot.Common.csproj | 2 +- .../Extensions/DiscordUserExtensions.cs | 48 ----------------- .../Nadeko.Bot.Modules.Administration.csproj | 2 +- .../Self/SelfCommands.cs | 2 +- .../Self/SelfService.cs | 53 +++++++++++++++++++ .../Nadeko.Bot.Modules.Expresssions.csproj | 2 +- .../Nadeko.Bot.Modules.Permisssions.csproj | 2 +- src/Nadeko.Medusa/Nadeko.Medusa.csproj | 2 +- 8 files changed, 59 insertions(+), 54 deletions(-) diff --git a/src/Nadeko.Bot.Common/Nadeko.Bot.Common.csproj b/src/Nadeko.Bot.Common/Nadeko.Bot.Common.csproj index 7250f219a..a5d86798a 100644 --- a/src/Nadeko.Bot.Common/Nadeko.Bot.Common.csproj +++ b/src/Nadeko.Bot.Common/Nadeko.Bot.Common.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs index a71c02730..3f15e189a 100644 --- a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs +++ b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs @@ -8,54 +8,6 @@ namespace NadekoBot.Db; public static class DiscordUserExtensions { - /// - /// Adds the specified to the database. If a database user with placeholder name - /// and discriminator is present in , their name and discriminator get updated accordingly. - /// - /// This database context. - /// The users to add or update in the database. - /// A tuple with the amount of new users added and old users updated. - public static async Task<(long UsersAdded, long UsersUpdated)> RefreshUsersAsync(this NadekoContext ctx, List users) - { - var presentDbUsers = await ctx.DiscordUser - .Select(x => new { x.UserId, x.Username, x.Discriminator }) - .Where(x => users.Select(y => y.Id).Contains(x.UserId)) - .ToArrayAsyncEF(); - - var usersToAdd = users - .Where(x => !presentDbUsers.Select(x => x.UserId).Contains(x.Id)) - .Select(x => new DiscordUser() - { - UserId = x.Id, - AvatarId = x.AvatarId, - Username = x.Username, - Discriminator = x.Discriminator - }); - - var added = (await ctx.BulkCopyAsync(usersToAdd)).RowsCopied; - var toUpdateUserIds = presentDbUsers - .Where(x => x.Username == "Unknown" && x.Discriminator == "????") - .Select(x => x.UserId) - .ToArray(); - - foreach (var user in users.Where(x => toUpdateUserIds.Contains(x.Id))) - { - await ctx.DiscordUser - .Where(x => x.UserId == user.Id) - .UpdateAsync(x => new DiscordUser() - { - Username = user.Username, - Discriminator = user.Discriminator, - - // .award tends to set AvatarId and DateAdded to NULL, so account for that. - AvatarId = user.AvatarId, - DateAdded = x.DateAdded ?? DateTime.UtcNow - }); - } - - return (added, toUpdateUserIds.Length); - } - public static Task GetByUserIdAsync( this IQueryable set, ulong userId) diff --git a/src/Nadeko.Bot.Modules.Administration/Nadeko.Bot.Modules.Administration.csproj b/src/Nadeko.Bot.Modules.Administration/Nadeko.Bot.Modules.Administration.csproj index 5b7e9b2a9..13300e68c 100644 --- a/src/Nadeko.Bot.Modules.Administration/Nadeko.Bot.Modules.Administration.csproj +++ b/src/Nadeko.Bot.Modules.Administration/Nadeko.Bot.Modules.Administration.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/Nadeko.Bot.Modules.Administration/Self/SelfCommands.cs b/src/Nadeko.Bot.Modules.Administration/Self/SelfCommands.cs index e2573b423..a4d5f6120 100644 --- a/src/Nadeko.Bot.Modules.Administration/Self/SelfCommands.cs +++ b/src/Nadeko.Bot.Modules.Administration/Self/SelfCommands.cs @@ -60,7 +60,7 @@ public partial class Administration .Cast() .ToList(); - var (added, updated) = await dbContext.RefreshUsersAsync(users); + var (added, updated) = await _service.RefreshUsersAsync(users); await message.ModifyAsync(x => x.Embed = _eb.Create() diff --git a/src/Nadeko.Bot.Modules.Administration/Self/SelfService.cs b/src/Nadeko.Bot.Modules.Administration/Self/SelfService.cs index a29efd41c..6018f44f1 100644 --- a/src/Nadeko.Bot.Modules.Administration/Self/SelfService.cs +++ b/src/Nadeko.Bot.Modules.Administration/Self/SelfService.cs @@ -3,6 +3,9 @@ using Microsoft.EntityFrameworkCore; using NadekoBot.Common.ModuleBehaviors; using Nadeko.Bot.Db.Models; using System.Collections.Immutable; +using LinqToDB; +using LinqToDB.EntityFrameworkCore; +using NadekoBot.Db.Models; namespace NadekoBot.Modules.Administration.Services; @@ -396,4 +399,54 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService public string Link { get; init; } public ActivityType Type { get; init; } } + + + /// + /// Adds the specified to the database. If a database user with placeholder name + /// and discriminator is present in , their name and discriminator get updated accordingly. + /// + /// This database context. + /// The users to add or update in the database. + /// A tuple with the amount of new users added and old users updated. + public async Task<(long UsersAdded, long UsersUpdated)> RefreshUsersAsync(List users) + { + await using var ctx = _db.GetDbContext(); + var presentDbUsers = await ctx.GetTable() + .Select(x => new { x.UserId, x.Username, x.Discriminator }) + .Where(x => users.Select(y => y.Id).Contains(x.UserId)) + .ToArrayAsyncEF(); + + var usersToAdd = users + .Where(x => !presentDbUsers.Select(x => x.UserId).Contains(x.Id)) + .Select(x => new DiscordUser() + { + UserId = x.Id, + AvatarId = x.AvatarId, + Username = x.Username, + Discriminator = x.Discriminator + }); + + var added = (await ctx.BulkCopyAsync(usersToAdd)).RowsCopied; + var toUpdateUserIds = presentDbUsers + .Where(x => x.Username == "Unknown" && x.Discriminator == "????") + .Select(x => x.UserId) + .ToArray(); + + foreach (var user in users.Where(x => toUpdateUserIds.Contains(x.Id))) + { + await ctx.GetTable() + .Where(x => x.UserId == user.Id) + .UpdateAsync(x => new DiscordUser() + { + Username = user.Username, + Discriminator = user.Discriminator, + + // .award tends to set AvatarId and DateAdded to NULL, so account for that. + AvatarId = user.AvatarId, + DateAdded = x.DateAdded ?? DateTime.UtcNow + }); + } + + return (added, toUpdateUserIds.Length); + } } \ No newline at end of file diff --git a/src/Nadeko.Bot.Modules.Expresssions/Nadeko.Bot.Modules.Expresssions.csproj b/src/Nadeko.Bot.Modules.Expresssions/Nadeko.Bot.Modules.Expresssions.csproj index 394e92d30..8ae72bc33 100644 --- a/src/Nadeko.Bot.Modules.Expresssions/Nadeko.Bot.Modules.Expresssions.csproj +++ b/src/Nadeko.Bot.Modules.Expresssions/Nadeko.Bot.Modules.Expresssions.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Nadeko.Bot.Modules.Permisssions/Nadeko.Bot.Modules.Permisssions.csproj b/src/Nadeko.Bot.Modules.Permisssions/Nadeko.Bot.Modules.Permisssions.csproj index 1dc52c7ae..b85ad4c11 100644 --- a/src/Nadeko.Bot.Modules.Permisssions/Nadeko.Bot.Modules.Permisssions.csproj +++ b/src/Nadeko.Bot.Modules.Permisssions/Nadeko.Bot.Modules.Permisssions.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Nadeko.Medusa/Nadeko.Medusa.csproj b/src/Nadeko.Medusa/Nadeko.Medusa.csproj index 3e9bb055c..ac909115c 100644 --- a/src/Nadeko.Medusa/Nadeko.Medusa.csproj +++ b/src/Nadeko.Medusa/Nadeko.Medusa.csproj @@ -9,7 +9,7 @@ - +