OttoBot/src/main/kotlin/nl/voidcorp/dbot/commands/AdminCommands.kt

111 lines
4.1 KiB
Kotlin

package nl.voidcorp.dbot.commands
import nl.voidcorp.dbot.commands
import nl.voidcorp.dbot.storage.MuteInfo
import nl.voidcorp.dbot.storage.getBefore
import nl.voidcorp.dbot.storage.put
import nl.voidcorp.dbot.storage.settings
import java.time.LocalDateTime
fun initAdmin() {
commands += SetPrefix
commands += ListPrefixes
commands += RemovePrefix
commands += AddRoleCommand
/*commands += MuteCommand
commands += LuaExec*/
}
object SetPrefix : UnityCommand(
"setprefix",
"Sets the prefix for this command",
AdminCategory,
howTo = "!setprefix prefix",
exec = { ce ->
ce.guild.settings().prefixes += ce.args
ce.reply("This servers prefix is now set to ${ce.args}, but you can still use ${ce.selfMember.asMention} to reset it!")
})
object ListPrefixes : UnityCommand("listprefixes", "Lists all server prefixes", AdminCategory, exec = { ce ->
ce.reply("This servers prefixes are: ${ce.guild.settings().prefixes.ifEmpty { listOf("!") }.joinToString { "`$it`" }}")
})
object RemovePrefix : UnityCommand("removeprefix", "Removes a prefix", AdminCategory, exec = { ce ->
val res = ce.guild.settings().prefixes.removeIf { it == ce.args }
if (res) {
ce.reply("Remove the prefix `${ce.args}`")
} else {
ce.reply("`${ce.args}` is not a known prefix?")
}
})
object MuteCommand : UnityCommand("mute", "Mutes a member of this guild", AdminCategory, exec = { ce ->
println(ce.hasArgs)
println("`${ce.args}`")
val settings = ce.guild.settings()
if (ce.hasArgs) {
val mentions = ce.message.mentionedMembers
if (mentions.isNotEmpty() and mentions.any { it.user != ce.author }) {
val muteTime = ce.args.split(">")[1].run { if (isBlank()) "10" else this }.trim()
ce.reply("The user `${mentions[0].effectiveName}` has been muted for `$muteTime` minutes!")
settings.muted.put(
LocalDateTime.now().plusMinutes(muteTime.toLong()), MuteInfo(
mentions[0].user.idLong,
mentions[0].roles
)
)
}
} else {
if (settings.muted.isNotEmpty()) {
ce.reply("The currently muted users are ${ce.guild.settings().muted.getBefore().map { list ->
list.map { mi ->
ce.guild.getMemberById(
mi.member
)
}.joinToString { "`${it.effectiveName}`" }
}}")
} else {
ce.reply("There are no muted users!")
}
}
}
)
object AddRoleCommand : UnityCommand(
"addrole",
"Add a role to the Role picker",
AdminCategory,
howTo = "addrole Rolename,rolekey",
exec = { ce ->
val args = ce.args.split(",").map { it.trim() }
if (args.size != 2) {
ce.reply("Ehm, that is not how this is supposed to work, the args have to be `Rolename,rolekey`\nWith Rolename the actual name in discord and rolekey the key you want to assign.")
} else {
val gs = GSM.getSettings(ce.guild)
val role = ce.guild.getRolesByName(args[0], true).firstOrNull()
val key = args[1]
if (role == null) {
ce.reply("Ehm, that is not how this is supposed to work, the args have to be `Rolename,rolekey`\nWith Rolename the actual name in discord and rolekey the key you want to assign.")
ce.reply("Also, the discord role you provided is not existing?")
} else {
if (gs.roleMap.containsKey(key)) {
ce.reply("First remove the key $key and then add it again, this is to prevent accidental overrides...")
} else {
gs.roleMap[key] = role.idLong
ce.reply("Added role ${role.name} with key `$key`")
}
}
}
})
object LuaExec : UnityCommand("luaexec", "Executes a bit of lua code", AdminCategory, exec = { ce ->
ce.message.delete().queue()
val r = nl.voidcorp.dbot.run(ce.args, scriptEngine = "javascript")
ce.reply(r.toString())
})
object UnknownCommandError : Exception("yeet!")