me.jakejmattson.kutils.api.arguments.GuildEmoteArg.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of KUtils Show documentation
Show all versions of KUtils Show documentation
A Discord bot framework for Kotlin.
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.stdlib.trimToID
import net.dv8tion.jda.api.entities.Emote
open class GuildEmoteArg(override val name: String = "Guild Emote", private val allowsGlobal: Boolean = false) : ArgumentType() {
companion object : GuildEmoteArg()
override fun convert(arg: String, args: List, event: CommandEvent<*>): ArgumentResult {
val trimmed = arg.trimToID()
val split = trimmed.split(":")
val id = when (split.size) {
1 -> split[0]
3 -> split[2]
else -> return ArgumentResult.Error("Couldn't retrieve $name from $arg.")
}
val availableEmotes =
if (allowsGlobal)
event.discord.jda.guilds.flatMap { it.emotes }
else
event.guild?.emotes
availableEmotes ?: return ArgumentResult.Error("Could not find a guild to fetch emojis from.")
val emote = availableEmotes.firstOrNull { it.id == id }
?: return ArgumentResult.Error("Could not find an emoji${if (!allowsGlobal) " in this guild " else " "}with the ID: $id")
return ArgumentResult.Success(emote)
}
override fun generateExamples(event: CommandEvent<*>) = event.guild?.emotes?.map { it.asMention } ?: emptyList()
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy