diff --git a/PluralKit.Bot/Commands/SwitchCommands.cs b/PluralKit.Bot/Commands/SwitchCommands.cs index 37adfeb0..aaa55d8a 100644 --- a/PluralKit.Bot/Commands/SwitchCommands.cs +++ b/PluralKit.Bot/Commands/SwitchCommands.cs @@ -76,17 +76,52 @@ namespace PluralKit.Bot.Commands // Now we can actually do the move, yay! // But, we do a prompt to confirm. var lastSwitchMembers = await Switches.GetSwitchMembers(lastTwoSwitches[0]); + var lastSwitchMemberStr = string.Join(", ", lastSwitchMembers.Select(m => m.Name)); + var lastSwitchTimeStr = lastTwoSwitches[0].Timestamp.ToString(Formats.DateTimeFormat, null); + var lastSwitchDeltaStr = SystemClock.Instance.GetCurrentInstant().Minus(lastTwoSwitches[0].Timestamp).ToString(Formats.DurationFormat, null); + var newSwitchTimeStr = time.ToString(Formats.DateTimeFormat, null); + var newSwitchDeltaStr = SystemClock.Instance.GetCurrentInstant().Minus(time.ToInstant()).ToString(Formats.DurationFormat, null); // yeet - var msg = await Context.Channel.SendMessageAsync($"{Emojis.Warn} This will move the latest switch ({string.Join(", ", lastSwitchMembers.Select(m => m.Name))}) from {lastTwoSwitches[0].Timestamp.ToString(Formats.DateTimeFormat, null)} ({SystemClock.Instance.GetCurrentInstant().Minus(lastTwoSwitches[0].Timestamp).ToString(Formats.DurationFormat, null)} ago) to {time.ToString(Formats.DateTimeFormat, null)} ({SystemClock.Instance.GetCurrentInstant().Minus(time.ToInstant()).ToString(Formats.DurationFormat, null)} ago). Is this OK?"); - if (!await Context.PromptYesNo(msg)) - { - throw Errors.SwitchMoveCancelled; - } + var msg = await Context.Channel.SendMessageAsync($"{Emojis.Warn} This will move the latest switch ({lastSwitchMemberStr}) from {lastSwitchTimeStr} ({lastSwitchDeltaStr} ago) to {newSwitchTimeStr} ({newSwitchDeltaStr} ago). Is this OK?"); + if (!await Context.PromptYesNo(msg)) throw Errors.SwitchMoveCancelled; // aaaand *now* we do the move await Switches.MoveSwitch(lastTwoSwitches[0], time.ToInstant()); await Context.Channel.SendMessageAsync($"{Emojis.Success} Switch moved."); } + + [Command("delete")] + [MustHaveSystem] + public async Task SwitchDelete() + { + // Fetch the last two switches for the system to do bounds checking on + var lastTwoSwitches = (await Switches.GetSwitches(Context.SenderSystem, 2)).ToArray(); + if (lastTwoSwitches.Length == 0) throw Errors.NoRegisteredSwitches; + + var lastSwitchMembers = await Switches.GetSwitchMembers(lastTwoSwitches[0]); + var lastSwitchMemberStr = string.Join(", ", lastSwitchMembers.Select(m => m.Name)); + var lastSwitchDeltaStr = SystemClock.Instance.GetCurrentInstant().Minus(lastTwoSwitches[0].Timestamp).ToString(Formats.DurationFormat, null); + + IUserMessage msg; + if (lastTwoSwitches.Length == 1) + { + msg = await Context.Channel.SendMessageAsync( + $"{Emojis.Warn} This will delete the latest switch ({lastSwitchMemberStr}, {lastSwitchDeltaStr} ago). You have no other switches logged. Is this okay?"); + } + else + { + var secondSwitchMembers = await Switches.GetSwitchMembers(lastTwoSwitches[1]); + var secondSwitchMemberStr = string.Join(", ", secondSwitchMembers.Select(m => m.Name)); + var secondSwitchDeltaStr = SystemClock.Instance.GetCurrentInstant().Minus(lastTwoSwitches[1].Timestamp).ToString(Formats.DurationFormat, null); + msg = await Context.Channel.SendMessageAsync( + $"{Emojis.Warn} This will delete the latest switch ({lastSwitchMemberStr}, {lastSwitchDeltaStr} ago). The next latest switch is {secondSwitchMemberStr} ({secondSwitchDeltaStr} ago). Is this okay?"); + } + + if (!await Context.PromptYesNo(msg)) throw Errors.SwitchDeleteCancelled; + await Switches.DeleteSwitch(lastTwoSwitches[0]); + + await Context.Channel.SendMessageAsync($"{Emojis.Success} Switch deleted."); + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index 5ebf48c4..26d3f0f8 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -55,5 +55,6 @@ namespace PluralKit.Bot { public static PKError SwitchMoveBeforeSecondLast(ZonedDateTime time) => new PKError($"Can't move switch to before last switch time ({time.ToString(Formats.DateTimeFormat, null)}), as it would cause conflicts."); public static PKError SwitchMoveCancelled => new PKError("Switch move cancelled."); + public static PKError SwitchDeleteCancelled => new PKError("Switch deletion cancelled."); } } \ No newline at end of file diff --git a/PluralKit.Bot/Utils.cs b/PluralKit.Bot/Utils.cs index 3fea795c..2cd90caf 100644 --- a/PluralKit.Bot/Utils.cs +++ b/PluralKit.Bot/Utils.cs @@ -123,7 +123,7 @@ namespace PluralKit.Bot } /// Subclass of ICommandContext with PK-specific additional fields and functionality - public class PKCommandContext : SocketCommandContext, ICommandContext + public class PKCommandContext : SocketCommandContext { public IDbConnection Connection { get; } public PKSystem SenderSystem { get; } diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 334667a1..98a787a2 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -203,5 +203,10 @@ namespace PluralKit { await _connection.ExecuteAsync("update switches set timestamp = @Time where id = @Id", new {Time = time, Id = sw.Id}); } + + public async Task DeleteSwitch(PKSwitch sw) + { + await _connection.ExecuteAsync("delete from switches where id = @Id", new {Id = sw.Id}); + } } } \ No newline at end of file