From 9332c8a24ccfa11446e08d1e882d4d923bc8ff3d Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 5 Sep 2023 23:22:24 +0000 Subject: [PATCH] * Refactored some of the cleanup code * Fixed blacklist issues due to code conversion from efcore to linq2db * Fixed medusasearch not having a description --- .../Services/Impl/BlacklistService.cs | 43 +++++++------ .../_common/GamblingCleanupService.cs | 60 ++++++++++++++++++ .../_common/IGamblingCleanupService.cs | 62 +------------------ src/NadekoBot/Db/NadekoContext.cs | 13 ++-- src/NadekoBot/data/aliases.yml | 3 + .../data/strings/commands/commands.en-US.yml | 5 ++ 6 files changed, 101 insertions(+), 85 deletions(-) create mode 100644 src/Nadeko.Bot.Modules.Gambling/Gambling/_common/GamblingCleanupService.cs diff --git a/src/Nadeko.Bot.Common/Services/Impl/BlacklistService.cs b/src/Nadeko.Bot.Common/Services/Impl/BlacklistService.cs index 3f77f5fe0..f8cc93760 100644 --- a/src/Nadeko.Bot.Common/Services/Impl/BlacklistService.cs +++ b/src/Nadeko.Bot.Common/Services/Impl/BlacklistService.cs @@ -1,5 +1,6 @@ #nullable disable using LinqToDB; +using LinqToDB.Data; using LinqToDB.EntityFrameworkCore; using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Db; @@ -96,6 +97,16 @@ public sealed class BlacklistService : IExecOnMessage Type = type, }); + if (type == BlacklistType.User) + { + await uow.GetTable() + .Where(x => x.UserId == id) + .UpdateAsync(_ => new() + { + CurrencyAmount = 0 + }); + } + Reload(); } @@ -109,27 +120,23 @@ public sealed class BlacklistService : IExecOnMessage Reload(); } - public void BlacklistUsers(IReadOnlyCollection toBlacklist) + public async Task BlacklistUsers(IReadOnlyCollection toBlacklist) { - using (var uow = _db.GetDbContext()) + await using var uow = _db.GetDbContext(); + var bc = uow.GetTable(); + await bc.BulkCopyAsync(toBlacklist.Select(uid => new BlacklistEntry { - var bc = uow.Set(); - bc.AddRange(toBlacklist.Select(x => new BlacklistEntry - { - ItemId = x, - Type = BlacklistType.User - })); + ItemId = uid, + Type = BlacklistType.User + })); - // todo check if blacklist works and removes currency - uow.GetTable() - .UpdateAsync(x => toBlacklist.Contains(x.UserId), - _ => new() - { - CurrencyAmount = 0 - }); - - uow.SaveChanges(); - } + var blList = toBlacklist.ToList(); + await uow.GetTable() + .Where(x => blList.Contains(x.UserId)) + .UpdateAsync(_ => new() + { + CurrencyAmount = 0 + }); Reload(); } diff --git a/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/GamblingCleanupService.cs b/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/GamblingCleanupService.cs new file mode 100644 index 000000000..a10806284 --- /dev/null +++ b/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/GamblingCleanupService.cs @@ -0,0 +1,60 @@ +using LinqToDB; +using NadekoBot.Db.Models; +using NadekoBot.Services.Database.Models; + +namespace Nadeko.Bot.Modules.Gambling.Gambling._Common; + +public class GamblingCleanupService : IGamblingCleanupService, INService +{ + private readonly DbService _db; + + public GamblingCleanupService(DbService db) + { + _db = db; + } + + public async Task DeleteWaifus() + { + await using var ctx = _db.GetDbContext(); + await ctx.Set().DeleteAsync(); + await ctx.Set().DeleteAsync(); + await ctx.Set().DeleteAsync(); + await ctx.SaveChangesAsync(); + } + + public async Task DeleteWaifu(ulong userId) + { + await using var ctx = _db.GetDbContext(); + await ctx.Set() + .Where(x => x.User.UserId == userId) + .DeleteAsync(); + await ctx.Set() + .Where(x => x.WaifuInfo.Waifu.UserId == userId) + .DeleteAsync(); + await ctx.Set() + .Where(x => x.Claimer.UserId == userId) + .UpdateAsync(old => new WaifuInfo() + { + ClaimerId = null, + }); + await ctx.Set() + .Where(x => x.Waifu.UserId == userId) + .DeleteAsync(); + await ctx.SaveChangesAsync(); + } + + public async Task DeleteCurrency() + { + await using var uow = _db.GetDbContext(); + await uow.Set().UpdateAsync(_ => new DiscordUser() + { + CurrencyAmount = 0 + }); + + await uow.Set().DeleteAsync(); + await uow.Set().DeleteAsync(); + await uow.Set().DeleteAsync(); + await uow.SaveChangesAsync(); + } + +} \ No newline at end of file diff --git a/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/IGamblingCleanupService.cs b/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/IGamblingCleanupService.cs index 809d75a96..7f66250af 100644 --- a/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/IGamblingCleanupService.cs +++ b/src/Nadeko.Bot.Modules.Gambling/Gambling/_common/IGamblingCleanupService.cs @@ -1,68 +1,8 @@ -using LinqToDB; -using NadekoBot.Db.Models; -using NadekoBot.Services.Database.Models; +namespace Nadeko.Bot.Modules.Gambling.Gambling._Common; -namespace Nadeko.Bot.Modules.Gambling.Gambling._Common; - -// todo organize public interface IGamblingCleanupService { Task DeleteWaifus(); Task DeleteWaifu(ulong userId); Task DeleteCurrency(); -} - -public class GamblingCleanupService : IGamblingCleanupService, INService -{ - private readonly DbService _db; - - public GamblingCleanupService(DbService db) - { - _db = db; - } - - public async Task DeleteWaifus() - { - await using var ctx = _db.GetDbContext(); - await ctx.Set().DeleteAsync(); - await ctx.Set().DeleteAsync(); - await ctx.Set().DeleteAsync(); - await ctx.SaveChangesAsync(); - } - - public async Task DeleteWaifu(ulong userId) - { - await using var ctx = _db.GetDbContext(); - await ctx.Set() - .Where(x => x.User.UserId == userId) - .DeleteAsync(); - await ctx.Set() - .Where(x => x.WaifuInfo.Waifu.UserId == userId) - .DeleteAsync(); - await ctx.Set() - .Where(x => x.Claimer.UserId == userId) - .UpdateAsync(old => new WaifuInfo() - { - ClaimerId = null, - }); - await ctx.Set() - .Where(x => x.Waifu.UserId == userId) - .DeleteAsync(); - await ctx.SaveChangesAsync(); - } - - public async Task DeleteCurrency() - { - await using var uow = _db.GetDbContext(); - await uow.Set().UpdateAsync(_ => new DiscordUser() - { - CurrencyAmount = 0 - }); - - await uow.Set().DeleteAsync(); - await uow.Set().DeleteAsync(); - await uow.Set().DeleteAsync(); - await uow.SaveChangesAsync(); - } - } \ No newline at end of file diff --git a/src/NadekoBot/Db/NadekoContext.cs b/src/NadekoBot/Db/NadekoContext.cs index 338b320cf..f78d30ab1 100644 --- a/src/NadekoBot/Db/NadekoContext.cs +++ b/src/NadekoBot/Db/NadekoContext.cs @@ -1,5 +1,6 @@ #nullable disable using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using NadekoBot.Db.Models; using NadekoBot.Services.Database.Models; @@ -481,10 +482,10 @@ public abstract class NadekoContext : DbContext #endregion } -// #if DEBUG -// private static readonly ILoggerFactory _debugLoggerFactory = LoggerFactory.Create(x => x.AddConsole()); -// -// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) -// => optionsBuilder.UseLoggerFactory(_debugLoggerFactory); -// #endif +#if DEBUG + private static readonly ILoggerFactory _debugLoggerFactory = LoggerFactory.Create(x => x.AddConsole()); + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + => optionsBuilder.UseLoggerFactory(_debugLoggerFactory); +#endif } \ No newline at end of file diff --git a/src/NadekoBot/data/aliases.yml b/src/NadekoBot/data/aliases.yml index c09a86afa..afcc0841b 100644 --- a/src/NadekoBot/data/aliases.yml +++ b/src/NadekoBot/data/aliases.yml @@ -1358,6 +1358,9 @@ medusalist: medusainfo: - medusainfo - meinfo +medusasearch: + - medusasearch + - mesearch # Bank stuff bankdeposit: - deposit diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index 0639cddcd..a80216d3c 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -2282,6 +2282,11 @@ medusalist: Read about the medusa system [here](https://nadekobot.readthedocs.io/en/latest/medusa/creating-a-medusa/) args: - "" +medusasearch: + desc: |- + Searches for medusae online given the search term + args: + - "shrine" bankdeposit: desc: "Deposits the specified amount of currency into the bank for later use." args: