diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java
index 73e26ad..88a7f8f 100644
--- a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java
+++ b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java
@@ -59,7 +59,7 @@ import java.util.TreeMap;
* @created Feb 11, 2004
* @since 1.0
*/
-public final class CurrencyConverter extends AbstractModule {
+public final class CurrencyConverter extends ThreadedModule {
// The rates keyword.
static final String CURRENCY_RATES_KEYWORD = "rates";
// The currency command.
@@ -78,7 +78,7 @@ public final class CurrencyConverter extends AbstractModule {
commands.add(CURRENCY_CMD);
}
- static Message convertCurrency(String query) throws ModuleException {
+ static Message convertCurrency(final String query) throws ModuleException {
if (EXCHANGE_RATES.isEmpty()) {
try {
final SAXBuilder builder = new SAXBuilder();
@@ -168,7 +168,7 @@ public final class CurrencyConverter extends AbstractModule {
}
}
- new Thread(() -> run(bot, sender, args)).start();
+ super.commandResponse(bot, sender, args, isPrivate);
}
/**
@@ -191,7 +191,7 @@ public final class CurrencyConverter extends AbstractModule {
* Converts the specified currencies.
*/
@SuppressFBWarnings(value = "REDOS")
- private void run(final Mobibot bot, final String sender, final String query) {
+ void run(final Mobibot bot, final String sender, final String query) {
if (Utils.isValidString(sender) && Utils.isValidString(query)) {
if (query.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+")) {
try {
diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java
index 074369b..5f40429 100644
--- a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java
+++ b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java
@@ -50,7 +50,7 @@ import java.nio.charset.StandardCharsets;
* @created 2014-04-20
* @since 1.0
*/
-public final class Joke extends AbstractModule {
+public final class Joke extends ThreadedModule {
// The ICNDB URL.
private static final String JOKE_URL =
"http://api.icndb.com/jokes/random?escape=javascript&exclude=[explicit]&limitTo=[nerdy]";
@@ -94,14 +94,6 @@ public final class Joke extends AbstractModule {
}
}
- /**
- * {@inheritDoc}
- */
- @Override
- public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
- new Thread(() -> run(bot, sender)).start();
- }
-
/**
* {@inheritDoc}
*/
@@ -114,7 +106,7 @@ public final class Joke extends AbstractModule {
/**
* Returns a random joke from The Internet Chuck Norris Database
*/
- private void run(final Mobibot bot, final String sender) {
+ void run(final Mobibot bot, final String sender, String arg) {
try {
bot.send(bot.getChannel(), Utils.cyan(randomJoke().getMessage()));
} catch (ModuleException e) {
diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java
index c51566d..e8f7a14 100644
--- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java
+++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java
@@ -53,7 +53,7 @@ import java.util.ArrayList;
* @created Feb 7, 2004
* @since 1.0
*/
-public final class StockQuote extends AbstractModule {
+public final class StockQuote extends ThreadedModule {
/**
* The Alpha Advantage property key.
*/
@@ -79,81 +79,78 @@ public final class StockQuote extends AbstractModule {
* @return The stock quote.
* @throws ModuleException If an errors occurs.
*/
- static ArrayList getQuote(String symbol, String apiKey) throws ModuleException {
- final String debugMessage = "getQuote(" + symbol + ')';
- final ArrayList messages = new ArrayList<>();
- final OkHttpClient client = new OkHttpClient();
- final Request request =
- new Request.Builder().url(ALAPHADVANTAGE_URL + "&symbol=" + symbol + "&apikey=" + apiKey).build();
+ static ArrayList getQuote(final String symbol, final String apiKey) throws ModuleException {
+ if (!Utils.isValidString(apiKey)) {
+ throw new ModuleException(Utils.capitalize(STOCK_CMD) + " is disabled. The API key is missing.");
+ }
- try {
- final Response response = client.newCall(request).execute();
- if (response.body() != null) {
- final JSONObject json = new JSONObject(response.body().string());
+ if (Utils.isValidString(symbol)) {
+ final String debugMessage = "getQuote(" + symbol + ')';
+ final ArrayList messages = new ArrayList<>();
+ final OkHttpClient client = new OkHttpClient();
+ final Request request =
+ new Request.Builder().url(ALAPHADVANTAGE_URL + "&symbol=" + symbol + "&apikey=" + apiKey).build();
- try {
- final String info = json.getString("Information");
- if (!info.isEmpty()) {
- throw new ModuleException(debugMessage, Utils.unescapeXml(info));
+ try {
+ final Response response = client.newCall(request).execute();
+ if (response.body() != null) {
+ final JSONObject json = new JSONObject(response.body().string());
+
+ try {
+ final String info = json.getString("Information");
+ if (!info.isEmpty()) {
+ throw new ModuleException(debugMessage, Utils.unescapeXml(info));
+ }
+ } catch (JSONException ignore) {
+ // Do nothing.
}
- } catch (JSONException ignore) {
- // Do nothing.
- }
- try {
- final String error = json.getString("Error Message");
- if (!error.isEmpty()) {
- throw new ModuleException(debugMessage, Utils.unescapeXml(error));
+ try {
+ final String error = json.getString("Error Message");
+ if (!error.isEmpty()) {
+ throw new ModuleException(debugMessage, Utils.unescapeXml(error));
+ }
+ } catch (JSONException ignore) {
+ // Do nothing.
}
- } catch (JSONException ignore) {
- // Do nothing.
+
+ final JSONObject quote = json.getJSONObject("Global Quote");
+
+ if (quote.isEmpty()) {
+ messages.add(new ErrorMessage("Invalid symbol."));
+ return messages;
+ }
+
+ messages.add(
+ new PublicMessage("Symbol: " + Utils.unescapeXml(quote.getString("01. symbol"))));
+ messages.add(
+ new PublicMessage(" Price: " + Utils.unescapeXml(quote.getString("05. price"))));
+ messages.add(
+ new PublicMessage(" Previous: "
+ + Utils.unescapeXml(quote.getString("08. previous close"))));
+ messages.add(
+ new NoticeMessage(" Open: " + Utils.unescapeXml(quote.getString("02. open"))));
+ messages.add(
+ new NoticeMessage(" High: " + Utils.unescapeXml(quote.getString("03. high"))));
+ messages.add(
+ new NoticeMessage(" Low: " + Utils.unescapeXml(quote.getString("04. low"))));
+ messages.add(
+ new NoticeMessage(" Volume: " + Utils.unescapeXml(quote.getString("06. volume"))));
+ messages.add(
+ new NoticeMessage(" Latest: "
+ + Utils.unescapeXml(quote.getString("07. latest trading day"))));
+ messages.add(
+ new NoticeMessage(" Change: " + Utils.unescapeXml(quote.getString("09. change"))
+ + " [" + Utils.unescapeXml(quote.getString("10. change percent")) + ']'));
}
-
- final JSONObject quote = json.getJSONObject("Global Quote");
-
- if (quote.isEmpty()) {
- messages.add(new ErrorMessage("Invalid symbol."));
- return messages;
- }
-
- messages.add(
- new PublicMessage("Symbol: " + Utils.unescapeXml(quote.getString("01. symbol"))));
- messages.add(
- new PublicMessage(" Price: " + Utils.unescapeXml(quote.getString("05. price"))));
- messages.add(
- new PublicMessage(" Previous: "
- + Utils.unescapeXml(quote.getString("08. previous close"))));
- messages.add(
- new NoticeMessage(" Open: " + Utils.unescapeXml(quote.getString("02. open"))));
- messages.add(
- new NoticeMessage(" High: " + Utils.unescapeXml(quote.getString("03. high"))));
- messages.add(
- new NoticeMessage(" Low: " + Utils.unescapeXml(quote.getString("04. low"))));
- messages.add(
- new NoticeMessage(" Volume: " + Utils.unescapeXml(quote.getString("06. volume"))));
- messages.add(
- new NoticeMessage(" Latest: "
- + Utils.unescapeXml(quote.getString("07. latest trading day"))));
- messages.add(
- new NoticeMessage(" Change: " + Utils.unescapeXml(quote.getString("09. change"))
- + " [" + Utils.unescapeXml(quote.getString("10. change percent")) + ']'));
+ } catch (IOException e) {
+ throw new ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e);
}
- } catch (IOException e) {
- throw new ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e);
- }
- return messages;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
- if (args.length() > 0) {
- new Thread(() -> run(bot, sender, args)).start();
+ return messages;
} else {
- helpResponse(bot, sender, args, isPrivate);
+ throw new ModuleException("Invalid symbol.");
}
+
}
/**
@@ -168,17 +165,19 @@ public final class StockQuote extends AbstractModule {
/**
* Returns the specified stock quote from Alpha Advantage.
*/
- private void run(final Mobibot bot, final String sender, final String symbol) {
- try {
- final ArrayList messages =
- getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
- for (Message msg : messages) {
- bot.send(msg.isNotice() ? sender : bot.getChannel(), msg.getMessage());
+ void run(final Mobibot bot, final String sender, final String symbol) {
+ if (Utils.isValidString(symbol)) {
+ try {
+ final ArrayList messages = getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
+ for (final Message msg : messages) {
+ bot.send(msg.isNotice() ? sender : bot.getChannel(), msg.getMessage());
+ }
+ } catch (ModuleException e) {
+ bot.getLogger().warn(e.getDebugMessage(), e);
+ bot.send(bot.getChannel(), e.getMessage());
}
-
- } catch (ModuleException e) {
- bot.getLogger().warn(e.getDebugMessage(), e);
- bot.send(sender, "An error has occurred retrieving a stock quote.");
+ } else {
+ helpResponse(bot, sender, symbol, true);
}
}
}
diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java
index 2c9a78a..0922f1e 100644
--- a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java
+++ b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java
@@ -163,7 +163,7 @@ public final class WorldTime extends AbstractModule {
return String.format("%c%03d", '@', beats);
}
- static Message worldTime(String query) {
+ static Message worldTime(final String query) {
final String tz = (COUNTRIES_MAP.get((query.substring(query.indexOf(' ') + 1).trim().toUpperCase())));
final String response;