44import com .google .gson .JsonObject ;
55import com .google .gson .JsonParser ;
66import net .dv8tion .jda .api .EmbedBuilder ;
7+ import net .dv8tion .jda .api .entities .Guild ;
8+ import net .dv8tion .jda .api .entities .MessageEmbed ;
9+ import net .dv8tion .jda .api .events .interaction .command .MessageContextInteractionEvent ;
710import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
11+ import net .dv8tion .jda .api .requests .restaction .interactions .InteractionCallbackAction ;
812import net .dv8tion .jda .api .requests .restaction .interactions .ReplyCallbackAction ;
913import net .javadiscord .javabot .Bot ;
1014import net .javadiscord .javabot .command .Responses ;
15+ import net .javadiscord .javabot .command .interfaces .MessageContextCommand ;
1116import net .javadiscord .javabot .command .interfaces .SlashCommand ;
17+ import net .javadiscord .javabot .data .config .GuildConfig ;
1218
1319import java .io .IOException ;
1420import java .net .HttpURLConnection ;
2329/**
2430 * Command that allows members to search the internet using the bing api.
2531 */
26- public class SearchCommand implements SlashCommand {
32+ public class SearchCommand implements SlashCommand , MessageContextCommand {
2733
2834 private static final String HOST = "https://api.bing.microsoft.com" ;
2935 private static final String PATH = "/v7.0/search" ;
@@ -56,20 +62,14 @@ private SearchResults searchWeb(String searchQuery) throws IOException {
5662 return results ;
5763 }
5864
59- @ Override
60- public ReplyCallbackAction handleSlashCommandInteraction (SlashCommandInteractionEvent event ) {
61- var query = event .getOption ("query" );
62- if (query == null ) {
63- return Responses .warning (event , "Missing Required Query" );
64- }
65- String searchTerm = query .getAsString ();
65+ private MessageEmbed handleSearch (String searchTerm , Guild guild ) {
66+ GuildConfig config = Bot .config .get (guild );
6667 String name ;
6768 String url ;
6869 String snippet ;
6970 var embed = new EmbedBuilder ()
70- .setColor (Bot . config . get ( event . getGuild ()) .getSlashCommand ().getDefaultColor ())
71+ .setColor (config .getSlashCommand ().getDefaultColor ())
7172 .setTitle ("Search Results" );
72-
7373 try {
7474 SearchResults result = searchWeb (searchTerm );
7575 JsonObject json = JsonParser .parseString (result .jsonResponse ).getAsJsonObject ();
@@ -92,12 +92,33 @@ public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteraction
9292 resultString .append ("**" ).append (i + 1 ).append (". [" ).append (name ).append ("](" )
9393 .append (url ).append (")** \n " ).append (snippet ).append ("\n \n " );
9494 }
95-
9695 embed .setDescription (resultString );
9796 } catch (IOException e ) {
98- return Responses .info (event , "Not Found" , "There were no results for your search. This might be due to safe-search or because your search was too complex. Please try again." );
97+ return new EmbedBuilder ()
98+ .setColor (config .getSlashCommand ().getDefaultColor ())
99+ .setTitle ("No Results" )
100+ .setDescription ("There were no results for your search. This might be due to safe-search or because your search was too complex. Please try again." )
101+ .build ();
102+ }
103+ return embed .build ();
104+ }
105+
106+ @ Override
107+ public ReplyCallbackAction handleSlashCommandInteraction (SlashCommandInteractionEvent event ) {
108+ var query = event .getOption ("query" );
109+ if (query == null ) {
110+ return Responses .warning (event , "No Query" , "Missing Required Query" );
111+ }
112+ return event .replyEmbeds (handleSearch (query .getAsString (), event .getGuild ()));
113+ }
114+
115+ @ Override
116+ public InteractionCallbackAction <?> handleMessageContextCommandInteraction (MessageContextInteractionEvent event ) {
117+ String query = event .getTarget ().getContentDisplay ();
118+ if (query .equals ("" )) {
119+ return Responses .warning (event , "No Content" , "Message doesn't have any content." );
99120 }
100- return event .replyEmbeds (embed . build ( ));
121+ return event .replyEmbeds (handleSearch ( query , event . getGuild () ));
101122 }
102123
103124 /**
0 commit comments