Skip to content

Commit 0ad0075

Browse files
committed
Пример поддержки DSL настройки команд
1 parent e6499af commit 0ad0075

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package iris.tg.command
2+
3+
import iris.tg.event.Message
4+
5+
/**
6+
* @created 31.10.2020
7+
* @author [Ivan Ivanov](https://vk.com/irisism)
8+
*/
9+
fun commands(initializer: DslCommandBuilder.() -> Unit): List<Pair<CommandMatcher, CharArray?>> {
10+
return DslCommandBuilder().apply(initializer).build()
11+
}
12+
13+
open class DslCommandBuilder {
14+
15+
private val commands = mutableListOf<Pair<CommandMatcher, CharArray?>>()
16+
17+
infix fun String.to(command: (message: Message) -> Unit) {
18+
commands += CommandMatcherSimple(this, command).let { it to it.hashChars() }
19+
}
20+
21+
infix fun String.to(command: Command) {
22+
commands += CommandMatcherSimple(this, command).let { it to it.hashChars() }
23+
}
24+
25+
infix fun RegexBuilder.to(command: (message: Message, params: List<String>) -> Unit) {
26+
commands += CommandMatcherRegex(Regex(this.pattern), command) to this.hashChars
27+
}
28+
29+
infix fun RegexBuilder.to(command: CommandMatcherRegex.CommandRegex) {
30+
commands += CommandMatcherRegex(Regex(this.pattern), command) to this.hashChars
31+
}
32+
33+
fun regex(template: String, hashChars: String? = null) = regex(template, hashChars?.toCharArray())
34+
35+
fun regex(template: String, hashChars: CharArray?): RegexBuilder {
36+
return RegexBuilder(template, hashChars)
37+
}
38+
39+
class RegexBuilder(val pattern: String, val hashChars: CharArray?)
40+
41+
fun build(): List<Pair<CommandMatcher, CharArray?>> {
42+
return commands
43+
}
44+
}

0 commit comments

Comments
 (0)