@@ -46,7 +46,8 @@ public inline fun SaltifyApplication.regex(
4646 val regex = Regex (regex)
4747
4848 return on<Event .MessageReceive >(scope) { event ->
49- val text = event.segments.plainText
49+ val text = event.segments.filterIsInstance<IncomingSegment .Text >()
50+ .joinToString(" " ) { it.text }
5051
5152 val matches = regex.findAll(text)
5253 if (matches.any()) block(event, matches)
@@ -71,12 +72,10 @@ public fun SaltifyApplication.command(
7172 .joinToString(" " ) { it.text }
7273 .trim()
7374
74- if (rawText != " $prefix$name " && ! rawText.startsWith(" $prefix$name " )) return @on
75-
76- val content = rawText.removePrefix(" $prefix$name " ).trim()
77- val tokens = if (content.isEmpty()) emptyList() else content.split(spaceRegex)
75+ val tokens = if (rawText.isEmpty()) emptyList() else rawText.split(spaceRegex)
76+ if (tokens.isEmpty() || tokens[0 ] != " $prefix$name " ) return @on
7877
79- executeCommand(rootDsl, tokens, this , event, name)
78+ executeCommand(rootDsl, tokens.drop( 1 ) , this , event, name)
8079 }
8180}
8281
@@ -87,21 +86,28 @@ private suspend fun executeCommand(
8786 event : Event .MessageReceive ,
8887 name : String
8988) {
89+ val argumentMap = mutableMapOf<SaltifyCommandParamDef <* >, ParameterParseResult <Any >>()
90+ val execution = SaltifyCommandExecutionContext (client, event, name, argumentMap)
91+
92+ dsl.requirementBlock?.let { block ->
93+ val requirement = SaltifyCommandRequirementContext (execution).block()
94+ if (! requirement.satisfies()) return
95+ }
96+
9097 if (tokens.isNotEmpty()) {
9198 val subName = tokens[0 ]
9299 val subCommand = dsl.subCommands.find { it.first == subName }
93100 if (subCommand != null ) {
94- executeCommand(subCommand.second, tokens.drop(1 ), client, event, name)
101+ executeCommand(subCommand.second, tokens.drop(1 ), client, event, " $ name $subName " )
95102 return
96103 }
97104 }
98105
99- val argumentMap = mutableMapOf<SaltifyCommandParamDef <* >, ParameterParseResult <Any >>()
100106 val errors = mutableListOf<CommandError >()
101107 val currentTokens = tokens.toMutableList()
102108
103109 for (param in dsl.parameters) {
104- argumentMap[param] = when {
110+ val result = when {
105111 currentTokens.isEmpty() -> ParameterParseResult .MissingParam
106112 param.isGreedy -> {
107113 val value = currentTokens.joinToString(" " )
@@ -113,24 +119,18 @@ private suspend fun executeCommand(
113119 convertValue(rawValue, param.type)?.let { ParameterParseResult .Success (it) }
114120 ? : ParameterParseResult .InvalidParam (rawValue)
115121 }
116- }.also { res ->
117- when (res) {
118- is ParameterParseResult .MissingParam -> errors.add(CommandError .MissingParam (param))
119- is ParameterParseResult .InvalidParam -> errors.add(CommandError .InvalidParam (param, res.rawValue))
120- else -> {}
121- }
122122 }
123- }
124123
125- if (currentTokens.isNotEmpty()) {
126- errors.add(CommandError .TooManyArguments (currentTokens))
124+ argumentMap[param] = result
125+ if (result is ParameterParseResult .MissingParam ) errors.add(CommandError .MissingParam (param))
126+ if (result is ParameterParseResult .InvalidParam ) errors.add(CommandError .InvalidParam (param, result.rawValue))
127127 }
128128
129- val execution = SaltifyCommandExecutionContext (client, event, name, argumentMap )
129+ if (currentTokens.isNotEmpty()) errors.add( CommandError . TooManyArguments (currentTokens) )
130130
131- dsl.requirementBlock?. let { block ->
132- val requirement = SaltifyCommandRequirementContext (execution).block( )
133- if ( ! requirement.satisfies()) return
131+ if (errors.isNotEmpty()) {
132+ dsl.failureBlock?.invoke (execution, errors.first() )
133+ return
134134 }
135135
136136 val startInstant = Clock .System .now()
0 commit comments