@@ -4,7 +4,7 @@ use serenity::json::Value;
44use serenity:: model:: prelude:: command:: CommandOptionType ;
55use serenity:: model:: prelude:: interaction:: application_command:: { ApplicationCommandInteraction , CommandDataOptionValue } ;
66use serenity:: prelude:: Context ;
7- use crate :: structures:: { State , Snippet } ;
7+ use crate :: structures:: { State , Snippet , Embeddable } ;
88use crate :: commands:: { arg, respond_ok} ;
99
1010use super :: { respond_err, respond_embed, arg_opt} ;
@@ -27,10 +27,8 @@ pub(super) fn sync_snippets(state: &State, command: &mut CreateApplicationComman
2727pub ( super ) async fn snippet ( ctx : & Context , interaction : & ApplicationCommandInteraction ) {
2828 match arg ( interaction, "id" ) {
2929 CommandDataOptionValue :: String ( id) => {
30- interaction. defer ( ctx) . await . expect ( "Failed to defer interaction" ) ;
31-
3230 if let Some ( snippet) = get_snippet ( ctx, & id) . await {
33- let embed = snippet. create_embed ( ) ;
31+ let embed = snippet. embed ( ) ;
3432
3533 respond_embed ( ctx, interaction, & embed, false ) . await ;
3634 } else {
@@ -47,8 +45,6 @@ pub(super) async fn edit_snippet(ctx: &Context, interaction: &ApplicationCommand
4745 let content = arg_opt ( interaction, "content" ) ;
4846
4947 if let CommandDataOptionValue :: String ( id) = id {
50- interaction. defer ( ctx) . await . expect ( "Failed to defer interaction" ) ;
51-
5248 {
5349 let mut data = ctx. data . write ( ) . await ;
5450 let state = data. get_mut :: < State > ( ) . expect ( "Failed to get state" ) ;
@@ -97,7 +93,7 @@ pub(super) async fn edit_snippet(ctx: &Context, interaction: &ApplicationCommand
9793
9894 let mut embed = get_snippet ( ctx, & id) . await
9995 . expect ( "Failed to get snippet for recently modified snippet" )
100- . create_embed ( ) ;
96+ . embed ( ) ;
10197
10298 embed. colour ( super :: OK_COLOUR ) ;
10399
@@ -116,8 +112,6 @@ pub(super) async fn create_snippet(ctx: &Context, interaction: &ApplicationComma
116112 CommandDataOptionValue :: String ( title) ,
117113 CommandDataOptionValue :: String ( content)
118114 ) => {
119- interaction. defer ( ctx) . await . expect ( "Failed to defer interaction" ) ;
120-
121115 let embed = {
122116 let mut data = ctx. data . write ( ) . await ;
123117 let state = data. get_mut :: < State > ( ) . expect ( "Failed to get state" ) ;
@@ -134,7 +128,7 @@ pub(super) async fn create_snippet(ctx: &Context, interaction: &ApplicationComma
134128
135129 println ! ( "New snippet created '{}: {}'" , id, title) ;
136130
137- let mut embed = snippet. create_embed ( ) ;
131+ let mut embed = snippet. embed ( ) ;
138132 embed. colour ( super :: OK_COLOUR ) ;
139133
140134 state. snippets . push ( snippet) ;
@@ -159,8 +153,6 @@ pub(super) async fn remove_snippet(ctx: &Context, interaction: &ApplicationComma
159153
160154 match id {
161155 CommandDataOptionValue :: String ( id) => {
162- interaction. defer ( ctx) . await . expect ( "Failed to defer interaction" ) ;
163-
164156 println ! ( "Removing snippet '{id}'" ) ;
165157
166158 match get_snippet ( ctx, & id) . await {
@@ -188,16 +180,12 @@ pub(super) async fn export_snippet(ctx: &Context, interaction: &ApplicationComma
188180
189181 match id {
190182 CommandDataOptionValue :: String ( id) => {
191- interaction. defer ( ctx) . await . expect ( "Failed to defer interaction" ) ;
192-
193183 let snippet = get_snippet ( ctx, & id) . await
194184 . expect ( "Failed to get snippet" ) ;
195185
196- let embed = snippet. create_embed ( ) ;
197-
198186 let result = interaction. create_followup_message ( ctx, |r| r
199187 . content ( format ! ( "```{}```" , & snippet. content. replace( "\n " , r#"\n"# ) ) )
200- . add_embed ( embed)
188+ . add_embed ( snippet . embed ( ) )
201189 ) . await ;
202190
203191 if let Err ( e) = result {
@@ -208,12 +196,8 @@ pub(super) async fn export_snippet(ctx: &Context, interaction: &ApplicationComma
208196 }
209197}
210198
211- trait SnippetEmbed {
212- fn create_embed ( & self ) -> CreateEmbed ;
213- }
214-
215- impl SnippetEmbed for Snippet {
216- fn create_embed ( & self ) -> CreateEmbed {
199+ impl Embeddable for Snippet {
200+ fn embed ( & self ) -> CreateEmbed {
217201 CreateEmbed :: default ( )
218202 . title ( & self . title )
219203 . description ( & self . content )
0 commit comments