From 0451551ddf1a1c71a3aba9b055eef336c5708692 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 26 Nov 2024 17:02:09 +0000 Subject: [PATCH] change: .gc will now remove previous plants with the same pw length from the channel and add them to itself. This is to avoid missed gcs. The amount text will be wrong however, as it will only show how much flowers spawned now. The user will get full amount of all gcs previously --- CHANGELOG.md | 4 +- src/NadekoBot/Modules/Gambling/Gambling.cs | 6 ++ .../PlantPick/PlantAndPickCommands.cs | 2 +- .../Gambling/PlantPick/PlantPickService.cs | 55 +++++++++++++------ 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b9667c26..ff3998e02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o -## [5.2.0] - 20.11.2024 +## [5.2.0] - 26.11.2024 ### Added @@ -13,7 +13,7 @@ Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except da - `.btr rm` to remove a button role from the specified message - `.btr rma` to remove all button roles on the specified message - `.btr excl` to toggle exclusive button roles (only 1 role per message or any number) - - Use `.h` on any of the above for more info + - Use `.h btr` for more info - Added `.wrongsong` which will delete the last queued song. - Useful in case you made a mistake, or the bot queued a wrong song - It will reset after a shuffle or fairplay toggle, or similar events. diff --git a/src/NadekoBot/Modules/Gambling/Gambling.cs b/src/NadekoBot/Modules/Gambling/Gambling.cs index 6514d2e9b..ffc44d67d 100644 --- a/src/NadekoBot/Modules/Gambling/Gambling.cs +++ b/src/NadekoBot/Modules/Gambling/Gambling.cs @@ -390,6 +390,12 @@ public partial class Gambling : GamblingModule [Priority(0)] public Task CurrencyTransactions([Leftover] IUser usr) => InternalCurrencyTransactions(usr.Id, 1); + + [Cmd] + [OwnerOnly] + [Priority(-1)] + public Task CurrencyTransactions([Leftover] ulong userId) + => InternalCurrencyTransactions(userId, 1); [Cmd] [OwnerOnly] diff --git a/src/NadekoBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs b/src/NadekoBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs index 7a7d1c458..9ded7792b 100644 --- a/src/NadekoBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs +++ b/src/NadekoBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs @@ -59,7 +59,7 @@ public partial class Gambling } var success = await _service.PlantAsync(ctx.Guild.Id, - ctx.Channel, + (ITextChannel)ctx.Channel, ctx.User.Id, ctx.User.ToString(), amount, diff --git a/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs b/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs index f4991d783..6870b9ce1 100644 --- a/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs +++ b/src/NadekoBot/Modules/Gambling/PlantPick/PlantPickService.cs @@ -241,12 +241,18 @@ public class PlantPickService : INService, IExecNoCommand await using (stream) sent = await channel.SendFileAsync(stream, $"currency_image.{ext}", toSend); - await AddPlantToDatabase(channel.GuildId, + var res = await AddPlantToDatabase(channel.GuildId, channel.Id, _client.CurrentUser.Id, sent.Id, dropAmount, - pw); + pw, + true); + + if (res.toDelete.Length > 0) + { + await channel.DeleteMessagesAsync(res.toDelete); + } } } } @@ -335,7 +341,7 @@ public class PlantPickService : INService, IExecNoCommand public async Task PlantAsync( ulong gid, - IMessageChannel ch, + ITextChannel ch, ulong uid, string user, long amount, @@ -361,6 +367,7 @@ public class PlantPickService : INService, IExecNoCommand // if it doesn't fail, put the plant in the database for other people to pick await AddPlantToDatabase(gid, ch.Id, uid, msgId.Value, amount, pass); + return true; } @@ -368,25 +375,41 @@ public class PlantPickService : INService, IExecNoCommand return false; } - private async Task AddPlantToDatabase( + private async Task<(long totalAmount, ulong[] toDelete)> AddPlantToDatabase( ulong gid, ulong cid, ulong uid, ulong mid, long amount, - string pass) + string pass, + bool auto = false) { await using var uow = _db.GetDbContext(); - uow.Set() - .Add(new() - { - Amount = amount, - GuildId = gid, - ChannelId = cid, - Password = pass, - UserId = uid, - MessageId = mid - }); - await uow.SaveChangesAsync(); + + PlantedCurrency[] deleted = []; + if (!string.IsNullOrWhiteSpace(pass) && auto) + { + deleted = await uow.GetTable() + .Where(x => x.GuildId == gid + && x.ChannelId == cid + && x.Password != null + && x.Password.Length == pass.Length) + .DeleteWithOutputAsync(); + } + + var totalDeletedAmount = deleted.Length == 0 ? 0 : deleted.Sum(x => x.Amount); + + await uow.GetTable() + .InsertAsync(() => new() + { + Amount = totalDeletedAmount + amount, + GuildId = gid, + ChannelId = cid, + Password = pass, + UserId = uid, + MessageId = mid, + }); + + return (totalDeletedAmount + amount, deleted.Select(x => x.MessageId).ToArray()); } } \ No newline at end of file