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

me.jakejmattson.kutils.api.extensions.jda.JDAExtensions.kt Maven / Gradle / Ivy

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

package me.jakejmattson.kutils.api.extensions.jda

import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Role

enum class RoleIdentifier {
    Name, Id, Invalid
}

fun JDA.getRoleIdentifier(role: String): RoleIdentifier {
    val roles = roles

    if (roles.any { it.id == role }) return RoleIdentifier.Id

    if (roles.any { it.name.toLowerCase() == role.toLowerCase() }) return RoleIdentifier.Name

    return RoleIdentifier.Invalid
}

fun JDA.isRole(role: String) = roles.any { it.id == role || it.name.toLowerCase() == role.toLowerCase() }

fun JDA.obtainRole(role: String): Role? = roles.firstOrNull { it.name.toLowerCase() == role.toLowerCase() || it.id == role }

fun JDA.tryRetrieveSnowflake(action: (JDA) -> Any?) =
    try {
        action(this)
    } catch (e: RuntimeException) {
        null
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy