Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 0 additions & 89 deletions application/config.json.template

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.togetherjava.tjbot.commands.moderation.temp.TemporaryModerationRoutine;
import org.togetherjava.tjbot.commands.reminder.RemindCommand;
import org.togetherjava.tjbot.commands.reminder.RemindRoutine;
import org.togetherjava.tjbot.commands.search.GoogleCommand;
import org.togetherjava.tjbot.commands.search.GoogleItCommand;
import org.togetherjava.tjbot.commands.system.BotCore;
import org.togetherjava.tjbot.commands.system.LogLevelCommand;
import org.togetherjava.tjbot.commands.tags.TagCommand;
Expand Down Expand Up @@ -127,6 +129,8 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new WolframAlphaCommand(config));
features.add(new AskCommand(config, helpSystemHelper));
features.add(new HelpThreadCommand(config, helpSystemHelper));
features.add(new GoogleCommand(config));
features.add(new GoogleItCommand(config));

// Mixtures
features.add(new HelpThreadOverviewUpdater(config, helpSystemHelper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ private RestAction<Message> sendExplanationMessage(GuildMessageChannel threadCha
With enough info, someone knows the answer for sure."""),
HelpSystemHelper.embedWith(
"Don't forget to close your thread using the command %s when your question has been answered, thanks."
.formatted(closeCommandMention)));
.formatted(closeCommandMention)),

HelpSystemHelper.embedWith(
"You can now search your question on Google using the `/google` slash command! If you want me to search your question for you, just enter the slash command `/googleit`"));

MessageCreateAction action = threadChannel.sendMessage(message);
if (useCodeSyntaxExampleImage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.togetherjava.tjbot.commands.search;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.commands.CommandVisibility;
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.config.Config;

import java.net.http.HttpResponse;
import java.util.Objects;

/**
* <p>
* This class contains the logic required for the '/google' slash command.
* </p>
* <p>
* The syntax of the command is as followed: /google query [search term].
* </p>
* <p>
* Results are sent to the user as embeds. The result contains the questions and answers of the
* search results under "People also asked" alongside the first 5 organic search results.
* </p>
*
* @author <a href="https://github.com/surajkumar">Suraj Kumar</a>
*/
public class GoogleCommand extends SlashCommandAdapter {
private static final Logger logger = LoggerFactory.getLogger(GoogleCommand.class);
/**
* The option that contains the search term within the /google command.
*/
private static final String COMMAND_OPTION = "query";
/**
* The search strategy that is going to return the results to us.
*/
private final SearchStrategy<HttpResponse<String>> searchStrategy;

/**
* <p>
* Constructs a new {@code GoogleCommand} object and sets up the metadata for this command
* including the name, description and command options.
* </p>
*/
public GoogleCommand(Config config) {
super("google", "Searches Google for your search query", CommandVisibility.GUILD);
this.getData()
.addOption(OptionType.STRING, COMMAND_OPTION, "the query to send to Google", true);
searchStrategy = new GoogleSearchStrategy(config.getSerpapiApiKey());
}

/**
* <p>
* The main logic for the /google command. When we receive the event from discord, the reply is
* deferred for a later period of time. This is because calling the
* {@link org.togetherjava.tjbot.commands.search.SearchStrategy} is considered a blocking
* operation.
* </p>
*
* @param event the event provided by Discord to ack the initialization of the slash command.
*/
@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
logger.info("Received /google slash command");

event.deferReply().queue();
String searchTerm = Objects.requireNonNull(event.getOption(COMMAND_OPTION)).getAsString();
logger.debug("{} entered search term {}",
Objects.requireNonNull(event.getMember()).getEffectiveName(), searchTerm);

new GoogleResponseComposer().doSearchAndSendResponse(searchStrategy, searchTerm, event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.togetherjava.tjbot.commands.search;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.commands.CommandVisibility;
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.config.Config;

import java.net.http.HttpResponse;

/**
* <p>
* This class is designed for specific use in the #active_questions channel within the Together Java
* Discord server. As such, it is a convenience command that allows users to search their questions
* (thread title) on Google.
* </p>
*
* @author <a href="https://github.com/surajkumar">Suraj Kumar</a>
*/
public class GoogleItCommand extends SlashCommandAdapter {
private static final Logger logger = LoggerFactory.getLogger(GoogleItCommand.class);
/**
* The search strategy that is going to return the results to us.
*/
private final SearchStrategy<HttpResponse<String>> searchStrategy;

/**
* The error message displayed to the user if they are not within a thread in #active_questions.
*/
private static final String WRONG_CHANNEL_ERROR =
"You must be within a thread in #active_questions to run this command.";

/**
* <p>
* Constructs a new {@code GoogleItCommand} object and sets up the metadata for this command
* including the name, description
* </p>
*/
public GoogleItCommand(Config config) {
super("googleit", "Searches the channel title on Google", CommandVisibility.GUILD);
searchStrategy = new GoogleSearchStrategy(config.getSerpapiApiKey());
}

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
logger.info("Received /googleit slash command");
try {
String parent = event.getChannel().asThreadChannel().getParentChannel().getName();
if (parent.equals("active_questions")) {
String searchTerm = event.getChannel().asThreadChannel().getName();
event.deferReply().queue();
new GoogleResponseComposer().doSearchAndSendResponse(searchStrategy, searchTerm,
event);
} else {
event.reply(WRONG_CHANNEL_ERROR).queue();
}
} catch (IllegalStateException ex) {
event.reply(WRONG_CHANNEL_ERROR).queue();
}
}
}
Loading