Moved to JokeAPI

This commit is contained in:
Erik C. Thauvin 2022-09-19 01:36:27 -07:00
parent c025117f44
commit 6e8726ea55
4 changed files with 30 additions and 14 deletions

View file

@ -50,6 +50,7 @@
<ID>NestedBlockDepth:EntryLink.kt$EntryLink$private fun setTags(tags: List&lt;String?&gt;)</ID>
<ID>NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$@JvmStatic @Throws(IOException::class, FeedException::class) fun loadFeed(entries: Entries, currentFile: String = currentXml): String</ID>
<ID>NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$@JvmStatic fun saveFeed(entries: Entries, currentFile: String = currentXml)</ID>
<ID>NestedBlockDepth:GoogleSearch.kt$GoogleSearch$override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent)</ID>
<ID>NestedBlockDepth:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List&lt;Message&gt;</ID>
<ID>NestedBlockDepth:LinksMgr.kt$LinksMgr$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent)</ID>
<ID>NestedBlockDepth:Lookup.kt$Lookup$override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent)</ID>

View file

@ -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<Message> {
return try {
val messages = mutableListOf<Message>()
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) {

View file

@ -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()
}
}
}

View file

@ -112,7 +112,7 @@
<li>Viewing when a nickname was last seen
<div><code>/msg mobibot seen nickname</code></div>
</li>
<li>Random jokes from <a href="https://geek-jokes.sameerkumar.website/">Geek-Jokes</a>
<li>Random jokes from <a href="https://sv443.net/jokeapi/v2/">JokeAPI</a>
<div><code>mobibot: joke</code></div>
</li>
<li>Rolling dice or Playing war and rock paper scissors