2018-10-30 20:38:44 +01:00
package nl.voidcorp.dbot.commands
import nl.voidcorp.dbot.commands
2018-11-05 17:41:18 +01:00
import nl.voidcorp.dbot.storage.MuteInfo
import nl.voidcorp.dbot.storage.getBefore
import nl.voidcorp.dbot.storage.put
2018-10-30 20:38:44 +01:00
import nl.voidcorp.dbot.storage.settings
2018-11-05 17:41:18 +01:00
import java.time.LocalDateTime
2018-10-30 20:38:44 +01:00
fun initAdmin ( ) {
commands += SetPrefix
commands += ListPrefixes
commands += RemovePrefix
2018-11-05 22:10:19 +01:00
commands += AddRoleCommand
2018-11-05 17:41:18 +01:00
/ * commands += MuteCommand
commands += LuaExec * /
2018-10-30 20:38:44 +01:00
}
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? " )
}
} )
2018-11-05 17:41:18 +01:00
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! " )
}
}
}
)
2018-11-05 22:10:19 +01:00
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 ) {
2018-11-07 23:25:56 +01:00
ce . reply ( " Ehm, that is not how this is supposed to work, the args have to be `Rolename,rolekey` \n With Rolename the actual title in discord and rolekey the key you want to assign. " )
2018-11-05 22:10:19 +01:00
} else {
val gs = GSM . getSettings ( ce . guild )
val role = ce . guild . getRolesByName ( args [ 0 ] , true ) . firstOrNull ( )
val key = args [ 1 ]
if ( role == null ) {
2018-11-07 23:25:56 +01:00
ce . reply ( " Ehm, that is not how this is supposed to work, the args have to be `Rolename,rolekey` \n With Rolename the actual title in discord and rolekey the key you want to assign. " )
2018-11-05 22:10:19 +01:00
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 ` " )
}
}
}
} )
2018-11-05 17:41:18 +01:00
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 ( ) )
} )
2018-10-30 20:38:44 +01:00
object UnknownCommandError : Exception ( " yeet! " )