@@ -20,7 +20,7 @@ pub async fn snippet_command(
2020 . as_ref ( )
2121 . ok_or_else ( database_connection_failed) ?;
2222
23- let content = match extract_reply_content ( & msg. content , & config. command . prefix , & [ "snippet" ] ) {
23+ let content = match extract_reply_content ( & msg. content , & config. command . prefix , & [ "snippet" , "s" ] ) {
2424 Some ( c) => c,
2525 None => {
2626 MessageBuilder :: system_message ( & ctx, config)
@@ -49,18 +49,7 @@ pub async fn snippet_command(
4949 "edit" => handle_edit ( & ctx, & msg, args, pool, config) . await ,
5050 "delete" => handle_delete ( & ctx, & msg, args, pool, config) . await ,
5151 _ => {
52- MessageBuilder :: system_message ( & ctx, config)
53- . translated_content (
54- "snippet.unknown_text_subcommand" ,
55- None ,
56- Some ( msg. author . id ) ,
57- msg. guild_id . map ( |g| g. get ( ) ) ,
58- )
59- . await
60- . reply_to ( msg)
61- . send ( true )
62- . await ?;
63- Ok ( ( ) )
52+ handle_use ( & ctx, & msg, subcommand, pool, config) . await
6453 }
6554 }
6655}
@@ -72,9 +61,17 @@ async fn handle_create(
7261 pool : & sqlx:: SqlitePool ,
7362 config : & Config ,
7463) -> ModmailResult < ( ) > {
75- let mut parts = args. splitn ( 2 , ' ' ) ;
76- let key = parts. next ( ) . unwrap_or ( "" ) . trim ( ) ;
77- let content = parts. next ( ) . unwrap_or ( "" ) . trim ( ) ;
64+ let trimmed_args = args. trim_start ( ) ;
65+ let split_pos = trimmed_args
66+ . find ( |c : char | c. is_whitespace ( ) )
67+ . unwrap_or ( trimmed_args. len ( ) ) ;
68+
69+ let key = trimmed_args[ ..split_pos] . trim ( ) ;
70+ let content = if split_pos < trimmed_args. len ( ) {
71+ & trimmed_args[ split_pos + 1 ..]
72+ } else {
73+ ""
74+ } ;
7875
7976 if key. is_empty ( ) || content. is_empty ( ) {
8077 MessageBuilder :: system_message ( ctx, config)
@@ -267,9 +264,17 @@ async fn handle_edit(
267264 pool : & sqlx:: SqlitePool ,
268265 config : & Config ,
269266) -> ModmailResult < ( ) > {
270- let mut parts = args. splitn ( 2 , ' ' ) ;
271- let key = parts. next ( ) . unwrap_or ( "" ) . trim ( ) ;
272- let content = parts. next ( ) . unwrap_or ( "" ) . trim ( ) ;
267+ let trimmed_args = args. trim_start ( ) ;
268+ let split_pos = trimmed_args
269+ . find ( |c : char | c. is_whitespace ( ) )
270+ . unwrap_or ( trimmed_args. len ( ) ) ;
271+
272+ let key = trimmed_args[ ..split_pos] . trim ( ) ;
273+ let content = if split_pos < trimmed_args. len ( ) {
274+ & trimmed_args[ split_pos + 1 ..]
275+ } else {
276+ ""
277+ } ;
273278
274279 if key. is_empty ( ) || content. is_empty ( ) {
275280 MessageBuilder :: system_message ( ctx, config)
@@ -367,3 +372,67 @@ async fn handle_delete(
367372
368373 Ok ( ( ) )
369374}
375+
376+ async fn handle_use (
377+ ctx : & Context ,
378+ msg : & Message ,
379+ key : & str ,
380+ pool : & sqlx:: SqlitePool ,
381+ config : & Config ,
382+ ) -> ModmailResult < ( ) > {
383+ if key. is_empty ( ) {
384+ MessageBuilder :: system_message ( ctx, config)
385+ . translated_content (
386+ "snippet.text_usage" ,
387+ None ,
388+ Some ( msg. author . id ) ,
389+ msg. guild_id . map ( |g| g. get ( ) ) ,
390+ )
391+ . await
392+ . reply_to ( msg. clone ( ) )
393+ . send ( true )
394+ . await ?;
395+ return Ok ( ( ) ) ;
396+ }
397+
398+ match get_snippet_by_key ( key, pool) . await ? {
399+ Some ( snippet) => {
400+ let thread = get_thread_by_channel_id ( & msg. channel_id . to_string ( ) , pool) . await ;
401+
402+ if let Some ( thread) = thread {
403+ let user_id = serenity:: all:: UserId :: new ( thread. user_id as u64 ) ;
404+
405+ use crate :: utils:: message:: message_builder:: StaffReply ;
406+
407+ let message_number = allocate_next_message_number ( & thread. id , pool) . await ?;
408+
409+ StaffReply :: new (
410+ ctx,
411+ config,
412+ thread. id . clone ( ) ,
413+ msg. author . id ,
414+ msg. author . name . clone ( ) ,
415+ message_number,
416+ )
417+ . to_thread ( msg. channel_id )
418+ . to_user ( user_id)
419+ . content ( snippet. content )
420+ . send_msg_and_record ( pool)
421+ . await ?;
422+ } else {
423+ MessageBuilder :: system_message ( ctx, config)
424+ . content ( & snippet. content )
425+ . reply_to ( msg. clone ( ) )
426+ . send ( true )
427+ . await ?;
428+ }
429+ }
430+ None => {
431+ return Err ( ModmailError :: Command ( CommandError :: SnippetNotFound (
432+ key. to_string ( ) ,
433+ ) ) ) ;
434+ }
435+ }
436+
437+ Ok ( ( ) )
438+ }
0 commit comments