From a4907ce111e3b353e275f8bd0797be40de273a52 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 9 Apr 2019 23:04:38 -0700 Subject: [PATCH] Implemented new send methods. --- .../net/thauvin/erik/mobibot/Mobibot.java | 42 +++++++++++++++++-- .../net/thauvin/erik/mobibot/Pinboard.java | 17 ++++---- .../thauvin/erik/mobibot/modules/Calc.java | 2 +- .../mobibot/modules/CurrencyConverter.java | 2 +- .../erik/mobibot/modules/GoogleSearch.java | 13 ++---- .../thauvin/erik/mobibot/modules/Joke.java | 2 +- .../erik/mobibot/modules/StockQuote.java | 4 +- .../net/thauvin/erik/mobibot/modules/War.java | 2 +- .../erik/mobibot/modules/Weather2.java | 4 +- .../erik/mobibot/modules/WorldTime.java | 2 +- 10 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index f7c7948..c0e9dc3 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -34,12 +34,14 @@ package net.thauvin.erik.mobibot; import com.rometools.rome.io.FeedException; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import net.thauvin.erik.mobibot.modules.*; +import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.semver.Version; import org.apache.commons.cli.*; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configurator; +import org.jibble.pircbot.Colors; import org.jibble.pircbot.PircBot; import org.jibble.pircbot.User; import org.jsoup.Jsoup; @@ -796,7 +798,7 @@ public class Mobibot extends PircBot { private void infoResponse(final String sender, final boolean isPrivate) { for (final String info : INFO_STRS) { if (info.startsWith("https://")) { - send(sender, Utils.green(info), isPrivate); + send(sender, info, Colors.DARK_GREEN, isPrivate); } else { send(sender, info, isPrivate); } @@ -1429,20 +1431,54 @@ public class Mobibot extends PircBot { } } + /** + * Sends a notice to the channel. + * + * @param notice The notice message. + */ + public final void send(final String notice) { + send(getChannel(), notice); + + } + /** * Sends a message. * - * @param who The channel or nick of the person who sent the message. + * @param who The channel or nick of the person who sent the command. * @param message The actual message. */ public final void send(final String who, final String message) { send(who, message, false); } + /** * Sends a message. * - * @param who The channel or nick of the person who sent the message. + * @param who The channel or nick of the person who sent the command. + * @param message The message. + */ + public final void send(final String who, final Message message) { + send(message.isNotice() ? who : getChannel(), message.getMessage(), message.getColor(), message.isPrivate()); + } + + /** + * Sends a message. + * + * @param who The channel or nick of the person who sent the command. + * @param message The actual message. + * @param color The message's color. + * @param isPrivate The private flag. + */ + public final void send(final String who, final String message, final String color, boolean isPrivate) { + send(who, Utils.colorize(message, color), isPrivate); + } + + + /** + * Sends a message. + * + * @param who The channel or nick of the person who sent the command. * @param message The actual message. * @param color The message's color. */ diff --git a/src/main/java/net/thauvin/erik/mobibot/Pinboard.java b/src/main/java/net/thauvin/erik/mobibot/Pinboard.java index f8e9b61..5682456 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Pinboard.java +++ b/src/main/java/net/thauvin/erik/mobibot/Pinboard.java @@ -60,7 +60,7 @@ class Pinboard { * @param apiToken The API end point. * @param ircServer The IRC server. */ - public Pinboard(final Mobibot bot, final String apiToken, final String ircServer) { + Pinboard(final Mobibot bot, final String apiToken, final String ircServer) { pinboard = new PinboardPoster(apiToken); this.ircServer = ircServer; @@ -78,11 +78,10 @@ class Pinboard { * * @param entry The entry to add. */ - public final void addPost(final EntryLink entry) { + final void addPost(final EntryLink entry) { final SwingWorker worker = new SwingWorker() { @Override - protected Boolean doInBackground() - throws Exception { + protected Boolean doInBackground() { return pinboard.addPin(entry.getLink(), entry.getTitle(), postedBy(entry), @@ -99,13 +98,12 @@ class Pinboard { * * @param entry The entry to delete. */ - public final void deletePost(final EntryLink entry) { + final void deletePost(final EntryLink entry) { final String link = entry.getLink(); final SwingWorker worker = new SwingWorker() { @Override - protected Boolean doInBackground() - throws Exception { + protected Boolean doInBackground() { return pinboard.deletePin(link); } }; @@ -139,11 +137,10 @@ class Pinboard { * @param oldUrl The old post URL. * @param entry The entry to add. */ - public final void updatePost(final String oldUrl, final EntryLink entry) { + final void updatePost(final String oldUrl, final EntryLink entry) { final SwingWorker worker = new SwingWorker() { @Override - protected Boolean doInBackground() - throws Exception { + protected Boolean doInBackground() { if (!oldUrl.equals(entry.getLink())) { pinboard.deletePin(oldUrl); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java b/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java index 2c2a513..a862652 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Calc.java @@ -81,7 +81,7 @@ public class Calc extends AbstractModule { @Override public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { if (Utils.isValidString(args)) { - bot.send(bot.getChannel(), calc(args)); + bot.send(calc(args)); } else { helpResponse(bot, sender, args, isPrivate); 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 88a7f8f..3e9ac77 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/CurrencyConverter.java @@ -199,7 +199,7 @@ public final class CurrencyConverter extends ThreadedModule { if (msg.isError()) { helpResponse(bot, sender, CURRENCY_CMD + ' ' + query, false); } - bot.send(msg.isNotice() ? sender : bot.getChannel(), msg.getMessage()); + bot.send(sender, msg); } catch (ModuleException e) { bot.getLogger().warn(e.getDebugMessage(), e); bot.send(sender, e.getMessage()); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java index 1fd4e6c..9a4af37 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java @@ -36,6 +36,7 @@ import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Utils; import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.NoticeMessage; +import org.jibble.pircbot.Colors; import org.json.JSONArray; import org.json.JSONObject; @@ -118,7 +119,8 @@ public final class GoogleSearch extends ThreadedModule { for (int i = 0; i < ja.length(); i++) { final JSONObject j = ja.getJSONObject(i); results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title")))); - results.add(new NoticeMessage(j.getString("link"))); + results.add( + new NoticeMessage(TAB_INDENT + j.getString("link"), Colors.DARK_GREEN)); } } } catch (IOException e) { @@ -152,15 +154,8 @@ public final class GoogleSearch extends ThreadedModule { try { final ArrayList results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP), properties.get(GOOGLE_CSE_KEY_PROP)); - - int i = 0; for (final Message msg : results) { - if (i % 2 == 0) { - bot.send(sender, Utils.green(TAB_INDENT + msg.getMessage())); - } else { - bot.send(sender, msg.getMessage()); - } - i++; + bot.send(sender, msg); } } catch (ModuleException e) { bot.getLogger().warn(e.getDebugMessage(), e); 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 5f40429..5b01d17 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Joke.java @@ -108,7 +108,7 @@ public final class Joke extends ThreadedModule { */ void run(final Mobibot bot, final String sender, String arg) { try { - bot.send(bot.getChannel(), Utils.cyan(randomJoke().getMessage())); + bot.send(Utils.cyan(randomJoke().getMessage())); } catch (ModuleException e) { bot.getLogger().warn(e.getDebugMessage(), e); bot.send(sender, e.getMessage()); 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 e8f7a14..3805592 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.java @@ -170,11 +170,11 @@ public final class StockQuote extends ThreadedModule { 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()); + bot.send(sender, msg); } } catch (ModuleException e) { bot.getLogger().warn(e.getDebugMessage(), e); - bot.send(bot.getChannel(), e.getMessage()); + bot.send(e.getMessage()); } } else { helpResponse(bot, sender, symbol, true); diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/War.java b/src/main/java/net/thauvin/erik/mobibot/modules/War.java index 74a8f33..3f6349b 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/War.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/War.java @@ -86,7 +86,7 @@ public final class War extends AbstractModule { break; } - bot.send(bot.getChannel(), "This means " + Utils.bold("WAR") + '!'); + bot.send("This means " + Utils.bold("WAR") + '!'); } if (i < y) { diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java index 282210e..51c176e 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java @@ -197,12 +197,12 @@ public class Weather2 extends ThreadedModule { helpResponse(bot, sender, args, true); } else { for (final Message msg : messages) { - bot.send(msg.isNotice() ? bot.getChannel() : sender, msg.getMessage(), msg.getColor()); + bot.send(sender, msg); } } } catch (ModuleException e) { bot.getLogger().debug(e.getDebugMessage(), e); - bot.send(bot.getChannel(), e.getMessage()); + bot.send(e.getMessage()); } } else { helpResponse(bot, sender, args, 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 0922f1e..01842c5 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.java @@ -200,7 +200,7 @@ public final class WorldTime extends AbstractModule { if (msg.isError()) { bot.send(sender, msg.getMessage()); } else { - bot.send(bot.getChannel(), msg.getMessage()); + bot.send(msg.getMessage()); } } }