From 6e8726ea55de12c09e2109df3338263b52616571 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 19 Sep 2022 01:36:27 -0700 Subject: [PATCH] Moved to JokeAPI --- config/detekt/baseline.xml | 1 + .../net/thauvin/erik/mobibot/modules/Joke.kt | 28 +++++++++++++------ .../thauvin/erik/mobibot/modules/JokeTest.kt | 13 +++++---- website/index.html | 2 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 8cc68a2..88c0a0c 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -50,6 +50,7 @@ NestedBlockDepth:EntryLink.kt$EntryLink$private fun setTags(tags: List<String?>) NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$@JvmStatic @Throws(IOException::class, FeedException::class) fun loadFeed(entries: Entries, currentFile: String = currentXml): String NestedBlockDepth:FeedsMgr.kt$FeedsMgr.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.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message> NestedBlockDepth:LinksMgr.kt$LinksMgr$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) NestedBlockDepth:Lookup.kt$Lookup$override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) 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 bc89d75..556580a 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Joke.kt @@ -34,14 +34,15 @@ package net.thauvin.erik.mobibot.modules import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.thauvin.erik.mobibot.Utils.bot -import net.thauvin.erik.mobibot.Utils.cyan +import net.thauvin.erik.mobibot.Utils.colorize import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.reader -import net.thauvin.erik.mobibot.Utils.sendMessage +import net.thauvin.erik.mobibot.msg.ErrorMessage import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.PublicMessage import org.json.JSONException import org.json.JSONObject +import org.pircbotx.Colors import org.pircbotx.hooks.types.GenericMessageEvent import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -63,16 +64,18 @@ class Joke : ThreadedModule() { } /** - * Returns a random joke from [Geek-Jokes](https://geek-jokes.sameerkumar.website/). + * Returns a random joke from [JokeAPI](https://sv443.net/jokeapi/v2/). */ override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) { with(event.bot()) { try { - sendIRC().notice(channel, randomJoke().msg.cyan()) + randomJoke().forEach { + sendIRC().notice(channel, it.msg.colorize(it.color)) + } } catch (e: ModuleException) { if (logger.isWarnEnabled) logger.warn(e.debugMessage, e) e.message?.let { - event.sendMessage(it) + event.respond(it) } } } @@ -84,18 +87,27 @@ class Joke : ThreadedModule() { // ICNDB URL private const val JOKE_URL = - "https://geek-jokes.sameerkumar.website/api?format=json" + "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit&type=single" /** * Retrieves a random joke. */ @JvmStatic @Throws(ModuleException::class) - fun randomJoke(): Message { + fun randomJoke(): List { return try { + val messages = mutableListOf() val url = URL(JOKE_URL) val json = JSONObject(url.reader().body) - PublicMessage(json.getString("joke")) + 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)) + } + messages } catch (e: IOException) { throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e) } catch (e: JSONException) { 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 56355a8..be27645 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt @@ -31,11 +31,12 @@ */ package net.thauvin.erik.mobibot.modules -import assertk.all import assertk.assertThat -import assertk.assertions.endsWith +import assertk.assertions.isGreaterThan +import assertk.assertions.isInstanceOf import assertk.assertions.isNotEmpty import net.thauvin.erik.mobibot.modules.Joke.Companion.randomJoke +import net.thauvin.erik.mobibot.msg.PublicMessage import org.testng.annotations.Test /** @@ -45,9 +46,11 @@ class JokeTest { @Test(groups = ["modules"]) @Throws(ModuleException::class) fun testRandomJoke() { - assertThat(randomJoke().msg, "randomJoke() > 0").all { - isNotEmpty() - endsWith(".") + val joke = randomJoke() + assertThat(joke.size, "joke is empty").isGreaterThan(0) + joke.forEach { + assertThat(it, "is not a public message").isInstanceOf(PublicMessage::class.java) + assertThat(it.msg, "msg is empty").isNotEmpty() } } } diff --git a/website/index.html b/website/index.html index 8637767..774eaff 100644 --- a/website/index.html +++ b/website/index.html @@ -112,7 +112,7 @@
  • Viewing when a nickname was last seen
    /msg mobibot seen nickname
  • -
  • Random jokes from Geek-Jokes +
  • Random jokes from JokeAPI
    mobibot: joke
  • Rolling dice or Playing war and rock paper scissors