From e1271336123acb21c7a9849f13979da1fcb87932 Mon Sep 17 00:00:00 2001 From: Toastie Date: Thu, 28 Nov 2024 19:38:28 +1300 Subject: [PATCH] .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 | 2 +- src/EllieBot/Modules/Gambling/Gambling.cs | 6 ++ .../PlantPick/PlantAndPickCommands.cs | 2 +- .../Gambling/PlantPick/PlantPickService.cs | 55 +++++++++++++------ 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f2ad9b..ac7a146 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ Mostly based on [keepachangelog](https://keepachangelog.com/en/1.1.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/EllieBot/Modules/Gambling/Gambling.cs b/src/EllieBot/Modules/Gambling/Gambling.cs index 249060f..c4df107 100644 --- a/src/EllieBot/Modules/Gambling/Gambling.cs +++ b/src/EllieBot/Modules/Gambling/Gambling.cs @@ -391,6 +391,12 @@ public partial class Gambling : GamblingModule 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] [Priority(1)] diff --git a/src/EllieBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs b/src/EllieBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs index 558885b..909de44 100644 --- a/src/EllieBot/Modules/Gambling/PlantPick/PlantAndPickCommands.cs +++ b/src/EllieBot/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/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs index 5a732a5..27c59d8 100644 --- a/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs +++ b/src/EllieBot/Modules/Gambling/PlantPick/PlantPickService.cs @@ -241,12 +241,18 @@ public class PlantPickService : IEService, 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 : IEService, IExecNoCommand public async Task PlantAsync( ulong gid, - IMessageChannel ch, + ITextChannel ch, ulong uid, string user, long amount, @@ -361,6 +367,7 @@ public class PlantPickService : IEService, 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 : IEService, 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