Switched to JokeAPI library
This commit is contained in:
parent
b9deb55de6
commit
9bb7b34c69
5 changed files with 22 additions and 20 deletions
|
@ -80,6 +80,7 @@ dependencies {
|
||||||
|
|
||||||
// Thauvin
|
// Thauvin
|
||||||
implementation 'net.thauvin.erik:cryptoprice:1.0.0'
|
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'
|
implementation 'net.thauvin.erik:pinboard-poster:1.0.3'
|
||||||
|
|
||||||
testImplementation 'com.willowtreeapps.assertk:assertk-jvm:0.25'
|
testImplementation 'com.willowtreeapps.assertk:assertk-jvm:0.25'
|
||||||
|
|
|
@ -70,6 +70,7 @@
|
||||||
<ID>SwallowedException:StockQuoteTest.kt$StockQuoteTest$e: ModuleException</ID>
|
<ID>SwallowedException:StockQuoteTest.kt$StockQuoteTest$e: ModuleException</ID>
|
||||||
<ID>SwallowedException:WolframAlphaTest.kt$WolframAlphaTest$e: ModuleException</ID>
|
<ID>SwallowedException:WolframAlphaTest.kt$WolframAlphaTest$e: ModuleException</ID>
|
||||||
<ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message></ID>
|
<ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message></ID>
|
||||||
|
<ID>ThrowsCount:Joke.kt$Joke.Companion$@JvmStatic @Throws(ModuleException::class) fun randomJoke(): List<Message></ID>
|
||||||
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List<Message></ID>
|
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List<Message></ID>
|
||||||
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID>
|
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID>
|
||||||
<ID>ThrowsCount:Weather2.kt$Weather2.Companion$@JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List<Message></ID>
|
<ID>ThrowsCount:Weather2.kt$Weather2.Companion$@JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List<Message></ID>
|
||||||
|
|
|
@ -33,6 +33,11 @@ package net.thauvin.erik.mobibot.modules
|
||||||
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
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.bot
|
||||||
import net.thauvin.erik.mobibot.Utils.colorize
|
import net.thauvin.erik.mobibot.Utils.colorize
|
||||||
import net.thauvin.erik.mobibot.Utils.helpFormat
|
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) {
|
override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
|
||||||
with(event.bot()) {
|
with(event.bot()) {
|
||||||
|
@ -85,10 +90,6 @@ class Joke : ThreadedModule() {
|
||||||
// Joke command
|
// Joke command
|
||||||
private const val JOKE_CMD = "joke"
|
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.
|
* Retrieves a random joke.
|
||||||
*/
|
*/
|
||||||
|
@ -97,28 +98,26 @@ class Joke : ThreadedModule() {
|
||||||
fun randomJoke(): List<Message> {
|
fun randomJoke(): List<Message> {
|
||||||
return try {
|
return try {
|
||||||
val messages = mutableListOf<Message>()
|
val messages = mutableListOf<Message>()
|
||||||
val url = URL(JOKE_URL)
|
val joke = getJoke(safe = true, type = Type.SINGLE)
|
||||||
val json = JSONObject(url.reader().body)
|
joke.joke.forEach {
|
||||||
if (json.has("joke")) {
|
|
||||||
val joke = json.getString("joke").split("\n")
|
|
||||||
joke.forEach {
|
|
||||||
messages.add(PublicMessage(it, Colors.CYAN))
|
messages.add(PublicMessage(it, Colors.CYAN))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
messages.add(ErrorMessage(json.getString("message"), Colors.RED))
|
|
||||||
}
|
|
||||||
messages
|
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) {
|
} catch (e: IOException) {
|
||||||
throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e)
|
throw ModuleException("randomJoke(): IOE", "An IO error has occurred retrieving a random joke.", e)
|
||||||
} catch (e: JSONException) {
|
} 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 {
|
init {
|
||||||
commands.add(JOKE_CMD)
|
commands.add(JOKE_CMD)
|
||||||
help.add("To retrieve a random joke:")
|
help.add("To display a random joke:")
|
||||||
help.add(helpFormat("%c $JOKE_CMD"))
|
help.add(helpFormat("%c $JOKE_CMD"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,10 +47,10 @@ class JokeTest {
|
||||||
@Throws(ModuleException::class)
|
@Throws(ModuleException::class)
|
||||||
fun testRandomJoke() {
|
fun testRandomJoke() {
|
||||||
val joke = randomJoke()
|
val joke = randomJoke()
|
||||||
assertThat(joke.size, "joke is empty").isGreaterThan(0)
|
assertThat(joke.size, "joke should not be empty").isGreaterThan(0)
|
||||||
joke.forEach {
|
joke.forEach {
|
||||||
assertThat(it, "is not a public message").isInstanceOf(PublicMessage::class.java)
|
assertThat(it, "message should be public").isInstanceOf(PublicMessage::class.java)
|
||||||
assertThat(it.msg, "msg is empty").isNotEmpty()
|
assertThat(it.msg, "message should not be empty").isNotEmpty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
<li><a href="https://commons.apache.org/proper/commons-net/">Apache Commons Net</a></li>
|
<li><a href="https://commons.apache.org/proper/commons-net/">Apache Commons Net</a></li>
|
||||||
<li><a href="https://github.com/ethauvin/cryptoprice">CryptoPrice</a></li>
|
<li><a href="https://github.com/ethauvin/cryptoprice">CryptoPrice</a></li>
|
||||||
<li><a href="https://www.objecthunter.net/exp4j/">exp4j</a></li>
|
<li><a href="https://www.objecthunter.net/exp4j/">exp4j</a></li>
|
||||||
|
<li><a href="https://github.com/ethauvin/jokeapi">JokeAPI</a></li>
|
||||||
<li><a href="https://jsoup.org/">jsoup</a></li>
|
<li><a href="https://jsoup.org/">jsoup</a></li>
|
||||||
<li><a href="https://github.com/Kotlin/kotlinx-cli">kotlinx-cli</a></li>
|
<li><a href="https://github.com/Kotlin/kotlinx-cli">kotlinx-cli</a></li>
|
||||||
<li><a href="https://square.github.io/okhttp/">OkHttp</a></li>
|
<li><a href="https://square.github.io/okhttp/">OkHttp</a></li>
|
||||||
|
@ -112,7 +113,7 @@
|
||||||
<li>Viewing when a nickname was last seen
|
<li>Viewing when a nickname was last seen
|
||||||
<div><code>/msg mobibot seen nickname</code></div>
|
<div><code>/msg mobibot seen nickname</code></div>
|
||||||
</li>
|
</li>
|
||||||
<li>Random jokes from <a href="https://sv443.net/jokeapi/v2/">JokeAPI</a>
|
<li>Random jokes from <a href="https://v2.jokeapi.dev/">Sv443's JokeAPI</a>
|
||||||
<div><code>mobibot: joke</code></div>
|
<div><code>mobibot: joke</code></div>
|
||||||
</li>
|
</li>
|
||||||
<li>Rolling dice or Playing war and rock paper scissors
|
<li>Rolling dice or Playing war and rock paper scissors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue