Add some fun commands

This commit is contained in:
Julius de Jeu 2018-10-20 16:43:21 +02:00
parent 1dd0925897
commit 2012e76ed7
5 changed files with 82 additions and 6 deletions

View file

@ -18,10 +18,7 @@ import net.dv8tion.jda.core.entities.MessageEmbed
import net.dv8tion.jda.core.events.message.MessageReceivedEvent import net.dv8tion.jda.core.events.message.MessageReceivedEvent
import net.dv8tion.jda.webhook.WebhookClient import net.dv8tion.jda.webhook.WebhookClient
import net.dv8tion.jda.webhook.WebhookClientBuilder import net.dv8tion.jda.webhook.WebhookClientBuilder
import nl.voidcorp.dbot.commands.UnityCommand import nl.voidcorp.dbot.commands.*
import nl.voidcorp.dbot.commands.helloCommand
import nl.voidcorp.dbot.commands.initMusic
import nl.voidcorp.dbot.commands.pingCommand
import nl.voidcorp.dbot.storage.GitlabWebhook import nl.voidcorp.dbot.storage.GitlabWebhook
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.io.File import java.io.File
@ -141,6 +138,7 @@ fun main(args: Array<String>) {
} }
initMusic() initMusic()
initFun()
cb.setListener(object : CommandListener { cb.setListener(object : CommandListener {
override fun onNonCommandMessage(event: MessageReceivedEvent) { override fun onNonCommandMessage(event: MessageReceivedEvent) {
val msg = event.message.contentRaw val msg = event.message.contentRaw

View file

@ -12,7 +12,7 @@ val helloCommand = UnityCommand("hello", "Say hello to Andy!", aliases = *arrayO
val pingCommand = UnityCommand("ping", help = "Check the bot's ping", aliases = *arrayOf("pong")) { event -> val pingCommand = UnityCommand("ping", help = "Check the bot's ping", aliases = *arrayOf("pong")) { event ->
event.reply("Ping: ...") { m -> event.reply("Ping: ...") { m ->
val ping = event.message.creationTime.until(m.creationTime, ChronoUnit.MILLIS) val ping = event.message.creationTime.until(m.creationTime, ChronoUnit.MILLIS)
m.editMessage("Ping: " + ping + "ms | Websocket: " + event.getJDA().getPing() + "ms").queue() m.editMessage("Ping: " + ping + "ms | Websocket: " + event.jda.ping + "ms").queue()
} }
} }

View file

@ -0,0 +1,74 @@
package nl.voidcorp.dbot.commands
import com.github.salomonbrys.kotson.fromJson
import com.jagrosh.jdautilities.command.Command
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 funCategory = Command.Category("Fun Commands") {
if (it.guild.getTextChannelsByName("bot", true).firstOrNull() == null) return@Category true
val test = it.textChannel.name.contains("bot")
if (!test) {
it.reply("Non music commands can only be used in the ${it.guild.getTextChannelsByName("bot", true).first().asMention} channel")
}
test
}
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)
}

View file

@ -44,7 +44,7 @@ val generalCategory = Command.Category("General commands") {
open class UnityCommand(name: String, help: String = "", open class UnityCommand(name: String, help: String = "",
category: Category = generalCategory, category: Category = generalCategory,
arguments: String = "", vararg aliases: String = arrayOf(name.first().toString()), val exec: (event: CommandEvent) -> Unit) : Command() { arguments: String = "", vararg aliases: String = arrayOf(), val exec: (event: CommandEvent) -> Unit) : Command() {
init { init {
super.name = name super.name = name
super.help = help super.help = help

View file

@ -0,0 +1,4 @@
package nl.voidcorp.dbot.storage
data class XKCD(val title: String, val num: Int, val img: String, val alt:String)