diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 33a9f22..19b4dbd 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -54,7 +54,7 @@ NestedBlockDepth:EntryLink.kt$EntryLink$private fun setTags(tags: List<String?>) NestedBlockDepth:FeedsManager.kt$FeedsManager.Companion$@JvmStatic @Throws(IOException::class, FeedException::class) fun loadFeed(entries: Entries, currentFile: String = currentXml): String NestedBlockDepth:FeedsManager.kt$FeedsManager.Companion$@JvmStatic fun saveFeed(entries: Entries, currentFile: String = currentXml) - NestedBlockDepth:GoogleSearch.kt$GoogleSearch$override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) + NestedBlockDepth:GoogleSearch.kt$GoogleSearch$override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) NestedBlockDepth:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message> NestedBlockDepth:LinksManager.kt$LinksManager$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) NestedBlockDepth:Lookup.kt$Lookup$override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) @@ -66,7 +66,7 @@ NestedBlockDepth:TwitterOAuth.kt$TwitterOAuth$@JvmStatic fun main(args: Array<String>) NestedBlockDepth:Utils.kt$Utils$@JvmStatic fun loadData(file: String, default: Any, logger: Logger, description: String): Any NestedBlockDepth:Utils.kt$Utils$@JvmStatic fun saveData(file: String, data: Any, logger: Logger, description: String) - NestedBlockDepth:Weather2.kt$Weather2$override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) + NestedBlockDepth:Weather2.kt$Weather2$override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) NestedBlockDepth:Weather2.kt$Weather2.Companion$@JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List<Message> PrintStackTrace:TwitterOAuth.kt$TwitterOAuth$ioe PrintStackTrace:TwitterOAuth.kt$TwitterOAuth$te diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt b/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt index eb00137..fc43dbc 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt @@ -65,7 +65,7 @@ class FeedReader(private val url: String, val event: GenericMessageEvent) : Runn event.sendMessage("An error has occurred while parsing the feed: ${e.message}") } catch (e: IOException) { if (logger.isWarnEnabled) logger.warn("Unable to fetch the feed at $url", e) - event.sendMessage("An error has occurred while fetching the feed: ${e.message}") + event.sendMessage("An IO error has occurred while fetching the feed: ${e.message}") } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt index bae8403..fcd2d08 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt @@ -176,7 +176,7 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro override fun onDisconnect(event: DisconnectEvent?) { event?.let { with(event.getBot()) { - LinksManager.socialManager.notification("$nick disconnected from irc://$serverHostname") + LinksManager.socialManager.notification("$nick disconnected from $serverHostname") seen.add(userChannelDao.getChannel(channel).users) } } @@ -202,7 +202,7 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro with(event.getBot()) { if (user.nick == nick) { LinksManager.socialManager.notification( - "$nick has joined ${event.channel.name} on irc://$serverHostname" + "$nick has joined ${event.channel.name} on $serverHostname" ) seen.add(userChannelDao.getChannel(channel).users) } else { @@ -215,12 +215,10 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro override fun onMessage(event: MessageEvent?) { event?.user?.let { user -> - val sender = user.nick - val message = event.message tell.send(event) - if (message.matches("(?i)${Pattern.quote(event.bot().nick)}:.*".toRegex())) { // mobibot: - if (logger.isTraceEnabled) logger.trace(">>> $sender: $message") - val cmds = message.substring(message.indexOf(':') + 1).trim().split(" ".toRegex(), 2) + if (event.message.matches("(?i)${Pattern.quote(event.bot().nick)}:.*".toRegex())) { // mobibot: + if (logger.isTraceEnabled) logger.trace(">>> ${user.nick}: ${event.message}") + val cmds = event.message.substring(event.bot().nick.length + 1).trim().split(" ".toRegex(), 2) val cmd = cmds[0].lowercase() val args = cmds.lastOrEmpty().trim() if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help @@ -230,10 +228,10 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro addons.exec(channel, cmd, args, event) } } else if (addons.match(channel, event)) { // Links, e.g.: https://www.example.com/ or L1: , etc. - if (logger.isTraceEnabled) logger.trace(">>> $sender: $message") + if (logger.isTraceEnabled) logger.trace(">>> ${user.nick}: ${event.message}") } - storeRecap(sender, message, false) - seen.add(sender) + storeRecap(user.nick, event.message, false) + seen.add(user.nick) } } @@ -252,7 +250,7 @@ class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Pro with(event.getBot()) { if (user.nick == nick) { LinksManager.socialManager.notification( - "$nick has left ${event.channel.name} on irc://$serverHostname" + "$nick has left ${event.channel.name} on $serverHostname" ) seen.add(userChannelDao.getChannel(channel).users) } else { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Pinboard.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Pinboard.kt index ee9b020..1bc719b 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Pinboard.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Pinboard.kt @@ -32,8 +32,6 @@ package net.thauvin.erik.mobibot -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.pinboard.PinboardPoster import java.time.ZoneId @@ -53,12 +51,8 @@ class Pinboard { */ fun addPin(ircServer: String, entry: EntryLink) { if (poster.apiToken.isNotBlank()) { - runBlocking { - launch { - with(entry) { - poster.addPin(link, title, postedBy(ircServer), formatTags(), date.toTimestamp()) - } - } + with(entry) { + poster.addPin(link, title, postedBy(ircServer), formatTags(), date.toTimestamp()) } } } @@ -75,12 +69,9 @@ class Pinboard { */ fun deletePin(entry: EntryLink) { if (poster.apiToken.isNotBlank()) { - runBlocking { - launch { - poster.deletePin(entry.link) - } - } + poster.deletePin(entry.link) } + } /** @@ -88,15 +79,11 @@ class Pinboard { */ fun updatePin(ircServer: String, oldUrl: String, entry: EntryLink) { if (poster.apiToken.isNotBlank()) { - runBlocking { - launch { - with(entry) { - if (oldUrl != link) { - poster.deletePin(oldUrl) - } - poster.addPin(link, title, postedBy(ircServer), formatTags(), date.toTimestamp()) - } + with(entry) { + if (oldUrl != link) { + poster.deletePin(oldUrl) } + poster.addPin(link, title, postedBy(ircServer), formatTags(), date.toTimestamp()) } } } @@ -106,8 +93,7 @@ class Pinboard { */ private fun Date.toTimestamp(): String { return ZonedDateTime.ofInstant( - toInstant().truncatedTo(ChronoUnit.SECONDS), - ZoneId.systemDefault() + toInstant().truncatedTo(ChronoUnit.SECONDS), ZoneId.systemDefault() ).format(DateTimeFormatter.ISO_INSTANT) } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt index 3cc1b1a..8412af0 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/ChannelFeed.kt @@ -32,8 +32,6 @@ package net.thauvin.erik.mobibot.commands -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.thauvin.erik.mobibot.FeedReader import net.thauvin.erik.mobibot.Utils.helpFormat import org.pircbotx.hooks.types.GenericMessageEvent @@ -55,11 +53,7 @@ class ChannelFeed(channel: String) : AbstractCommand() { override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) { if (isEnabled()) { - runBlocking { - launch { - properties[FEED_PROP]?.let { FeedReader(it, event).run() } - } - } + properties[FEED_PROP]?.let { FeedReader(it, event).run() } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Cycle.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Cycle.kt index 20a49d2..31a9c65 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Cycle.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Cycle.kt @@ -33,6 +33,7 @@ package net.thauvin.erik.mobibot.commands import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.helpFormat @@ -51,10 +52,12 @@ class Cycle : AbstractCommand() { with(event.bot()) { if (event.isChannelOp(channel)) { runBlocking { - sendIRC().message(channel, "${event.user.nick} asked me to leave. I'll be back!") - userChannelDao.getChannel(channel).send().part() - delay(wait * 1000L) - sendIRC().joinChannel(channel) + launch { + sendIRC().message(channel, "${event.user.nick} asked me to leave. I'll be back!") + userChannelDao.getChannel(channel).send().part() + delay(wait * 1000L) + sendIRC().joinChannel(channel) + } } } else { helpResponse(channel, args, event) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt index 5d03cb8..f47f057 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Ignore.kt @@ -141,7 +141,7 @@ class Ignore : AbstractCommand() { override fun setProperty(key: String, value: String) { super.setProperty(key, value) if (IGNORE_PROP == key) { - ignored.addAll(value.split(LinksManager.TAG_MATCH) + ignored.addAll(value.split(LinksManager.TAG_MATCH)) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt index fddb9be..66c4ebf 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/Users.kt @@ -45,15 +45,7 @@ class Users : AbstractCommand() { override val isVisible = true override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) { - val nicks = mutableListOf() val ch = event.bot().userChannelDao.getChannel(channel) - ch.users.forEach { - if (it.channelsOpIn.contains(ch)) { - nicks.add("@${it.nick}") - } else { - nicks.add(it.nick) - } - } - event.sendList(nicks, 8) + event.sendList(ch.users.map { if (it.channelsOpIn.contains(ch)) "@${it.nick}" else it.nick }, 8) } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt index a504c92..7d8aaed 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt @@ -37,7 +37,7 @@ import java.time.LocalDateTime import java.time.format.DateTimeFormatter /** - * The `TellMessage` class. + * Tell Message. */ class TellMessage( /** diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt index 40021e3..c464928 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ChatGpt.kt @@ -46,12 +46,12 @@ import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse -class ChatGpt : ThreadedModule() { +class ChatGpt : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(ChatGpt::class.java) override val name = "ChatGPT" - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { event.sendMessage( diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt index 7f9e4b7..66a33ae 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CryptoPrices.kt @@ -46,7 +46,7 @@ import java.io.IOException /** * The Cryptocurrency Prices module. */ -class CryptoPrices : ThreadedModule() { +class CryptoPrices : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(CryptoPrices::class.java) override val name = "CryptoPrices" @@ -55,7 +55,7 @@ class CryptoPrices : ThreadedModule() { * Returns the cryptocurrency market price from * [Coinbase](https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-prices#get-spot-price). */ - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (CURRENCIES.isEmpty()) { try { loadCurrencies() diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index 712aedf..3d0bf0a 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -52,7 +52,7 @@ import java.util.TreeMap /** * The CurrencyConverter module. */ -class CurrencyConverter : ThreadedModule() { +class CurrencyConverter : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(CurrencyConverter::class.java) override val name = "CurrencyConverter" @@ -71,7 +71,7 @@ class CurrencyConverter : ThreadedModule() { /** * Converts the specified currencies. */ - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { reload() if (SYMBOLS.isEmpty()) { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index 7499ec8..76451db 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -54,7 +54,7 @@ import java.net.URL /** * The GoogleSearch module. */ -class GoogleSearch : ThreadedModule() { +class GoogleSearch : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(GoogleSearch::class.java) override val name = "GoogleSearch" @@ -62,7 +62,7 @@ class GoogleSearch : ThreadedModule() { /** * Searches Google. */ - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { val results = searchGoogle( diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt index e85a914..202dc68 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt @@ -31,8 +31,6 @@ */ package net.thauvin.erik.mobibot.modules -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.thauvin.erik.jokeapi.exceptions.HttpErrorException import net.thauvin.erik.jokeapi.exceptions.JokeException import net.thauvin.erik.jokeapi.getJoke @@ -52,21 +50,15 @@ import java.io.IOException /** * The Joke module. */ -class Joke : ThreadedModule() { +class Joke : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(Joke::class.java) override val name = "Joke" - override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { - runBlocking { - launch { run(channel, cmd, args, event) } - } - } - /** * Returns a random joke from [JokeAPI](https://v2.jokeapi.dev/). */ - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { with(event.bot()) { try { randomJoke().forEach { @@ -92,12 +84,8 @@ class Joke : ThreadedModule() { @Throws(ModuleException::class) fun randomJoke(): List { return try { - val messages = mutableListOf() val joke = getJoke(safe = true, type = Type.SINGLE, splitNewLine = true) - joke.joke.forEach { - messages.add(PublicMessage(it, Colors.CYAN)) - } - messages + joke.joke.map { PublicMessage(it, Colors.CYAN) } } catch (e: JokeException) { throw ModuleException("randomJoke(): ${e.additionalInfo}", e.message, e) } catch (e: HttpErrorException) { diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Mastodon.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Mastodon.kt index b355fdc..ae805cd 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Mastodon.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Mastodon.kt @@ -93,7 +93,7 @@ class Mastodon : SocialModule() { private const val MASTODON_CMD = "mastodon" /** - * Toots on Mastodon. + * Post on Mastodon. */ @JvmStatic @Throws(ModuleException::class) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index 4c2859f..ffca08b 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -52,7 +52,7 @@ import java.net.URL /** * The StockQuote module. */ -class StockQuote : ThreadedModule() { +class StockQuote : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(StockQuote::class.java) override val name = "StockQuote" @@ -60,7 +60,7 @@ class StockQuote : ThreadedModule() { /** * Returns the specified stock quote from Alpha Vantage. */ - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { val messages = getQuote(args, properties[ALPHAVANTAGE_API_KEY_PROP]) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ThreadedModule.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ThreadedModule.kt deleted file mode 100644 index 269b24d..0000000 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ThreadedModule.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * ThreadedModule.kt - * - * Copyright (c) 2004-2022, Erik C. Thauvin (erik@thauvin.net) - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of this project nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package net.thauvin.erik.mobibot.modules - -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking -import org.pircbotx.hooks.types.GenericMessageEvent - -/** - * The `ThreadedModule` class. - */ -abstract class ThreadedModule : AbstractModule() { - override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { - if (isEnabled && event.message.isNotEmpty()) { - runBlocking { - launch { - run(channel, cmd, args, event) - } - } - } else { - helpResponse(event) - } - } - - /** - * Runs the thread. - */ - abstract fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) -} diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt index 2253494..28f9cde 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Twitter.kt @@ -88,7 +88,7 @@ class Twitter : SocialModule() { private const val TWITTER_CMD = "twitter" /** - * Tweets on Twitter. + * Post on Twitter. */ @JvmStatic @Throws(ModuleException::class) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt index 8dcdc15..5c5ae67 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -54,7 +54,7 @@ import kotlin.math.roundToInt /** * The `Weather2` module. */ -class Weather2 : ThreadedModule() { +class Weather2 : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(Weather2::class.java) override val name = "Weather" @@ -62,7 +62,7 @@ class Weather2 : ThreadedModule() { /** * Fetches the weather data from a specific city. */ - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { val messages = getWeather(args, properties[OWM_API_KEY_PROP]) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt index 1d5abd3..56d3a3d 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WolframAlpha.kt @@ -43,7 +43,7 @@ import org.slf4j.LoggerFactory import java.io.IOException import java.net.URL -class WolframAlpha : ThreadedModule() { +class WolframAlpha : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(WolframAlpha::class.java) override val name = "WolframAlpha" @@ -56,7 +56,7 @@ class WolframAlpha : ThreadedModule() { } } - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { if (args.isNotBlank()) { try { val query = args.trim().split("units=", limit = 2, ignoreCase = true) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialManager.kt b/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialManager.kt index c991140..f3a86fe 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialManager.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialManager.kt @@ -110,8 +110,8 @@ class SocialManager { */ fun shutdown() { timer.cancel() - for (index in entries) { - postEntry(index) + entries.forEach { + postEntry(it) } } } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialModule.kt b/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialModule.kt index c15dd55..e31ccdc 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialModule.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/social/SocialModule.kt @@ -32,18 +32,16 @@ package net.thauvin.erik.mobibot.social -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.thauvin.erik.mobibot.commands.links.LinksManager import net.thauvin.erik.mobibot.entries.EntriesUtils.toLinkLabel import net.thauvin.erik.mobibot.entries.EntryLink +import net.thauvin.erik.mobibot.modules.AbstractModule import net.thauvin.erik.mobibot.modules.ModuleException -import net.thauvin.erik.mobibot.modules.ThreadedModule import org.pircbotx.hooks.types.GenericMessageEvent import org.slf4j.Logger import org.slf4j.LoggerFactory -abstract class SocialModule : ThreadedModule() { +abstract class SocialModule : AbstractModule() { private val logger: Logger = LoggerFactory.getLogger(SocialManager::class.java) abstract val handle: String? @@ -56,15 +54,11 @@ abstract class SocialModule : ThreadedModule() { */ fun notification(msg: String) { if (isEnabled && !handle.isNullOrBlank()) { - runBlocking { - launch { - try { - post(message = msg, isDm = true) - if (logger.isDebugEnabled) logger.debug("Notified $handle on $name: $msg") - } catch (e: ModuleException) { - if (logger.isWarnEnabled) logger.warn("Failed to notify $handle on $name: $msg", e) - } - } + try { + post(message = msg, isDm = true) + if (logger.isDebugEnabled) logger.debug("Notified $handle on $name: $msg") + } catch (e: ModuleException) { + if (logger.isWarnEnabled) logger.warn("Failed to notify $handle on $name: $msg", e) } } } @@ -76,25 +70,21 @@ abstract class SocialModule : ThreadedModule() { */ fun postEntry(index: Int) { if (isAutoPost && LinksManager.entries.links.size >= index) { - runBlocking { - launch { - try { - if (logger.isDebugEnabled) { - logger.debug("Posting {} to $name.", index.toLinkLabel()) - } - post(message = formatEntry(LinksManager.entries.links[index]), isDm = false) - } catch (e: ModuleException) { - if (logger.isWarnEnabled) logger.warn( - "Failed to post entry ${index.toLinkLabel()} on $name.", - e - ) - } + try { + if (logger.isDebugEnabled) { + logger.debug("Posting {} to $name.", index.toLinkLabel()) } + post(message = formatEntry(LinksManager.entries.links[index]), isDm = false) + } catch (e: ModuleException) { + if (logger.isWarnEnabled) logger.warn( + "Failed to post entry ${index.toLinkLabel()} on $name.", + e + ) } } } - override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { + override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) { try { event.respond(post("$args (by ${event.user.nick} on $channel)", false)) } catch (e: ModuleException) { diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt index b36d167..8c1a862 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt @@ -40,7 +40,6 @@ import assertk.assertions.isFalse import assertk.assertions.isTrue import assertk.assertions.prop import assertk.assertions.size -import assertk.assertions.startsWith import com.rometools.rome.feed.synd.SyndCategory import com.rometools.rome.feed.synd.SyndCategoryImpl import org.testng.annotations.Test diff --git a/version.properties b/version.properties index edb9391..4eb40f3 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Wed Dec 07 02:53:01 PST 2022 -version.buildmeta=857 +#Sat Dec 10 10:16:56 PST 2022 +version.buildmeta=874 version.major=0 version.minor=8 version.patch=0 version.prerelease=rc version.project=mobibot -version.semver=0.8.0-rc+857 +version.semver=0.8.0-rc+874