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