@@ -4,7 +4,10 @@ use crate::{
44 Context , Error ,
55} ;
66use :: serenity:: futures:: { Stream , StreamExt } ;
7- use poise:: serenity_prelude:: { futures, CreateAttachment , CreateEmbed } ;
7+ use poise:: serenity_prelude:: {
8+ self as serenity, futures, CreateAttachment , CreateEmbed , CreateInteractionResponse ,
9+ CreateInteractionResponseMessage ,
10+ } ;
811
912async fn autocomplete_snippet < ' a > (
1013 ctx : Context < ' a > ,
@@ -152,10 +155,7 @@ pub async fn remove_snippet(
152155) -> Result < ( ) , Error > {
153156 match get_snippet_lazy ( & ctx, & id) . await {
154157 Some ( snippet) => {
155- rm_snippet ( & ctx, & snippet) . await ;
156- let title = & "Snippet successfully removed" ;
157- let content = & & format ! ( "Removed snippet '{}: {}'" , snippet. id, snippet. title) ;
158- respond_ok ( & ctx, title, content) . await ;
158+ remove_snippet_confirm ( & ctx, & snippet) . await ?;
159159 }
160160 None => {
161161 let title = & "Failed to remove snippet" ;
@@ -215,10 +215,8 @@ pub async fn export_snippet(
215215) -> Result < ( ) , Error > {
216216 match get_snippet_lazy ( & ctx, & id) . await {
217217 Some ( snippet) => {
218- let attachment = CreateAttachment :: bytes (
219- format ! ( "{}" , & snippet. content. replace( '\n' , r"\n" ) ) ,
220- "snippet.txt" ,
221- ) ;
218+ let attachment =
219+ CreateAttachment :: bytes ( snippet. content . replace ( '\n' , r"\n" ) , "snippet.txt" ) ;
222220 let message = poise:: CreateReply :: default ( )
223221 . attachment ( attachment)
224222 . embed ( snippet. embed ( ) ) ;
@@ -282,3 +280,83 @@ async fn rm_snippet(ctx: &Context<'_>, snippet: &Snippet) {
282280 rwlock_guard. snippets . remove ( index) ;
283281 rwlock_guard. write ( ) ;
284282}
283+
284+ async fn remove_snippet_confirm ( ctx : & Context < ' _ > , snippet : & Snippet ) -> Result < ( ) , Error > {
285+ let snippet_embed = snippet. embed ( ) ;
286+
287+ let ctx_id = ctx. id ( ) ;
288+ let delete_id = format ! ( "{}cancel" , ctx_id) ;
289+ let cancel_id = format ! ( "{}delete" , ctx_id) ;
290+
291+ let components = serenity:: CreateActionRow :: Buttons ( vec ! [
292+ serenity:: CreateButton :: new( & cancel_id) . label( "Cancel" ) ,
293+ serenity:: CreateButton :: new( & delete_id)
294+ . label( "Delete" )
295+ . style( serenity:: ButtonStyle :: Danger ) ,
296+ ] ) ;
297+
298+ let builder: poise:: CreateReply = poise:: CreateReply :: default ( )
299+ . content ( format ! ( "Are you sure you want to delete snippet `{}`?" , snippet. id) )
300+ . ephemeral ( true )
301+ . embed ( snippet_embed)
302+ . components ( vec ! [ components] ) ;
303+
304+ ctx. send ( builder) . await ?;
305+
306+ while let Some ( press) = serenity:: ComponentInteractionCollector :: new ( ctx)
307+ . filter ( move |press| press. data . custom_id . starts_with ( & ctx_id. to_string ( ) ) )
308+ . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
309+ . await
310+ {
311+ if press. data . custom_id == delete_id {
312+ handle_delete ( ctx, snippet, press) . await ?;
313+ } else if press. data . custom_id == cancel_id {
314+ handle_cancel ( ctx, press) . await ?;
315+ }
316+ }
317+
318+ Ok ( ( ) )
319+ }
320+
321+ async fn handle_delete (
322+ ctx : & Context < ' _ > ,
323+ snippet : & Snippet ,
324+ interaction : serenity:: ComponentInteraction ,
325+ ) -> Result < ( ) , Error > {
326+
327+ rm_snippet ( ctx, snippet) . await ;
328+ interaction
329+ . create_response (
330+ ctx,
331+ CreateInteractionResponse :: UpdateMessage (
332+ CreateInteractionResponseMessage :: new ( ) . content ( "Deleted!" )
333+ . embeds ( vec ! [ ] )
334+ . components ( vec ! [ ] ) ,
335+ ) ,
336+ )
337+ . await ?;
338+
339+ let title = format ! ( "{} removed a snippet" , ctx. author( ) . tag( ) ) ;
340+ let content = & & format ! ( "Removed snippet `{}`" , snippet. format_output( ) ) ;
341+ respond_ok ( ctx, & title, content) . await ;
342+
343+ Ok ( ( ) )
344+ }
345+
346+ async fn handle_cancel (
347+ ctx : & Context < ' _ > ,
348+ interaction : serenity:: ComponentInteraction ,
349+ ) -> Result < ( ) , Error > {
350+ interaction
351+ . create_response (
352+ ctx,
353+ CreateInteractionResponse :: UpdateMessage (
354+ CreateInteractionResponseMessage :: new ( )
355+ . content ( "Aborted." )
356+ . embeds ( vec ! [ ] )
357+ . components ( vec ! [ ] ) ,
358+ ) ,
359+ )
360+ . await ?;
361+ Ok ( ( ) )
362+ }
0 commit comments