diff --git a/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt b/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt index e720945..8a0504b 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/UnityBot.kt @@ -18,10 +18,7 @@ import net.dv8tion.jda.core.entities.MessageEmbed import net.dv8tion.jda.core.events.message.MessageReceivedEvent import net.dv8tion.jda.webhook.WebhookClient import net.dv8tion.jda.webhook.WebhookClientBuilder -import nl.voidcorp.dbot.commands.UnityCommand -import nl.voidcorp.dbot.commands.helloCommand -import nl.voidcorp.dbot.commands.initMusic -import nl.voidcorp.dbot.commands.pingCommand +import nl.voidcorp.dbot.commands.* import nl.voidcorp.dbot.storage.GitlabWebhook import org.slf4j.LoggerFactory import java.io.File @@ -141,6 +138,7 @@ fun main(args: Array) { } initMusic() + initFun() cb.setListener(object : CommandListener { override fun onNonCommandMessage(event: MessageReceivedEvent) { val msg = event.message.contentRaw diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt index d2dd45c..ea53053 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt @@ -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 -> event.reply("Ping: ...") { m -> 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() } } diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/FunCommands.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/FunCommands.kt new file mode 100644 index 0000000..f147b7d --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/FunCommands.kt @@ -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(khttp.get(burl + eurl.substring(1)).text) + val num = random.nextInt(latest.num - 1) - 1 + val rnd = gson.fromJson(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(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) +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt b/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt index 5471613..45f9d2f 100644 --- a/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt +++ b/src/main/kotlin/nl/voidcorp/dbot/commands/UnityCommand.kt @@ -44,7 +44,7 @@ val generalCategory = Command.Category("General commands") { open class UnityCommand(name: String, help: String = "", 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 { super.name = name super.help = help diff --git a/src/main/kotlin/nl/voidcorp/dbot/storage/RestStore.kt b/src/main/kotlin/nl/voidcorp/dbot/storage/RestStore.kt new file mode 100644 index 0000000..8f896f6 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/dbot/storage/RestStore.kt @@ -0,0 +1,4 @@ +package nl.voidcorp.dbot.storage + +data class XKCD(val title: String, val num: Int, val img: String, val alt:String) +