Skip to content

Instantly share code, notes, and snippets.

@JohnnyJayJay
Created April 1, 2019 15:12
Show Gist options
  • Save JohnnyJayJay/ac67072ddca07f74be221ff71faee651 to your computer and use it in GitHub Desktop.
Save JohnnyJayJay/ac67072ddca07f74be221ff71faee651 to your computer and use it in GitHub Desktop.
Utilities for JDA discord bots created with the kotlin programming language
package com.github.johnnyjayjay.titanbot.utils
import net.dv8tion.jda.core.EmbedBuilder
import net.dv8tion.jda.core.MessageBuilder
import net.dv8tion.jda.core.entities.Message
import net.dv8tion.jda.core.entities.MessageEmbed
import net.dv8tion.jda.core.entities.MessageEmbed.*
import java.awt.Color
import java.time.Instant
import java.time.temporal.TemporalAccessor
import java.util.*
const val ZERO_WIDTH_SPACE = EmbedBuilder.ZERO_WIDTH_SPACE
fun messages(vararg policy: MessageBuilder.SplitPolicy, body: KotlinMessageBuilder.() -> Unit) =
KotlinMessageBuilder().buildAll(policy = *policy, body = body)
fun message(body: KotlinMessageBuilder.() -> Unit) = KotlinMessageBuilder().build(body)
fun embed(body: KotlinEmbedBuilder.() -> Unit) = KotlinEmbedBuilder().build(body)
fun fields(body: Fields.() -> Unit) = Fields().apply(body)
fun field(body: FieldBuilder.() -> Unit) = FieldBuilder().build(body)
class KotlinMessageBuilder {
lateinit var content: String
var embed: MessageEmbed? = null
var tts: Boolean = false
fun build(body: KotlinMessageBuilder.() -> Unit): Message {
body()
return build()
}
fun buildAll(vararg policy: MessageBuilder.SplitPolicy, body: KotlinMessageBuilder.() -> Unit): Queue<Message> {
body()
return buildAll(*policy)
}
fun build() = makeBuilder().build()
fun buildAll(vararg policy: MessageBuilder.SplitPolicy) =
makeBuilder().buildAll(*policy)
private fun makeBuilder() =
MessageBuilder()
.setContent(content)
.setEmbed(embed)
.setTTS(tts)
}
class KotlinEmbedBuilder {
lateinit var description: String
var thumbnail: String? = null
var image: String? = null
var title: String? = null
var avatar: String? = null
var author: String? = null
var authorURL: String? = null
var titleURL: String? = null
var footer: String? = null
var icon: String? = null
var timestamp: TemporalAccessor? = null
var color: Color? = null
var fields: Fields = Fields()
fun build(): MessageEmbed {
return EmbedBuilder().setDescription(description)
.setThumbnail(thumbnail)
.setImage(image)
.setTitle(title, titleURL)
.setAuthor(author, authorURL, avatar)
.setFooter(footer, icon)
.setTimestamp(timestamp)
.setColor(color)
.also {
for (field in fields)
it.addField(field)
}
.build()
}
fun build(body: KotlinEmbedBuilder.() -> Unit): MessageEmbed {
body()
return build()
}
fun now(): TemporalAccessor = Instant.now()
}
class Fields {
private val list: MutableList<Field> = ArrayList()
fun blank(inline: Boolean = false) =
add(Field(ZERO_WIDTH_SPACE, ZERO_WIDTH_SPACE, inline))
fun add(field: Field) = list.add(field)
fun add(body: FieldBuilder.() -> Unit) = add(FieldBuilder().build(body))
operator fun iterator() = list.iterator()
}
class FieldBuilder {
lateinit var name: String
lateinit var value: String
var inline = false
fun build() = Field(name, value, inline)
fun build(body: FieldBuilder.() -> Unit): Field {
body()
return build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment