Open ItsDoot opened 5 years ago
Commands requiring permissions should be built using an ExecutionTransformer.
annotation class Permission(val permission: String)
class PermissionTransformer(private val permission: String): ExecutionTransformer {
override fun transform(src: CommandSource, context: ExecutionContext, next: () -> Unit) {
if (!src.hasPermission(permission)) {
throw CommandException(!"You do not have permission to use this command.")
}
next()
}
}
@Permission("my.permission")
@Command("mycmd")
fun mycmd(@Source src: CommandSource) {
// do stuff...
}
EDIT: Will not be doing this since permission checking is so integrated with CommandCallable
Another usecase: Logging the time a command takes to run.
annotation class LogTime
class LogTimeTransformer(private val logger: Logger) : ExecutionTransformer {
override fun transform(src: CommandSource, context: ExecutionContext, next: () -> Unit) {
val time = measureTimeMillis {
next()
}
logger.debug("Command took ${time}ms")
}
}
@LogTime
@Command("mycmd")
fun mycmd(@Source src: CommandSource) {
// do stuff...
}
Basically what I am looking for is a convenient way to wrap a function call given some annotations on the function.
A good starting point:
My primary usecase: wrapping function calls in Exposed transaction blocks, to reduce redundancy.