11use crate :: commands:: add_staff:: common:: add_user_to_channel;
2+ use crate :: commands:: { BoxFuture , RegistrableCommand } ;
23use crate :: config:: Config ;
34use crate :: db:: thread_exists;
45use crate :: errors:: CommandError :: InvalidFormat ;
56use crate :: errors:: ThreadError :: NotAThreadChannel ;
6- use crate :: errors:: { CommandError , ModmailError , ModmailResult , common } ;
7+ use crate :: errors:: { common , CommandError , ModmailError , ModmailResult } ;
78use crate :: i18n:: get_translated_message;
89use crate :: utils:: command:: defer_response:: defer_response;
910use crate :: utils:: message:: message_builder:: MessageBuilder ;
@@ -13,84 +14,106 @@ use serenity::all::{
1314} ;
1415use std:: collections:: HashMap ;
1516
16- pub async fn register ( config : & Config ) -> CreateCommand {
17- let cmd_desc = get_translated_message (
18- config,
19- "slash_command.add_staff_command_description" ,
20- None ,
21- None ,
22- None ,
23- None ,
24- )
25- . await ;
26- let user_id_desc = get_translated_message (
27- config,
28- "slash_command.add_staff_user_id_argument" ,
29- None ,
30- None ,
31- None ,
32- None ,
33- )
34- . await ;
17+ pub struct AddStaffCommand ;
3518
36- CreateCommand :: new ( "add_staff" )
37- . description ( cmd_desc)
38- . add_option (
39- CreateCommandOption :: new ( CommandOptionType :: User , "user_id" , user_id_desc)
40- . required ( true ) ,
41- )
42- }
19+ impl RegistrableCommand for AddStaffCommand {
20+ fn name ( & self ) -> & ' static str {
21+ "add_staff"
22+ }
4323
44- pub async fn run (
45- ctx : & Context ,
46- command : & CommandInteraction ,
47- _options : & [ ResolvedOption < ' _ > ] ,
48- config : & Config ,
49- ) -> ModmailResult < ( ) > {
50- let pool = config
51- . db_pool
52- . as_ref ( )
53- . ok_or_else ( common:: database_connection_failed) ?;
24+ fn register ( & self , config : & Config ) -> BoxFuture < Vec < CreateCommand > > {
25+ let config = config. clone ( ) ;
5426
55- defer_response ( & ctx, & command) . await ?;
27+ Box :: pin ( async move {
28+ let cmd_desc = get_translated_message (
29+ & config,
30+ "slash_command.add_staff_command_description" ,
31+ None ,
32+ None ,
33+ None ,
34+ None ,
35+ )
36+ . await ;
5637
57- let user_id = match command
58- . data
59- . options
60- . iter ( )
61- . find ( |opt| opt. name == "user_id" )
62- {
63- Some ( opt) => match & opt. value {
64- CommandDataOptionValue :: User ( user_id) => * user_id,
65- _ => {
66- return Err ( ModmailError :: Command ( CommandError :: InvalidArguments (
67- "user_id" . to_string ( ) ,
68- ) ) ) ;
69- }
70- } ,
71- None => return Err ( ModmailError :: Command ( CommandError :: MissingArguments ) ) ,
72- } ;
38+ let user_id_desc = get_translated_message (
39+ & config,
40+ "slash_command.add_staff_user_id_argument" ,
41+ None ,
42+ None ,
43+ None ,
44+ None ,
45+ )
46+ . await ;
47+
48+ vec ! [
49+ CreateCommand :: new( "add_staff" )
50+ . description( cmd_desc)
51+ . add_option(
52+ CreateCommandOption :: new( CommandOptionType :: User , "user_id" , user_id_desc)
53+ . required( true ) ,
54+ ) ,
55+ ]
56+ } )
57+ }
58+
59+ fn run (
60+ & self ,
61+ ctx : & Context ,
62+ command : & CommandInteraction ,
63+ _options : & [ ResolvedOption < ' _ > ] ,
64+ config : & Config ,
65+ ) -> BoxFuture < ModmailResult < ( ) > > {
66+ let ctx = ctx. clone ( ) ;
67+ let command = command. clone ( ) ;
68+ let config = config. clone ( ) ;
69+
70+ Box :: pin ( async move {
71+ let pool = config
72+ . db_pool
73+ . as_ref ( )
74+ . ok_or_else ( common:: database_connection_failed) ?;
75+
76+ defer_response ( & ctx, & command) . await ?;
77+
78+ let user_id = match command
79+ . data
80+ . options
81+ . iter ( )
82+ . find ( |opt| opt. name == "user_id" )
83+ {
84+ Some ( opt) => match & opt. value {
85+ CommandDataOptionValue :: User ( user_id) => * user_id,
86+ _ => {
87+ return Err ( ModmailError :: Command ( CommandError :: InvalidArguments (
88+ "user_id" . to_string ( ) ,
89+ ) ) ) ;
90+ }
91+ } ,
92+ None => return Err ( ModmailError :: Command ( CommandError :: MissingArguments ) ) ,
93+ } ;
7394
74- if thread_exists ( command. user . id , pool) . await {
75- match add_user_to_channel ( ctx, command. channel_id , user_id) . await {
76- Ok ( _) => {
77- let mut params = HashMap :: new ( ) ;
78- params. insert ( "user" . to_string ( ) , format ! ( "<@{}>" , user_id) ) ;
95+ if thread_exists ( command. user . id , pool) . await {
96+ match add_user_to_channel ( & ctx, command. channel_id , user_id) . await {
97+ Ok ( _) => {
98+ let mut params = HashMap :: new ( ) ;
99+ params. insert ( "user" . to_string ( ) , format ! ( "<@{}>" , user_id) ) ;
79100
80- let response = MessageBuilder :: system_message ( ctx, config)
81- . translated_content ( "add_staff.add_success" , Some ( & params) , None , None )
82- . await
83- . to_channel ( command. channel_id )
84- . build_interaction_message_followup ( )
85- . await ;
101+ let response = MessageBuilder :: system_message ( & ctx, & config)
102+ . translated_content ( "add_staff.add_success" , Some ( & params) , None , None )
103+ . await
104+ . to_channel ( command. channel_id )
105+ . build_interaction_message_followup ( )
106+ . await ;
86107
87- let _ = command. create_followup ( & ctx. http , response) . await ;
108+ let _ = command. create_followup ( & ctx. http , response) . await ;
88109
89- Ok ( ( ) )
110+ Ok ( ( ) )
111+ }
112+ Err ( ..) => Err ( ModmailError :: Command ( InvalidFormat ) ) ,
113+ }
114+ } else {
115+ Err ( ModmailError :: Thread ( NotAThreadChannel ) )
90116 }
91- Err ( ..) => Err ( ModmailError :: Command ( InvalidFormat ) ) ,
92- }
93- } else {
94- Err ( ModmailError :: Thread ( NotAThreadChannel ) )
117+ } )
95118 }
96119}
0 commit comments