mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 16:24:27 -05:00 
			
		
		
		
	Split dangerous commands into cleanup commands for each module they belong to
This commit is contained in:
		@@ -45,7 +45,7 @@ public sealed class BankService : IBankService, INService
 | 
			
		||||
            throw new ArgumentOutOfRangeException(nameof(amount));
 | 
			
		||||
        
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        var rows = await ctx.BankUsers
 | 
			
		||||
        var rows = await ctx.Set<BankUser>()
 | 
			
		||||
            .ToLinqToDBTable()
 | 
			
		||||
            .Where(x => x.UserId == userId && x.Balance >= amount)
 | 
			
		||||
            .UpdateAsync((old) => new()
 | 
			
		||||
@@ -65,7 +65,7 @@ public sealed class BankService : IBankService, INService
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        await ctx.BankUsers
 | 
			
		||||
        await ctx.Set<BankUser>()
 | 
			
		||||
                 .ToLinqToDBTable()
 | 
			
		||||
                 .InsertOrUpdateAsync(() => new()
 | 
			
		||||
                     {
 | 
			
		||||
@@ -90,7 +90,7 @@ public sealed class BankService : IBankService, INService
 | 
			
		||||
            throw new ArgumentOutOfRangeException(nameof(amount));
 | 
			
		||||
        
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        var rows = await ctx.BankUsers
 | 
			
		||||
        var rows = await ctx.Set<BankUser>()
 | 
			
		||||
                 .ToLinqToDBTable()
 | 
			
		||||
                 .Where(x => x.UserId == userId && x.Balance >= amount)
 | 
			
		||||
                 .UpdateAsync((old) => new()
 | 
			
		||||
@@ -110,7 +110,7 @@ public sealed class BankService : IBankService, INService
 | 
			
		||||
    public async Task<long> GetBalanceAsync(ulong userId)
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        return (await ctx.BankUsers
 | 
			
		||||
        return (await ctx.Set<BankUser>()
 | 
			
		||||
                         .ToLinqToDBTable()
 | 
			
		||||
                         .FirstOrDefaultAsync(x => x.UserId == userId))
 | 
			
		||||
               ?.Balance
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										39
									
								
								src/Nadeko.Bot.Modules.Gambling/Gambling/CleanupCommands.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/Nadeko.Bot.Modules.Gambling/Gambling/CleanupCommands.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
using Nadeko.Bot.Modules.Gambling.Gambling._Common;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Gambling;
 | 
			
		||||
 | 
			
		||||
public partial class Gambling
 | 
			
		||||
{
 | 
			
		||||
    [Group]
 | 
			
		||||
    public class CleanupCommands : CleanupModuleBase
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IGamblingCleanupService _gcs;
 | 
			
		||||
 | 
			
		||||
        public CleanupCommands(IGamblingCleanupService gcs)
 | 
			
		||||
        {
 | 
			
		||||
            _gcs = gcs;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Cmd]
 | 
			
		||||
        [OwnerOnly]
 | 
			
		||||
        public Task DeleteWaifus()
 | 
			
		||||
            => ConfirmActionInternalAsync("Delete Waifus", () => _gcs.DeleteWaifus());
 | 
			
		||||
 | 
			
		||||
        [Cmd]
 | 
			
		||||
        [OwnerOnly]
 | 
			
		||||
        public async Task DeleteWaifu(IUser user)
 | 
			
		||||
            => await DeleteWaifu(user.Id);
 | 
			
		||||
 | 
			
		||||
        [Cmd]
 | 
			
		||||
        [OwnerOnly]
 | 
			
		||||
        public Task DeleteWaifu(ulong userId)
 | 
			
		||||
            => ConfirmActionInternalAsync($"Delete Waifu {userId}", () => _gcs.DeleteWaifu(userId));
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
        [Cmd]
 | 
			
		||||
        [OwnerOnly]
 | 
			
		||||
        public Task DeleteCurrency()
 | 
			
		||||
            => ConfirmActionInternalAsync("Delete Currency", () => _gcs.DeleteCurrency());
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -290,7 +290,7 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
			
		||||
        List<CurrencyTransaction> trs;
 | 
			
		||||
        await using (var uow = _db.GetDbContext())
 | 
			
		||||
        {
 | 
			
		||||
            trs = await uow.CurrencyTransactions.GetPageFor(userId, page);
 | 
			
		||||
            trs = await uow.Set<CurrencyTransaction>().GetPageFor(userId, page);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var embed = _eb.Create()
 | 
			
		||||
@@ -332,7 +332,7 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
			
		||||
        int intId = id;
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
 | 
			
		||||
        var tr = await uow.CurrencyTransactions.ToLinqToDBTable()
 | 
			
		||||
        var tr = await uow.Set<CurrencyTransaction>().ToLinqToDBTable()
 | 
			
		||||
            .Where(x => x.Id == intId && x.UserId == ctx.User.Id)
 | 
			
		||||
            .FirstOrDefaultAsync();
 | 
			
		||||
 | 
			
		||||
@@ -739,7 +739,7 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
			
		||||
        {
 | 
			
		||||
            await using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                cleanRichest = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 10_000);
 | 
			
		||||
                cleanRichest = uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 10_000);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await ctx.Channel.TriggerTypingAsync();
 | 
			
		||||
@@ -751,7 +751,7 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            await using var uow = _db.GetDbContext();
 | 
			
		||||
            cleanRichest = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
 | 
			
		||||
            cleanRichest = uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        await ctx.SendPaginatedConfirmAsync(page,
 | 
			
		||||
@@ -763,7 +763,7 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
			
		||||
                if (!opts.Clean)
 | 
			
		||||
                {
 | 
			
		||||
                    using var uow = _db.GetDbContext();
 | 
			
		||||
                    toSend = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, curPage);
 | 
			
		||||
                    toSend = uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, curPage);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ using NadekoBot.Db;
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
using NadekoBot.Modules.Gambling.Common;
 | 
			
		||||
using NadekoBot.Modules.Gambling.Common.Connect4;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Gambling.Services;
 | 
			
		||||
 | 
			
		||||
@@ -52,7 +53,7 @@ public class GamblingService : INService, IReadyExecutor
 | 
			
		||||
                var now = DateTime.UtcNow;
 | 
			
		||||
                var days = TimeSpan.FromDays(lifetime);
 | 
			
		||||
                await using var uow = _db.GetDbContext();
 | 
			
		||||
                await uow.CurrencyTransactions
 | 
			
		||||
                await uow.Set<CurrencyTransaction>()
 | 
			
		||||
                    .DeleteAsync(ct => ct.DateAdded == null || now - ct.DateAdded < days);
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
@@ -104,7 +105,7 @@ public class GamblingService : INService, IReadyExecutor
 | 
			
		||||
                    maxDecay = int.MaxValue;
 | 
			
		||||
 | 
			
		||||
                var decay = (double)config.Decay.Percent;
 | 
			
		||||
                await uow.DiscordUser
 | 
			
		||||
                await uow.Set<DiscordUser>()
 | 
			
		||||
                    .Where(x => x.CurrencyAmount > config.Decay.MinThreshold && x.UserId != _client.CurrentUser.Id)
 | 
			
		||||
                    .UpdateAsync(old => new()
 | 
			
		||||
                    {
 | 
			
		||||
@@ -135,11 +136,11 @@ public class GamblingService : INService, IReadyExecutor
 | 
			
		||||
            async () =>
 | 
			
		||||
            {
 | 
			
		||||
                await using var uow = _db.GetDbContext();
 | 
			
		||||
                var cash = uow.DiscordUser.GetTotalCurrency();
 | 
			
		||||
                var onePercent = uow.DiscordUser.GetTopOnePercentCurrency(_client.CurrentUser.Id);
 | 
			
		||||
                decimal planted = uow.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
 | 
			
		||||
                var waifus = uow.WaifuInfo.GetTotalValue();
 | 
			
		||||
                var bot = await uow.DiscordUser.GetUserCurrencyAsync(_client.CurrentUser.Id);
 | 
			
		||||
                var cash = uow.Set<DiscordUser>().GetTotalCurrency();
 | 
			
		||||
                var onePercent = uow.Set<DiscordUser>().GetTopOnePercentCurrency(_client.CurrentUser.Id);
 | 
			
		||||
                decimal planted = uow.Set<PlantedCurrency>().AsQueryable().Sum(x => x.Amount);
 | 
			
		||||
                var waifus = uow.Set<WaifuInfo>().GetTotalValue();
 | 
			
		||||
                var bot = await uow.Set<DiscordUser>().GetUserCurrencyAsync(_client.CurrentUser.Id);
 | 
			
		||||
                decimal bank = await uow.GetTable<BankUser>()
 | 
			
		||||
                    .SumAsyncLinqToDB(x => x.Balance);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -100,7 +100,7 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
    public IEnumerable<GuildConfigExtensions.GeneratingChannel> GetAllGeneratingChannels()
 | 
			
		||||
    {
 | 
			
		||||
        using var uow = _db.GetDbContext();
 | 
			
		||||
        var chs = uow.GuildConfigs.GetGeneratingChannels();
 | 
			
		||||
        var chs = uow.Set<GuildConfig>().GetGeneratingChannels();
 | 
			
		||||
        return chs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -259,7 +259,7 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
 | 
			
		||||
                pass = pass?.Trim().TrimTo(10, true).ToUpperInvariant();
 | 
			
		||||
                // gets all plants in this channel with the same password
 | 
			
		||||
                var entries = uow.PlantedCurrency.AsQueryable()
 | 
			
		||||
                var entries = uow.Set<PlantedCurrency>().AsQueryable()
 | 
			
		||||
                                 .Where(x => x.ChannelId == ch.Id && pass == x.Password)
 | 
			
		||||
                                 .ToList();
 | 
			
		||||
                // sum how much currency that is, and get all of the message ids (so that i can delete them)
 | 
			
		||||
@@ -371,7 +371,7 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
        string pass)
 | 
			
		||||
    {
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        uow.PlantedCurrency.Add(new()
 | 
			
		||||
        uow.Set<PlantedCurrency>().Add(new()
 | 
			
		||||
        {
 | 
			
		||||
            Amount = amount,
 | 
			
		||||
            GuildId = gid,
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ public class ShopService : IShopService, INService
 | 
			
		||||
    public ShopService(DbService db)
 | 
			
		||||
        => _db = db;
 | 
			
		||||
 | 
			
		||||
    private IndexedCollection<ShopEntry> GetEntriesInternal(NadekoContext uow, ulong guildId)
 | 
			
		||||
    private IndexedCollection<ShopEntry> GetEntriesInternal(DbContext uow, ulong guildId)
 | 
			
		||||
        => uow.GuildConfigsForId(guildId, set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items))
 | 
			
		||||
              .ShopEntries.ToIndexed();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -44,7 +44,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        var settings = _gss.Data;
 | 
			
		||||
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        var waifu = uow.WaifuInfo.ByWaifuUserId(waifuId);
 | 
			
		||||
        var waifu = uow.Set<WaifuInfo>().ByWaifuUserId(waifuId);
 | 
			
		||||
        var ownerUser = uow.GetOrCreateUser(owner);
 | 
			
		||||
 | 
			
		||||
        // owner has to be the owner of the waifu
 | 
			
		||||
@@ -85,14 +85,14 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
    {
 | 
			
		||||
        var settings = _gss.Data;
 | 
			
		||||
        using var uow = _db.GetDbContext();
 | 
			
		||||
        var waifu = uow.WaifuInfo.ByWaifuUserId(user.Id);
 | 
			
		||||
        var waifu = uow.Set<WaifuInfo>().ByWaifuUserId(user.Id);
 | 
			
		||||
 | 
			
		||||
        if (waifu is null)
 | 
			
		||||
            return settings.Waifu.MinPrice;
 | 
			
		||||
 | 
			
		||||
        var divorces = uow.WaifuUpdates.Count(x
 | 
			
		||||
        var divorces = uow.Set<WaifuUpdate>().Count(x
 | 
			
		||||
            => x.Old != null && x.Old.UserId == user.Id && x.UpdateType == WaifuUpdateType.Claimed && x.New == null);
 | 
			
		||||
        var affs = uow.WaifuUpdates.AsQueryable()
 | 
			
		||||
        var affs = uow.Set<WaifuUpdate>().AsQueryable()
 | 
			
		||||
                      .Where(w => w.User.UserId == user.Id
 | 
			
		||||
                                  && w.UpdateType == WaifuUpdateType.AffinityChanged
 | 
			
		||||
                                  && w.New != null)
 | 
			
		||||
@@ -111,22 +111,22 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        if (!await _cs.RemoveAsync(user.Id, price, new("waifu", "reset")))
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
        var affs = uow.WaifuUpdates.AsQueryable()
 | 
			
		||||
        var affs = uow.Set<WaifuUpdate>().AsQueryable()
 | 
			
		||||
                      .Where(w => w.User.UserId == user.Id
 | 
			
		||||
                                  && w.UpdateType == WaifuUpdateType.AffinityChanged
 | 
			
		||||
                                  && w.New != null);
 | 
			
		||||
 | 
			
		||||
        var divorces = uow.WaifuUpdates.AsQueryable()
 | 
			
		||||
        var divorces = uow.Set<WaifuUpdate>().AsQueryable()
 | 
			
		||||
                          .Where(x => x.Old != null
 | 
			
		||||
                                      && x.Old.UserId == user.Id
 | 
			
		||||
                                      && x.UpdateType == WaifuUpdateType.Claimed
 | 
			
		||||
                                      && x.New == null);
 | 
			
		||||
 | 
			
		||||
        //reset changes of heart to 0
 | 
			
		||||
        uow.WaifuUpdates.RemoveRange(affs);
 | 
			
		||||
        uow.Set<WaifuUpdate>().RemoveRange(affs);
 | 
			
		||||
        //reset divorces to 0
 | 
			
		||||
        uow.WaifuUpdates.RemoveRange(divorces);
 | 
			
		||||
        var waifu = uow.WaifuInfo.ByWaifuUserId(user.Id);
 | 
			
		||||
        uow.Set<WaifuUpdate>().RemoveRange(divorces);
 | 
			
		||||
        var waifu = uow.Set<WaifuInfo>().ByWaifuUserId(user.Id);
 | 
			
		||||
        //reset price, remove items
 | 
			
		||||
        //remove owner, remove affinity
 | 
			
		||||
        waifu.Price = 50;
 | 
			
		||||
@@ -149,7 +149,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        bool isAffinity;
 | 
			
		||||
        await using (var uow = _db.GetDbContext())
 | 
			
		||||
        {
 | 
			
		||||
            w = uow.WaifuInfo.ByWaifuUserId(target.Id);
 | 
			
		||||
            w = uow.Set<WaifuInfo>().ByWaifuUserId(target.Id);
 | 
			
		||||
            isAffinity = w?.Affinity?.UserId == user.Id;
 | 
			
		||||
            if (w is null)
 | 
			
		||||
            {
 | 
			
		||||
@@ -159,14 +159,14 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                    result = WaifuClaimResult.NotEnoughFunds;
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    uow.WaifuInfo.Add(w = new()
 | 
			
		||||
                    uow.Set<WaifuInfo>().Add(w = new()
 | 
			
		||||
                    {
 | 
			
		||||
                        Waifu = waifu,
 | 
			
		||||
                        Claimer = claimer,
 | 
			
		||||
                        Affinity = null,
 | 
			
		||||
                        Price = amount
 | 
			
		||||
                    });
 | 
			
		||||
                    uow.WaifuUpdates.Add(new()
 | 
			
		||||
                    uow.Set<WaifuUpdate>().Add(new()
 | 
			
		||||
                    {
 | 
			
		||||
                        User = waifu,
 | 
			
		||||
                        Old = null,
 | 
			
		||||
@@ -187,7 +187,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                    w.Price = amount + (amount / 4);
 | 
			
		||||
                    result = WaifuClaimResult.Success;
 | 
			
		||||
 | 
			
		||||
                    uow.WaifuUpdates.Add(new()
 | 
			
		||||
                    uow.Set<WaifuUpdate>().Add(new()
 | 
			
		||||
                    {
 | 
			
		||||
                        User = w.Waifu,
 | 
			
		||||
                        Old = oldClaimer,
 | 
			
		||||
@@ -207,7 +207,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                    w.Price = amount;
 | 
			
		||||
                    result = WaifuClaimResult.Success;
 | 
			
		||||
 | 
			
		||||
                    uow.WaifuUpdates.Add(new()
 | 
			
		||||
                    uow.Set<WaifuUpdate>().Add(new()
 | 
			
		||||
                    {
 | 
			
		||||
                        User = w.Waifu,
 | 
			
		||||
                        Old = oldClaimer,
 | 
			
		||||
@@ -233,7 +233,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        TimeSpan? remaining = null;
 | 
			
		||||
        await using (var uow = _db.GetDbContext())
 | 
			
		||||
        {
 | 
			
		||||
            var w = uow.WaifuInfo.ByWaifuUserId(user.Id);
 | 
			
		||||
            var w = uow.Set<WaifuInfo>().ByWaifuUserId(user.Id);
 | 
			
		||||
            var newAff = target is null ? null : uow.GetOrCreateUser(target);
 | 
			
		||||
            if (w?.Affinity?.UserId == target?.Id)
 | 
			
		||||
            {
 | 
			
		||||
@@ -249,7 +249,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
            else if (w is null)
 | 
			
		||||
            {
 | 
			
		||||
                var thisUser = uow.GetOrCreateUser(user);
 | 
			
		||||
                uow.WaifuInfo.Add(new()
 | 
			
		||||
                uow.Set<WaifuInfo>().Add(new()
 | 
			
		||||
                {
 | 
			
		||||
                    Affinity = newAff,
 | 
			
		||||
                    Waifu = thisUser,
 | 
			
		||||
@@ -258,7 +258,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                });
 | 
			
		||||
                success = true;
 | 
			
		||||
 | 
			
		||||
                uow.WaifuUpdates.Add(new()
 | 
			
		||||
                uow.Set<WaifuUpdate>().Add(new()
 | 
			
		||||
                {
 | 
			
		||||
                    User = thisUser,
 | 
			
		||||
                    Old = null,
 | 
			
		||||
@@ -273,7 +273,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                w.Affinity = newAff;
 | 
			
		||||
                success = true;
 | 
			
		||||
 | 
			
		||||
                uow.WaifuUpdates.Add(new()
 | 
			
		||||
                uow.Set<WaifuUpdate>().Add(new()
 | 
			
		||||
                {
 | 
			
		||||
                    User = w.Waifu,
 | 
			
		||||
                    Old = oldAff,
 | 
			
		||||
@@ -291,13 +291,13 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
    public IEnumerable<WaifuLbResult> GetTopWaifusAtPage(int page)
 | 
			
		||||
    {
 | 
			
		||||
        using var uow = _db.GetDbContext();
 | 
			
		||||
        return uow.WaifuInfo.GetTop(9, page * 9);
 | 
			
		||||
        return uow.Set<WaifuInfo>().GetTop(9, page * 9);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ulong GetWaifuUserId(ulong ownerId, string name)
 | 
			
		||||
    {
 | 
			
		||||
        using var uow = _db.GetDbContext();
 | 
			
		||||
        return uow.WaifuInfo.GetWaifuUserId(ownerId, name);
 | 
			
		||||
        return uow.Set<WaifuInfo>().GetWaifuUserId(ownerId, name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static TypedKey<long> GetDivorceKey(ulong userId)
 | 
			
		||||
@@ -314,7 +314,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
        WaifuInfo w;
 | 
			
		||||
        await using (var uow = _db.GetDbContext())
 | 
			
		||||
        {
 | 
			
		||||
            w = uow.WaifuInfo.ByWaifuUserId(targetId);
 | 
			
		||||
            w = uow.Set<WaifuInfo>().ByWaifuUserId(targetId);
 | 
			
		||||
            if (w?.Claimer is null || w.Claimer.UserId != user.Id)
 | 
			
		||||
                result = DivorceResult.NotYourWife;
 | 
			
		||||
            else
 | 
			
		||||
@@ -344,7 +344,7 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
                var oldClaimer = w.Claimer;
 | 
			
		||||
                w.Claimer = null;
 | 
			
		||||
 | 
			
		||||
                uow.WaifuUpdates.Add(new()
 | 
			
		||||
                uow.Set<WaifuUpdate>().Add(new()
 | 
			
		||||
                {
 | 
			
		||||
                    User = w.Waifu,
 | 
			
		||||
                    Old = oldClaimer,
 | 
			
		||||
@@ -365,10 +365,10 @@ public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        var w = uow.WaifuInfo.ByWaifuUserId(giftedWaifu.Id, set => set.Include(x => x.Items).Include(x => x.Claimer));
 | 
			
		||||
        var w = uow.Set<WaifuInfo>().ByWaifuUserId(giftedWaifu.Id, set => set.Include(x => x.Items).Include(x => x.Claimer));
 | 
			
		||||
        if (w is null)
 | 
			
		||||
        {
 | 
			
		||||
            uow.WaifuInfo.Add(w = new()
 | 
			
		||||
            uow.Set<WaifuInfo>().Add(w = new()
 | 
			
		||||
            {
 | 
			
		||||
                Affinity = null,
 | 
			
		||||
                Claimer = null,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								src/Nadeko.Bot.Modules.Gambling/Gambling/Waifus/db/Waifu.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/Nadeko.Bot.Modules.Gambling/Gambling/Waifus/db/Waifu.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
public class WaifuInfo : DbEntity
 | 
			
		||||
{
 | 
			
		||||
    public int WaifuId { get; set; }
 | 
			
		||||
    public DiscordUser Waifu { get; set; }
 | 
			
		||||
    
 | 
			
		||||
    public int? ClaimerId { get; set; }
 | 
			
		||||
    public DiscordUser Claimer { get; set; }
 | 
			
		||||
 | 
			
		||||
    public int? AffinityId { get; set; }
 | 
			
		||||
    public DiscordUser Affinity { get; set; }
 | 
			
		||||
 | 
			
		||||
    public long Price { get; set; }
 | 
			
		||||
    public List<WaifuItem> Items { get; set; } = new();
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,133 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
using LinqToDB;
 | 
			
		||||
using LinqToDB.EntityFrameworkCore;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
using NadekoBot.Services.Database;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Db;
 | 
			
		||||
 | 
			
		||||
public static class WaifuExtensions
 | 
			
		||||
{
 | 
			
		||||
    public static WaifuInfo ByWaifuUserId(
 | 
			
		||||
        this DbSet<WaifuInfo> waifus,
 | 
			
		||||
        ulong userId,
 | 
			
		||||
        Func<DbSet<WaifuInfo>, IQueryable<WaifuInfo>> includes = null)
 | 
			
		||||
    {
 | 
			
		||||
        if (includes is null)
 | 
			
		||||
        {
 | 
			
		||||
            return waifus.Include(wi => wi.Waifu)
 | 
			
		||||
                         .Include(wi => wi.Affinity)
 | 
			
		||||
                         .Include(wi => wi.Claimer)
 | 
			
		||||
                         .Include(wi => wi.Items)
 | 
			
		||||
                         .FirstOrDefault(wi => wi.Waifu.UserId == userId);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return includes(waifus).AsQueryable().FirstOrDefault(wi => wi.Waifu.UserId == userId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static IEnumerable<WaifuLbResult> GetTop(this DbSet<WaifuInfo> waifus, int count, int skip = 0)
 | 
			
		||||
    {
 | 
			
		||||
        if (count < 0)
 | 
			
		||||
            throw new ArgumentOutOfRangeException(nameof(count));
 | 
			
		||||
        if (count == 0)
 | 
			
		||||
            return new List<WaifuLbResult>();
 | 
			
		||||
 | 
			
		||||
        return waifus.Include(wi => wi.Waifu)
 | 
			
		||||
                     .Include(wi => wi.Affinity)
 | 
			
		||||
                     .Include(wi => wi.Claimer)
 | 
			
		||||
                     .OrderByDescending(wi => wi.Price)
 | 
			
		||||
                     .Skip(skip)
 | 
			
		||||
                     .Take(count)
 | 
			
		||||
                     .Select(x => new WaifuLbResult
 | 
			
		||||
                     {
 | 
			
		||||
                         Affinity = x.Affinity == null ? null : x.Affinity.Username,
 | 
			
		||||
                         AffinityDiscrim = x.Affinity == null ? null : x.Affinity.Discriminator,
 | 
			
		||||
                         Claimer = x.Claimer == null ? null : x.Claimer.Username,
 | 
			
		||||
                         ClaimerDiscrim = x.Claimer == null ? null : x.Claimer.Discriminator,
 | 
			
		||||
                         Username = x.Waifu.Username,
 | 
			
		||||
                         Discrim = x.Waifu.Discriminator,
 | 
			
		||||
                         Price = x.Price
 | 
			
		||||
                     })
 | 
			
		||||
                     .ToList();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static decimal GetTotalValue(this DbSet<WaifuInfo> waifus)
 | 
			
		||||
        => waifus.AsQueryable().Where(x => x.ClaimerId != null).Sum(x => x.Price);
 | 
			
		||||
 | 
			
		||||
    public static ulong GetWaifuUserId(this DbSet<WaifuInfo> waifus, ulong ownerId, string name)
 | 
			
		||||
        => waifus.AsQueryable()
 | 
			
		||||
                 .AsNoTracking()
 | 
			
		||||
                 .Where(x => x.Claimer.UserId == ownerId && x.Waifu.Username + "#" + x.Waifu.Discriminator == name)
 | 
			
		||||
                 .Select(x => x.Waifu.UserId)
 | 
			
		||||
                 .FirstOrDefault();
 | 
			
		||||
 | 
			
		||||
    public static async Task<WaifuInfoStats> GetWaifuInfoAsync(this DbContext ctx, ulong userId)
 | 
			
		||||
    {
 | 
			
		||||
        await ctx.Set<WaifuInfo>()
 | 
			
		||||
                 .ToLinqToDBTable()
 | 
			
		||||
                 .InsertOrUpdateAsync(() => new()
 | 
			
		||||
                     {
 | 
			
		||||
                         AffinityId = null,
 | 
			
		||||
                         ClaimerId = null,
 | 
			
		||||
                         Price = 1,
 | 
			
		||||
                         WaifuId = ctx.Set<DiscordUser>().Where(x => x.UserId == userId).Select(x => x.Id).First()
 | 
			
		||||
                     },
 | 
			
		||||
                     _ => new(),
 | 
			
		||||
                     () => new()
 | 
			
		||||
                     {
 | 
			
		||||
                         WaifuId = ctx.Set<DiscordUser>().Where(x => x.UserId == userId).Select(x => x.Id).First()
 | 
			
		||||
                     });
 | 
			
		||||
 | 
			
		||||
        var toReturn = ctx.Set<WaifuInfo>().AsQueryable()
 | 
			
		||||
                          .Where(w => w.WaifuId
 | 
			
		||||
                                      == ctx.Set<DiscordUser>()
 | 
			
		||||
                                            .AsQueryable()
 | 
			
		||||
                                            .Where(u => u.UserId == userId)
 | 
			
		||||
                                            .Select(u => u.Id)
 | 
			
		||||
                                            .FirstOrDefault())
 | 
			
		||||
                          .Select(w => new WaifuInfoStats
 | 
			
		||||
                          {
 | 
			
		||||
                              WaifuId = w.WaifuId,
 | 
			
		||||
                              FullName =
 | 
			
		||||
                                  ctx.Set<DiscordUser>()
 | 
			
		||||
                                     .AsQueryable()
 | 
			
		||||
                                     .Where(u => u.UserId == userId)
 | 
			
		||||
                                     .Select(u => u.Username + "#" + u.Discriminator)
 | 
			
		||||
                                     .FirstOrDefault(),
 | 
			
		||||
                              AffinityCount =
 | 
			
		||||
                                  ctx.Set<WaifuUpdate>()
 | 
			
		||||
                                     .AsQueryable()
 | 
			
		||||
                                     .Count(x => x.UserId == w.WaifuId
 | 
			
		||||
                                                 && x.UpdateType == WaifuUpdateType.AffinityChanged
 | 
			
		||||
                                                 && x.NewId != null),
 | 
			
		||||
                              AffinityName =
 | 
			
		||||
                                  ctx.Set<DiscordUser>()
 | 
			
		||||
                                     .AsQueryable()
 | 
			
		||||
                                     .Where(u => u.Id == w.AffinityId)
 | 
			
		||||
                                     .Select(u => u.Username + "#" + u.Discriminator)
 | 
			
		||||
                                     .FirstOrDefault(),
 | 
			
		||||
                              ClaimCount = ctx.Set<WaifuInfo>().AsQueryable().Count(x => x.ClaimerId == w.WaifuId),
 | 
			
		||||
                              ClaimerName =
 | 
			
		||||
                                  ctx.Set<DiscordUser>()
 | 
			
		||||
                                     .AsQueryable()
 | 
			
		||||
                                     .Where(u => u.Id == w.ClaimerId)
 | 
			
		||||
                                     .Select(u => u.Username + "#" + u.Discriminator)
 | 
			
		||||
                                     .FirstOrDefault(),
 | 
			
		||||
                              DivorceCount =
 | 
			
		||||
                                  ctx.Set<WaifuUpdate>()
 | 
			
		||||
                                     .AsQueryable()
 | 
			
		||||
                                     .Count(x => x.OldId == w.WaifuId
 | 
			
		||||
                                                 && x.NewId == null
 | 
			
		||||
                                                 && x.UpdateType == WaifuUpdateType.Claimed),
 | 
			
		||||
                              Price = w.Price,
 | 
			
		||||
                          })
 | 
			
		||||
                          .FirstOrDefault();
 | 
			
		||||
 | 
			
		||||
        if (toReturn is null)
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        return toReturn;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,14 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
namespace NadekoBot.Db;
 | 
			
		||||
 | 
			
		||||
public class WaifuInfoStats
 | 
			
		||||
{
 | 
			
		||||
    public int WaifuId { get; init; }
 | 
			
		||||
    public string FullName { get; init; }
 | 
			
		||||
    public long Price { get; init; }
 | 
			
		||||
    public string ClaimerName { get; init; }
 | 
			
		||||
    public string AffinityName { get; init; }
 | 
			
		||||
    public int AffinityCount { get; init; }
 | 
			
		||||
    public int DivorceCount { get; init; }
 | 
			
		||||
    public int ClaimCount { get; init; }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,10 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
namespace NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
public class WaifuItem : DbEntity
 | 
			
		||||
{
 | 
			
		||||
    public WaifuInfo WaifuInfo { get; set; }
 | 
			
		||||
    public int? WaifuInfoId { get; set; }
 | 
			
		||||
    public string ItemEmoji { get; set; }
 | 
			
		||||
    public string Name { get; set; }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,16 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
namespace NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
public class WaifuLbResult
 | 
			
		||||
{
 | 
			
		||||
    public string Username { get; set; }
 | 
			
		||||
    public string Discrim { get; set; }
 | 
			
		||||
 | 
			
		||||
    public string Claimer { get; set; }
 | 
			
		||||
    public string ClaimerDiscrim { get; set; }
 | 
			
		||||
 | 
			
		||||
    public string Affinity { get; set; }
 | 
			
		||||
    public string AffinityDiscrim { get; set; }
 | 
			
		||||
 | 
			
		||||
    public long Price { get; set; }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
public class WaifuUpdate : DbEntity
 | 
			
		||||
{
 | 
			
		||||
    public int UserId { get; set; }
 | 
			
		||||
    public DiscordUser User { get; set; }
 | 
			
		||||
    public WaifuUpdateType UpdateType { get; set; }
 | 
			
		||||
 | 
			
		||||
    public int? OldId { get; set; }
 | 
			
		||||
    public DiscordUser Old { get; set; }
 | 
			
		||||
 | 
			
		||||
    public int? NewId { get; set; }
 | 
			
		||||
    public DiscordUser New { get; set; }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,8 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
namespace NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
public enum WaifuUpdateType
 | 
			
		||||
{
 | 
			
		||||
    AffinityChanged,
 | 
			
		||||
    Claimed
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,67 @@
 | 
			
		||||
using LinqToDB;
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace Nadeko.Bot.Modules.Gambling.Gambling._Common;
 | 
			
		||||
 | 
			
		||||
public interface IGamblingCleanupService
 | 
			
		||||
{
 | 
			
		||||
    Task DeleteWaifus();
 | 
			
		||||
    Task DeleteWaifu(ulong userId);
 | 
			
		||||
    Task DeleteCurrency();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public class GamblingCleanupService : IGamblingCleanupService
 | 
			
		||||
{
 | 
			
		||||
    private readonly DbService _db;
 | 
			
		||||
 | 
			
		||||
    public GamblingCleanupService(DbService db)
 | 
			
		||||
    {
 | 
			
		||||
        _db = db;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public async Task DeleteWaifus()
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        await ctx.Set<WaifuInfo>().DeleteAsync();
 | 
			
		||||
        await ctx.Set<WaifuItem>().DeleteAsync();
 | 
			
		||||
        await ctx.Set<WaifuUpdate>().DeleteAsync();
 | 
			
		||||
        await ctx.SaveChangesAsync();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task DeleteWaifu(ulong userId)
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        await ctx.Set<WaifuUpdate>()
 | 
			
		||||
            .Where(x => x.User.UserId == userId)
 | 
			
		||||
            .DeleteAsync();
 | 
			
		||||
        await ctx.Set<WaifuItem>()
 | 
			
		||||
            .Where(x => x.WaifuInfo.Waifu.UserId == userId)
 | 
			
		||||
            .DeleteAsync();
 | 
			
		||||
        await ctx.Set<WaifuInfo>()
 | 
			
		||||
            .Where(x => x.Claimer.UserId == userId)
 | 
			
		||||
            .UpdateAsync(old => new WaifuInfo()
 | 
			
		||||
            {
 | 
			
		||||
                ClaimerId = null,
 | 
			
		||||
            });
 | 
			
		||||
        await ctx.Set<WaifuInfo>()
 | 
			
		||||
            .Where(x => x.Waifu.UserId == userId)
 | 
			
		||||
            .DeleteAsync();
 | 
			
		||||
        await ctx.SaveChangesAsync();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public async Task DeleteCurrency()
 | 
			
		||||
    {
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        await uow.Set<DiscordUser>().UpdateAsync(_ => new DiscordUser()
 | 
			
		||||
        {
 | 
			
		||||
            CurrencyAmount = 0
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        await uow.Set<CurrencyTransaction>().DeleteAsync();
 | 
			
		||||
        await uow.Set<PlantedCurrency>().DeleteAsync();
 | 
			
		||||
        await uow.Set<BankUser>().DeleteAsync();
 | 
			
		||||
        await uow.SaveChangesAsync();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
using NadekoBot.Db;
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
using NadekoBot.Modules.Gambling.Services;
 | 
			
		||||
using NCalc;
 | 
			
		||||
using OneOf;
 | 
			
		||||
@@ -69,7 +70,7 @@ public class BaseShmartInputAmountReader
 | 
			
		||||
    protected virtual async Task<long> Cur(ICommandContext ctx)
 | 
			
		||||
    {
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        return await uow.DiscordUser.GetUserCurrencyAsync(ctx.User.Id);
 | 
			
		||||
        return await uow.Set<DiscordUser>().GetUserCurrencyAsync(ctx.User.Id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual async Task<long> Max(ICommandContext ctx)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
using NadekoBot.Db;
 | 
			
		||||
using NadekoBot.Modules.Games.Services;
 | 
			
		||||
using NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Games;
 | 
			
		||||
 | 
			
		||||
@@ -25,7 +26,7 @@ public partial class Games
 | 
			
		||||
            {
 | 
			
		||||
                await using (var uow = _db.GetDbContext())
 | 
			
		||||
                {
 | 
			
		||||
                    uow.GuildConfigs.SetCleverbotEnabled(ctx.Guild.Id, false);
 | 
			
		||||
                    uow.Set<GuildConfig>().SetCleverbotEnabled(ctx.Guild.Id, false);
 | 
			
		||||
                    await uow.SaveChangesAsync();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@@ -37,7 +38,7 @@ public partial class Games
 | 
			
		||||
 | 
			
		||||
            await using (var uow = _db.GetDbContext())
 | 
			
		||||
            {
 | 
			
		||||
                uow.GuildConfigs.SetCleverbotEnabled(ctx.Guild.Id, true);
 | 
			
		||||
                uow.Set<GuildConfig>().SetCleverbotEnabled(ctx.Guild.Id, true);
 | 
			
		||||
                await uow.SaveChangesAsync();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,4 +18,5 @@
 | 
			
		||||
        <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0"/>
 | 
			
		||||
    </ItemGroup>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</Project>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user