ottobotv2/src/main/kotlin/nl/voidcorp/discord/commands/roles/JoinRoleCommand.kt

51 lines
1.9 KiB
Kotlin

package nl.voidcorp.discord.commands.roles
import net.dv8tion.jda.api.entities.Role
import nl.voidcorp.discord.command.*
import nl.voidcorp.discord.storage.GuildStore
import org.springframework.stereotype.Service
@Service
class JoinRoleCommand :
Command(
"joinrole",
aliases = listOf("role"),
commandLevel = CommandLevel.ALL,
usage = "joinrole [rolename]",
location = CommandSource.GUILD,
group = CommandGroup.ROLES
) {
override fun handle(event: CommandMessage): CommandResult {
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
if (event.params.size == 1) {
if (guild.roleMap.isNotEmpty())
event.reply(guild.roleMap.keys.joinToString(prefix = "The available roles are: ") { "`$it`" })
else
event.reply("There are no roles to pick here...")
} else {
val fail = mutableListOf<String>()
val success = mutableListOf<String>()
val roles = mutableListOf<Role>()
for (p in event.params.drop(1)) {
val roleID = guild.roleMap[p]
if (roleID == null) {
fail += p
} else {
val role = event.guild.getRoleById(roleID)
if (role != null) {
success += p
roles += role
} else {
fail += p
}
}
}
roles.forEach {
event.guild.addRoleToMember(event.member!!, it).queue()
}
if (success.isNotEmpty()) event.reply("I have given you the following roles: ${roles.joinToString { "`${it.name}`" }}!")
if (fail.isNotEmpty()) event.reply("I failed to find the following roles: ${fail.joinToString { "`$it`" }}")
}
return CommandResult.SUCCESS
}
}