Now extends ThreadedModule
This commit is contained in:
parent
0ce160c905
commit
70f6781e56
4 changed files with 83 additions and 92 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 <a href="http://www.icndb.com/">The Internet Chuck Norris Database</a>
|
||||
*/
|
||||
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) {
|
||||
|
|
|
@ -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,7 +79,12 @@ public final class StockQuote extends AbstractModule {
|
|||
* @return The stock quote.
|
||||
* @throws ModuleException If an errors occurs.
|
||||
*/
|
||||
static ArrayList<Message> getQuote(String symbol, String apiKey) throws ModuleException {
|
||||
static ArrayList<Message> 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.");
|
||||
}
|
||||
|
||||
if (Utils.isValidString(symbol)) {
|
||||
final String debugMessage = "getQuote(" + symbol + ')';
|
||||
final ArrayList<Message> messages = new ArrayList<>();
|
||||
final OkHttpClient client = new OkHttpClient();
|
||||
|
@ -142,18 +147,10 @@ public final class StockQuote extends AbstractModule {
|
|||
throw new ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e);
|
||||
}
|
||||
return messages;
|
||||
} else {
|
||||
throw new ModuleException("Invalid symbol.");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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();
|
||||
} else {
|
||||
helpResponse(bot, sender, args, isPrivate);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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) {
|
||||
void run(final Mobibot bot, final String sender, final String symbol) {
|
||||
if (Utils.isValidString(symbol)) {
|
||||
try {
|
||||
final ArrayList<Message> messages =
|
||||
getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
|
||||
for (Message msg : messages) {
|
||||
final ArrayList<Message> 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(sender, "An error has occurred retrieving a stock quote.");
|
||||
bot.send(bot.getChannel(), e.getMessage());
|
||||
}
|
||||
} else {
|
||||
helpResponse(bot, sender, symbol, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue