49 lines
1.8 KiB
Kotlin
49 lines
1.8 KiB
Kotlin
package nl.voidcorp.discord.commands.`fun`
|
|
|
|
import net.dv8tion.jda.api.EmbedBuilder
|
|
import nl.voidcorp.discord.command.Command
|
|
import nl.voidcorp.discord.command.CommandGroup
|
|
import nl.voidcorp.discord.command.CommandMessage
|
|
import nl.voidcorp.discord.command.CommandResult
|
|
import nl.voidcorp.discord.external.XKCDComic
|
|
import org.springframework.boot.web.client.RestTemplateBuilder
|
|
import org.springframework.stereotype.Service
|
|
import org.springframework.web.client.getForObject
|
|
import java.lang.Exception
|
|
|
|
@Service
|
|
class XKXDComicCommand : Command(
|
|
"xkcd",
|
|
helpMesage = "Shows the latest xkcd comic, or the one specified",
|
|
usage = "xkcd [number]",
|
|
group = CommandGroup.FUN,
|
|
allowAnywhere = true
|
|
) {
|
|
override fun handle(event: CommandMessage): CommandResult {
|
|
val template = RestTemplateBuilder().build()
|
|
val comic: XKCDComic? = if (event.params.drop(1).isEmpty()) {
|
|
template.getForObject("https://xkcd.com/info.0.json")
|
|
} else {
|
|
val id = event.params.drop(1).first()
|
|
if (!id.matches("\\d+".toRegex())) {
|
|
event.reply("$id is not a number...")
|
|
return CommandResult.SUCCESS
|
|
}
|
|
try {
|
|
template.getForObject<XKCDComic>("https://xkcd.com/$id/info.0.json")
|
|
} catch (e: Exception) {
|
|
event.reply("It seems I can't find that comic?")
|
|
return CommandResult.SUCCESS
|
|
}
|
|
}
|
|
|
|
if (comic != null) {
|
|
val builder = EmbedBuilder().setTitle(comic.title, "https://xkcd.com/${comic.num}")
|
|
.setImage(comic.img).setFooter(comic.alt).build()
|
|
event.reply(builder)
|
|
} else {
|
|
return CommandResult.ERROR
|
|
}
|
|
return CommandResult.SUCCESS
|
|
}
|
|
} |