diff --git a/build.gradle b/build.gradle index f8029a3..49d1823 100644 --- a/build.gradle +++ b/build.gradle @@ -80,6 +80,7 @@ dependencies { // Thauvin implementation 'net.thauvin.erik:cryptoprice:1.0.0' + implementation 'net.thauvin.erik:jokeapi:0.9-SNAPSHOT' implementation 'net.thauvin.erik:pinboard-poster:1.0.3' testImplementation 'com.willowtreeapps.assertk:assertk-jvm:0.25' diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 88c0a0c..75f6acf 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -70,6 +70,7 @@ SwallowedException:StockQuoteTest.kt$StockQuoteTest$e: ModuleException SwallowedException:WolframAlphaTest.kt$WolframAlphaTest$e: ModuleException ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message> + ThrowsCount:Joke.kt$Joke.Companion$@JvmStatic @Throws(ModuleException::class) fun randomJoke(): List<Message> ThrowsCount:StockQuote.kt$StockQuote.Companion$@JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List<Message> ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject ThrowsCount:Weather2.kt$Weather2.Companion$@JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List<Message> 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 556580a..1895269 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt @@ -33,6 +33,11 @@ package net.thauvin.erik.mobibot.modules import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import net.thauvin.erik.jokeapi.JokeApi +import net.thauvin.erik.jokeapi.JokeApi.Companion.getJoke +import net.thauvin.erik.jokeapi.exceptions.HttpErrorException +import net.thauvin.erik.jokeapi.exceptions.JokeException +import net.thauvin.erik.jokeapi.models.Type import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.colorize import net.thauvin.erik.mobibot.Utils.helpFormat @@ -64,7 +69,7 @@ class Joke : ThreadedModule() { } /** - * Returns a random joke from [JokeAPI](https://sv443.net/jokeapi/v2/). + * Returns a random joke from [JokeAPI](https://v2.jokeapi.dev/). */ override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { with(event.bot()) { @@ -85,10 +90,6 @@ class Joke : ThreadedModule() { // Joke command private const val JOKE_CMD = "joke" - // ICNDB URL - private const val JOKE_URL = - "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=single" - /** * Retrieves a random joke. */ @@ -97,28 +98,26 @@ class Joke : ThreadedModule() { fun randomJoke(): List { return try { val messages = mutableListOf() - val url = URL(JOKE_URL) - val json = JSONObject(url.reader().body) - if (json.has("joke")) { - val joke = json.getString("joke").split("\n") - joke.forEach { - messages.add(PublicMessage(it, Colors.CYAN)) - } - } else { - messages.add(ErrorMessage(json.getString("message"), Colors.RED)) + val joke = getJoke(safe = true, type = Type.SINGLE) + joke.joke.forEach { + messages.add(PublicMessage(it, Colors.CYAN)) } messages + } catch (e: JokeException) { + throw ModuleException("randomJoke(): ${e.additionalInfo}", e.message, e) + } catch (e: HttpErrorException) { + throw ModuleException("randomJoke(): HTTP: ${e.statusCode}", e.message, e) } catch (e: IOException) { throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e) } catch (e: JSONException) { - throw ModuleException("randomJoke(): JSON", "An JSON error has occurred retrieving a random joke.", e) + throw ModuleException("randomJoke(): JSON", "A parsing error has occurred retrieving a random joke.", e) } } } init { commands.add(JOKE_CMD) - help.add("To retrieve a random joke:") + help.add("To display a random joke:") help.add(helpFormat("%c $JOKE_CMD")) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt index be27645..ed921a3 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt @@ -47,10 +47,10 @@ class JokeTest { @Throws(ModuleException::class) fun testRandomJoke() { val joke = randomJoke() - assertThat(joke.size, "joke is empty").isGreaterThan(0) + assertThat(joke.size, "joke should not be empty").isGreaterThan(0) joke.forEach { - assertThat(it, "is not a public message").isInstanceOf(PublicMessage::class.java) - assertThat(it.msg, "msg is empty").isNotEmpty() + assertThat(it, "message should be public").isInstanceOf(PublicMessage::class.java) + assertThat(it.msg, "message should not be empty").isNotEmpty() } } } diff --git a/website/index.html b/website/index.html index 774eaff..6a2d139 100644 --- a/website/index.html +++ b/website/index.html @@ -39,6 +39,7 @@
  • Apache Commons Net
  • CryptoPrice
  • exp4j
  • +
  • JokeAPI
  • jsoup
  • kotlinx-cli
  • OkHttp
  • @@ -112,7 +113,7 @@
  • Viewing when a nickname was last seen
    /msg mobibot seen nickname
  • -
  • Random jokes from JokeAPI +
  • Random jokes from Sv443's JokeAPI
    mobibot: joke
  • Rolling dice or Playing war and rock paper scissors