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

33 lines
1.3 KiB
Kotlin

package nl.voidcorp.discord.commands.roles
import nl.voidcorp.discord.command.*
import nl.voidcorp.discord.storage.GuildStore
import org.springframework.stereotype.Service
@Service
class LeaveRole : Command(
"leaverole",
aliases = listOf("derole"),
location = CommandSource.GUILD,
usage = "leaverole rolename",
group = CommandGroup.ROLES,
commandLevel = CommandLevel.VERIFIED
) {
override fun handle(event: CommandMessage): CommandResult {
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
val toRemove = guild.roleMap.filterKeys { it in event.params.drop(1) }
// toRemove.forEach { guild.roleMap.remove(it) }
// repo.save(guild)
val roleLongs =
event.member!!.roles.map { it.idLong }.intersect(toRemove.values)
val remove = roleLongs.map { event.guild.getRoleById(it) }.filter { it != null }
val remmed = toRemove.filterValues { it in roleLongs }.keys
remove.forEach {
if (it != null)
event.guild.removeRoleFromMember(event.member, it).queue()
}
event.reply("Removed the roles ${remmed.joinToString { "`$it`" }}")
return CommandResult.SUCCESS
}
}