Advertisement
gt22

Untitled

Aug 12th, 2020
2,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.34 KB | None | 0 0
  1. package dsl
  2.  
  3. import net.dv8tion.jda.core.EmbedBuilder
  4. import net.dv8tion.jda.core.entities.MessageEmbed
  5. import java.awt.Color
  6. import java.io.*
  7. import java.time.temporal.TemporalAccessor
  8.  
  9. typealias Init<T> = T.() -> Unit
  10.  
  11. @DslMarker
  12. annotation class EmbedDsl
  13.  
  14. typealias Attachments = Map<String, InputStream>
  15.  
  16. @EmbedDsl
  17. open class BaseEmbedCreater {
  18.  
  19.     var builder = EmbedBuilder()
  20.  
  21.     var thumbnail: String? = null
  22.         set(value) {
  23.             field = value
  24.             builder.setThumbnail(value)
  25.         }
  26.  
  27.     var color: Color? = null
  28.         set(value) {
  29.             field = value
  30.             builder.setColor(value)
  31.         }
  32.  
  33.     var timestamp: TemporalAccessor? = null
  34.         set(value) {
  35.             field = value
  36.             builder.setTimestamp(value)
  37.         }
  38.  
  39.     var title: String? = null
  40.         set(value) {
  41.             field = value
  42.             builder.setTitle(value, url)
  43.         }
  44.  
  45.     var url: String? = null
  46.         set(value) {
  47.             field = value
  48.             builder.setTitle(title, value)
  49.         }
  50.  
  51.     var image: String? = null
  52.         set(value) {
  53.             field = value
  54.             builder.setImage(value)
  55.         }
  56.  
  57.     var attachments: MutableMap<String, InputStream> = mutableMapOf()
  58.  
  59.     val append by lazy { FieldHolder(this, false) }
  60.     val inline by lazy { FieldHolder(this, true) }
  61.  
  62.  
  63.     open fun text(s: String) {
  64.         builder.appendDescription(s)
  65.     }
  66.  
  67.     open fun text(init: BaseEmbedCreater.() -> String) {
  68.         text(init())
  69.     }
  70.  
  71.     open operator fun String.unaryPlus() {
  72.         text(this)
  73.     }
  74.  
  75.     open fun field(init: Init<FieldBuilder>) {
  76.         setElement(::FieldBuilder, init)
  77.     }
  78.  
  79.     open fun author(init: Init<AuthorBuilder>) {
  80.         setElement(::AuthorBuilder, init)
  81.     }
  82.  
  83.     open fun footer(init: Init<FooterBuilder>) {
  84.         setElement(::FooterBuilder, init)
  85.     }
  86.  
  87.     open infix fun String.attach(stream: InputStream): String {
  88.         attachments[this] = stream
  89.         return "attachment://${this}"
  90.     }
  91.  
  92.     open infix fun String.attach(stream: File): String {
  93.         attachments[this] = FileInputStream(stream)
  94.         return "attachment://${this}"
  95.     }
  96.  
  97.     open infix fun String.attach(stream: ByteArray): String {
  98.         attachments[this] = ByteArrayInputStream(stream)
  99.         return "attachment://${this}"
  100.     }
  101.  
  102.     private inline fun <T : ElementBuilder> setElement(eBuilder: (EmbedBuilder) -> T, init: Init<T>) {
  103.         val b = eBuilder(this.builder)
  104.         b.init()
  105.         b.complete()
  106.     }
  107.  
  108. }
  109.  
  110.  
  111. class FieldHolder(private val builder: BaseEmbedCreater, private val inline: Boolean) {
  112.  
  113.     class FieldBase(private val builder: BaseEmbedCreater, val name: String, private val inline: Boolean) {
  114.  
  115.         infix fun to(value: String) {
  116.             builder.field {
  117.                 name = this@FieldBase.name
  118.                 this.value = value
  119.                 inline = this@FieldBase.inline
  120.             }
  121.         }
  122.  
  123.     }
  124.  
  125.     infix fun field(name: String) = FieldBase(builder, name, inline)
  126.  
  127. }
  128.  
  129. @EmbedDsl
  130. interface ElementBuilder {
  131.     fun complete()
  132. }
  133.  
  134. class FieldBuilder(val builder: EmbedBuilder) : ElementBuilder {
  135.  
  136.     var name: String? = null
  137.     var value: String? = null
  138.     var inline = false
  139.  
  140.  
  141.     override fun complete() {
  142.         builder.addField(name, value, inline)
  143.     }
  144.  
  145. }
  146.  
  147. class FooterBuilder(private val builder: EmbedBuilder) : ElementBuilder {
  148.  
  149.     var text: String? = null
  150.     var icon: String? = null
  151.  
  152.     init {
  153.         val e = builder.build().footer
  154.         if(e != null) {
  155.             text = e.text
  156.             icon = e.iconUrl
  157.         }
  158.     }
  159.  
  160.     override fun complete() {
  161.         builder.setFooter(text, icon)
  162.     }
  163.  
  164. }
  165.  
  166. class AuthorBuilder(private val builder: EmbedBuilder) : ElementBuilder {
  167.     var name: String? = null
  168.     var url: String? = null
  169.     var icon: String? = null
  170.  
  171.     init {
  172.         val e = builder.build().author
  173.         if(e != null) {
  174.             name = e.name
  175.             url = e.url
  176.             icon = e.iconUrl
  177.         }
  178.     }
  179.  
  180.     override fun complete() {
  181.         builder.setAuthor(name, url, icon)
  182.     }
  183.  
  184. }
  185.  
  186. inline fun embed(init: Init<BaseEmbedCreater>): Pair<MessageEmbed, Attachments> {
  187.     val e = BaseEmbedCreater()
  188.     e.init()
  189.     return e.builder.build() to e.attachments.toMap()
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement