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

me.jakejmattson.kutils.api.arguments.VoiceChannelArg.kt Maven / Gradle / Ivy

There is a newer version: 0.18.1
Show newest version
package me.jakejmattson.kutils.api.arguments

import me.jakejmattson.kutils.api.dsl.arguments.*
import me.jakejmattson.kutils.api.dsl.command.CommandEvent
import me.jakejmattson.kutils.api.extensions.jda.tryRetrieveSnowflake
import me.jakejmattson.kutils.api.extensions.stdlib.trimToID
import net.dv8tion.jda.api.entities.*

open class VoiceChannelArg(override val name: String = "Voice Channel", private val allowsGlobal: Boolean = false) : ArgumentType() {
    companion object : VoiceChannelArg()

    override fun convert(arg: String, args: List, event: CommandEvent<*>): ArgumentResult {
        val channel = event.discord.jda.tryRetrieveSnowflake {
            it.getVoiceChannelById(arg.trimToID())
        } as VoiceChannel? ?: return ArgumentResult.Error("Couldn't retrieve $name from $arg.")

        if (!allowsGlobal && channel.guild.id != event.guild?.id)
            return ArgumentResult.Error("$name must be from this guild.")

        return ArgumentResult.Success(channel)
    }

    override fun generateExamples(event: CommandEvent<*>): List {
        val channel = event.guild?.channels?.firstOrNull { it.type == ChannelType.VOICE } as? VoiceChannel
        return listOf(channel?.id ?: "582168201979494421")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy