|
| 1 | +using Discord; |
1 | 2 | using Discord.Interactions; |
2 | 3 | using Microsoft.Extensions.DependencyInjection; |
| 4 | +using RustPlusBot.Features.Pairing.Accounts; |
3 | 5 | using RustPlusBot.Features.Pairing.Listening; |
4 | 6 | using RustPlusBot.Features.Pairing.Supervisor; |
5 | 7 | using RustPlusBot.Features.Pairing.Validation; |
@@ -68,4 +70,71 @@ await FollowupAsync( |
68 | 70 | await FollowupAsync(message, ephemeral: true).ConfigureAwait(false); |
69 | 71 | } |
70 | 72 | } |
| 73 | + |
| 74 | + private const string DisconnectConfirmId = "pairing:account:disconnect:confirm"; |
| 75 | + private const string DisconnectCancelId = "pairing:account:disconnect:cancel"; |
| 76 | + |
| 77 | + /// <summary>Opens an ephemeral confirmation listing the servers a disconnect would affect.</summary> |
| 78 | + [ComponentInteraction(WorkspaceComponentIds.DisconnectAccount)] |
| 79 | + public async Task DisconnectPromptAsync() |
| 80 | + { |
| 81 | + if (Context.Guild is null) |
| 82 | + { |
| 83 | + await RespondAsync("This control must be used in a server.", ephemeral: true).ConfigureAwait(false); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + await DeferAsync(ephemeral: true).ConfigureAwait(false); |
| 88 | + |
| 89 | + var scope = scopeFactory.CreateAsyncScope(); |
| 90 | + await using (scope.ConfigureAwait(false)) |
| 91 | + { |
| 92 | + var disconnect = scope.ServiceProvider.GetRequiredService<IAccountDisconnectService>(); |
| 93 | + var preview = await disconnect.PreviewAsync(Context.Guild.Id, Context.User.Id).ConfigureAwait(false); |
| 94 | + if (!preview.IsConnected) |
| 95 | + { |
| 96 | + await FollowupAsync("You're not connected.", ephemeral: true).ConfigureAwait(false); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + var servers = preview.AffectedServerNames.Count > 0 |
| 101 | + ? string.Join(", ", preview.AffectedServerNames) |
| 102 | + : "no servers yet"; |
| 103 | + var components = new ComponentBuilder() |
| 104 | + .WithButton("Disconnect", DisconnectConfirmId, ButtonStyle.Danger) |
| 105 | + .WithButton("Cancel", DisconnectCancelId, ButtonStyle.Secondary) |
| 106 | + .Build(); |
| 107 | + await FollowupAsync( |
| 108 | + $"This disconnects your account and removes your credentials from: {servers}. Continue?", |
| 109 | + ephemeral: true, components: components) |
| 110 | + .ConfigureAwait(false); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary>Performs the disconnect after confirmation.</summary> |
| 115 | + [ComponentInteraction(DisconnectConfirmId)] |
| 116 | + public async Task DisconnectConfirmAsync() |
| 117 | + { |
| 118 | + if (Context.Guild is null) |
| 119 | + { |
| 120 | + await RespondAsync("This control must be used in a server.", ephemeral: true).ConfigureAwait(false); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + await DeferAsync(ephemeral: true).ConfigureAwait(false); |
| 125 | + |
| 126 | + var scope = scopeFactory.CreateAsyncScope(); |
| 127 | + await using (scope.ConfigureAwait(false)) |
| 128 | + { |
| 129 | + var disconnect = scope.ServiceProvider.GetRequiredService<IAccountDisconnectService>(); |
| 130 | + var count = await disconnect.DisconnectAsync(Context.Guild.Id, Context.User.Id).ConfigureAwait(false); |
| 131 | + await FollowupAsync($"Account disconnected. Removed from {count} server(s).", ephemeral: true) |
| 132 | + .ConfigureAwait(false); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary>Cancels the disconnect.</summary> |
| 137 | + [ComponentInteraction(DisconnectCancelId)] |
| 138 | + public async Task DisconnectCancelAsync() => |
| 139 | + await RespondAsync("Cancelled.", ephemeral: true).ConfigureAwait(false); |
71 | 140 | } |
0 commit comments