64 lines
2.5 KiB
Kotlin
64 lines
2.5 KiB
Kotlin
package nl.voidcorp.dbot.commands
|
|
|
|
import com.github.salomonbrys.kotson.fromJson
|
|
import net.dv8tion.jda.core.EmbedBuilder
|
|
import nl.voidcorp.dbot.addAll
|
|
import nl.voidcorp.dbot.commands
|
|
import nl.voidcorp.dbot.gson
|
|
import nl.voidcorp.dbot.random
|
|
import nl.voidcorp.dbot.storage.XKCD
|
|
import java.time.LocalDateTime
|
|
|
|
|
|
val xkcd = UnityCommand("xkcd", help = "Fetch an xkcd comic, either a random one, or a specific one if specified", category = FunCategory, arguments = "the comic number (optional)") { event ->
|
|
val burl = "https://xkcd.com/"
|
|
val eurl = "/info.0.json"
|
|
val eb = EmbedBuilder().setTimestamp(LocalDateTime.now()).setFooter("Requested by ${event.member.effectiveName}", event.member.user.effectiveAvatarUrl)
|
|
.setColor(event.selfMember.color)
|
|
if (event.args.isEmpty()) {
|
|
val latest = gson.fromJson<XKCD>(khttp.get(burl + eurl.substring(1)).text)
|
|
val num = random.nextInt(latest.num - 1) - 1
|
|
val rnd = gson.fromJson<XKCD>(khttp.get(burl + num + eurl).text)
|
|
eb.setTitle(rnd.title, "$burl${rnd.num}")
|
|
eb.setImage(rnd.img)
|
|
eb.setDescription(rnd.alt)
|
|
|
|
} else {
|
|
val comic = khttp.get(burl + event.args + eurl)
|
|
if (comic.statusCode != 200) {
|
|
eb.setTitle("Oops, this comic does not exist...")
|
|
eb.setDescription(comic.statusCode.toString())
|
|
} else {
|
|
val com = gson.fromJson<XKCD>(comic.text)
|
|
eb.setTitle(com.title, "$burl${com.num}")
|
|
eb.setImage(com.img)
|
|
eb.setDescription(com.alt)
|
|
}
|
|
}
|
|
|
|
event.reply(eb.build())
|
|
}
|
|
|
|
val cat = UnityCommand("cat", help = "Get a random cat image!", category = FunCategory) { event ->
|
|
val url = "https://aws.random.cat/meow"
|
|
val eb = EmbedBuilder().setTimestamp(LocalDateTime.now()).setFooter("Requested by ${event.member.effectiveName}", event.member.user.effectiveAvatarUrl)
|
|
.setColor(event.selfMember.color)
|
|
eb.setImage(khttp.get(url).jsonObject["file"].toString())
|
|
|
|
event.reply(eb.build())
|
|
|
|
}
|
|
|
|
val dog = UnityCommand("dog", help = "Get a random dog image!", category = FunCategory) { event ->
|
|
val url = "https://random.dog/woof.json"
|
|
val eb = EmbedBuilder().setTimestamp(LocalDateTime.now()).setFooter("Requested by ${event.member.effectiveName}", event.member.user.effectiveAvatarUrl)
|
|
.setColor(event.selfMember.color)
|
|
eb.setImage(khttp.get(url).jsonObject["url"].toString())
|
|
|
|
event.reply(eb.build())
|
|
|
|
}
|
|
|
|
fun initFun() {
|
|
commands.addAll(xkcd, cat, dog)
|
|
} |