public async Task SwitchDelete(Context ctx) { ctx.CheckSystem(); if (ctx.Match("all", "clear") || ctx.MatchFlag("all", "clear")) { // Subcommand: "delete all" var purgeMsg = $"{Emojis.Warn} This will delete *all registered switches* in your system. Are you sure you want to proceed?"; if (!await ctx.PromptYesNo(purgeMsg, "Clear Switches")) { throw Errors.GenericCancelled(); } await _db.Execute(c => _repo.DeleteAllSwitches(c, ctx.System.Id)); await ctx.Reply($"{Emojis.Success} Cleared system switches!"); return; } await using var conn = await _db.Obtain(); // Fetch the last two switches for the system to do bounds checking on var lastTwoSwitches = await _repo.GetSwitches(conn, ctx.System.Id).Take(2).ToListAsync(); if (lastTwoSwitches.Count == 0) { throw Errors.NoRegisteredSwitches; } var lastSwitchMembers = _repo.GetSwitchMembers(conn, lastTwoSwitches[0].Id); var lastSwitchMemberStr = string.Join(", ", await lastSwitchMembers.Select(m => m.NameFor(ctx)).ToListAsync()); var lastSwitchDeltaStr = (SystemClock.Instance.GetCurrentInstant() - lastTwoSwitches[0].Timestamp).FormatDuration(); string msg; if (lastTwoSwitches.Count == 1) { msg = $"{Emojis.Warn} This will delete the latest switch ({lastSwitchMemberStr}, {lastSwitchDeltaStr} ago). You have no other switches logged. Is this okay?"; } else { var secondSwitchMembers = _repo.GetSwitchMembers(conn, lastTwoSwitches[1].Id); var secondSwitchMemberStr = string.Join(", ", await secondSwitchMembers.Select(m => m.NameFor(ctx)).ToListAsync()); var secondSwitchDeltaStr = (SystemClock.Instance.GetCurrentInstant() - lastTwoSwitches[1].Timestamp).FormatDuration(); msg = $"{Emojis.Warn} This will delete the latest switch ({lastSwitchMemberStr}, {lastSwitchDeltaStr} ago). The next latest switch is {secondSwitchMemberStr} ({secondSwitchDeltaStr} ago). Is this okay?"; } if (!await ctx.PromptYesNo(msg, "Delete Switch")) { throw Errors.SwitchDeleteCancelled; } await _repo.DeleteSwitch(conn, lastTwoSwitches[0].Id); await ctx.Reply($"{Emojis.Success} Switch deleted."); }