44using DSharpPlus . Commands ;
55using DSharpPlus . Commands . Processors . SlashCommands ;
66using DSharpPlus . Entities ;
7+ using DSharpPlus . EventArgs ;
8+ using DSharpPlus . Interactivity ;
79using MediatR ;
810using Microsoft . Extensions . Options ;
911using Nellebot . Utils ;
1012
1113namespace Nellebot . CommandHandlers ;
1214
13- public record ValhallKickUserCommand ( CommandContext Ctx , DiscordMember Member , string Reason )
15+ public record ValhallKickUserCommand ( CommandContext Ctx , DiscordMember Member , string ? Reason )
1416 : BotCommandCommand ( Ctx ) ;
1517
1618public class ValhallKickUserHandler : IRequestHandler < ValhallKickUserCommand >
1719{
20+ private readonly InteractivityExtension _interactivityExtension ;
21+ private const string ModalTextInputId = "modal-text-input" ;
1822 private readonly BotOptions _options ;
1923
20- public ValhallKickUserHandler ( IOptions < BotOptions > options )
24+ public ValhallKickUserHandler ( IOptions < BotOptions > options , InteractivityExtension interactivityExtension )
2125 {
26+ _interactivityExtension = interactivityExtension ;
2227 _options = options . Value ;
2328 }
2429
@@ -30,7 +35,7 @@ public async Task Handle(ValhallKickUserCommand request, CancellationToken cance
3035
3136 if ( ctx . Member ? . Id == targetMember . Id )
3237 {
33- await TryRespondEphemeral ( ctx , "Hmm" ) ;
38+ await ctx . TryRespondEphemeral ( "Hmm" ) ;
3439 return ;
3540 }
3641
@@ -43,24 +48,58 @@ public async Task Handle(ValhallKickUserCommand request, CancellationToken cance
4348 var content =
4449 $ "You cannot vkick this user. They have been a member of the server for more than { maxAgeHours } hours.";
4550
46- await TryRespondEphemeral ( ctx , content ) ;
51+ await ctx . TryRespondEphemeral ( content ) ;
4752
4853 return ;
4954 }
5055
51- var kickReason =
52- $ "Kicked on behalf of { currentMember . DisplayName } . Reason: { request . Reason . NullOrWhiteSpaceTo ( "/shrug" ) } " ;
56+ DiscordInteraction ? modalInteraction = null ;
57+ string ? kickReason = null ;
5358
54- await targetMember . RemoveAsync ( kickReason ) ;
59+ if ( ctx is SlashCommandContext slashCtx && request . Reason == null )
60+ {
61+ ModalSubmittedEventArgs modalSubmissionResult = await ShowGetReasonModal ( slashCtx ) ;
62+
63+ modalInteraction = modalSubmissionResult . Interaction ;
64+
65+ kickReason = modalSubmissionResult . Values [ ModalTextInputId ] ;
66+
67+ await modalInteraction . DeferAsync ( ephemeral : true ) ;
68+ }
69+
70+ kickReason = kickReason . NullOrWhiteSpaceTo ( "/shrug" ) ;
5571
56- await TryRespondEphemeral ( ctx , "User vkicked successfully" ) ;
72+ var onBehalfOfReason =
73+ $ "Kicked on behalf of { currentMember . DisplayName } . Reason: { kickReason } ";
74+
75+ await targetMember . RemoveAsync ( onBehalfOfReason ) ;
76+
77+ await ctx . TryRespondEphemeral ( "User vkicked successfully" , modalInteraction ) ;
5778 }
5879
59- private static async Task TryRespondEphemeral ( CommandContext ctx , string successMessage )
80+ private async Task < ModalSubmittedEventArgs > ShowGetReasonModal ( SlashCommandContext ctx )
6081 {
61- if ( ctx is SlashCommandContext slashCtx )
62- await slashCtx . RespondAsync ( successMessage , true ) ;
63- else
64- await ctx . RespondAsync ( successMessage ) ;
82+ var modalId = $ "get-reason-modal-{ Guid . NewGuid ( ) } ";
83+
84+ DiscordInteractionResponseBuilder interactionBuilder = new DiscordInteractionResponseBuilder ( )
85+ . WithTitle ( "Valhall kick user" )
86+ . WithCustomId ( modalId )
87+ . AddTextInputComponent (
88+ new DiscordTextInputComponent (
89+ "Reason" ,
90+ ModalTextInputId ,
91+ "Write a reason for kicking" ,
92+ string . Empty ,
93+ required : true ,
94+ DiscordTextInputStyle . Paragraph ,
95+ min_length : 0 ,
96+ DiscordConstants . MaxAuditReasonLength ) ) ;
97+
98+ await ctx . RespondWithModalAsync ( interactionBuilder ) ;
99+
100+ InteractivityResult < ModalSubmittedEventArgs > modalSubmission =
101+ await _interactivityExtension . WaitForModalAsync ( modalId , DiscordConstants . MaxDeferredInteractionWait ) ;
102+
103+ return modalSubmission . Result ;
65104 }
66105}
0 commit comments