Skip to content

Commit cd10f82

Browse files
committed
feat(pairing): #setup disconnect-account button with confirm
1 parent 9f3d500 commit cd10f82

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

src/RustPlusBot.Features.Pairing/Modules/CredentialModule.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using Discord;
12
using Discord.Interactions;
23
using Microsoft.Extensions.DependencyInjection;
4+
using RustPlusBot.Features.Pairing.Accounts;
35
using RustPlusBot.Features.Pairing.Listening;
46
using RustPlusBot.Features.Pairing.Supervisor;
57
using RustPlusBot.Features.Pairing.Validation;
@@ -68,4 +70,71 @@ await FollowupAsync(
6870
await FollowupAsync(message, ephemeral: true).ConfigureAwait(false);
6971
}
7072
}
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);
71140
}

0 commit comments

Comments
 (0)