All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.jakejmattson.kutils.api.Discord.kt Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
@file:Suppress("unused")

package me.jakejmattson.kutils.api

import com.google.gson.Gson
import me.jakejmattson.kutils.api.dsl.command.*
import me.jakejmattson.kutils.api.dsl.configuration.KConfiguration
import me.jakejmattson.kutils.internal.event.EventRegister
import me.jakejmattson.kutils.internal.utils.diService
import net.dv8tion.jda.api.*
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.hooks.EventListener
import net.dv8tion.jda.api.requests.GatewayIntent
import net.dv8tion.jda.api.utils.MemberCachePolicy
import kotlin.reflect.KClass

data class KUtilsProperties(val repository: String,
                            val kutilsVersion: String,
                            val kotlinVersion: String,
                            val jdaVersion: String)

private val propFile = KUtilsProperties::class.java.getResource("/kutils-properties.json").readText()

abstract class Discord {
    @Deprecated("To be removed")
    abstract val jda: JDA
    abstract val configuration: KConfiguration
    val properties = Gson().fromJson(propFile, KUtilsProperties::class.java)!!

    internal abstract fun addEventListener(register: EventRegister)

    @Deprecated("Use classes as parameters", ReplaceWith("discord.getInjectionObjects(T::class)"))
    inline fun  getInjectionObject() = diService.getElement()

    inline fun  getInjectionObjects(a: KClass) = diService.getElement()

    inline fun 
        getInjectionObjects(a: KClass, b: KClass) =
        Args2(getInjectionObjects(a), getInjectionObjects(b))

    inline fun 
        getInjectionObjects(a: KClass, b: KClass, c: KClass) =
        Args3(getInjectionObjects(a), getInjectionObjects(b), getInjectionObjects(c))

    inline fun 
        getInjectionObjects(a: KClass, b: KClass, c: KClass, d: KClass) =
        Args4(getInjectionObjects(a), getInjectionObjects(b), getInjectionObjects(c), getInjectionObjects(d))

    inline fun 
        getInjectionObjects(a: KClass, b: KClass, c: KClass, d: KClass, e: KClass) =
        Args5(getInjectionObjects(a), getInjectionObjects(b), getInjectionObjects(c), getInjectionObjects(d), getInjectionObjects(e))
}

internal fun buildDiscordClient(token: String, configuration: KConfiguration) =
    object : Discord() {
        override val jda: JDA = JDABuilder.createDefault(token)
            .setMemberCachePolicy(MemberCachePolicy.ALL)
            .enableIntents(GatewayIntent.GUILD_MEMBERS)
            .build()
            .also { it.awaitReady() }

        override val configuration: KConfiguration = configuration

        override fun addEventListener(register: EventRegister) {
            jda.addEventListener(object : EventListener {
                override fun onEvent(event: GenericEvent) = register.onEvent(event)
            })
        }
    }