1+ /*
2+ * Copyright 2025 Lambda
3+ *
4+ * This program is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+ package com.lambda.command.commands
19+
20+ import com.lambda.brigadier.CommandResult.Companion.failure
21+ import com.lambda.brigadier.CommandResult.Companion.success
22+ import com.lambda.brigadier.argument.greedyString
23+ import com.lambda.brigadier.argument.string
24+ import com.lambda.brigadier.argument.value
25+ import com.lambda.brigadier.execute
26+ import com.lambda.brigadier.executeWithResult
27+ import com.lambda.brigadier.required
28+ import com.lambda.command.CommandRegistry
29+ import com.lambda.command.LambdaCommand
30+ import com.lambda.config.Configuration
31+ import com.lambda.config.Setting
32+ import com.lambda.util.Communication.info
33+ import com.lambda.util.extension.CommandBuilder
34+ import com.lambda.util.text.buildText
35+ import com.lambda.util.text.literal
36+
37+ object PrefixCommand : LambdaCommand(
38+ " prefix" ,
39+ usage = " prefix <prefix>" ,
40+ description = " Sets the prefix for Lambda commands. If the prefix does not seem to work, try putting it in double quotes."
41+ ) {
42+ // i have no idea why someone would want to use some of these as a prefix
43+ // but ig the people who run 20 clients at once could benefit from this
44+ val ptrn = Regex (" ^[!\" #$%&'()*+,\\ -./:;<=>?@\\ [\\\\\\ ]^_`{|}~]$" )
45+
46+ override fun CommandBuilder.create () {
47+ required(greedyString(" prefix" )) { prefixStr ->
48+ executeWithResult {
49+ val prefix = prefixStr().value()
50+ if (! ptrn.matches(prefix)) {
51+ return @executeWithResult failure(" Prefix must be a single non-alphanumeric ASCII character, excluding spaces." )
52+ }
53+ val prefixChar = prefix.first()
54+ val configurable = Configuration .configurableByName(" command" ) ? : return @executeWithResult failure(" No command configurable found." )
55+ val setting = configurable.settings.find { it.name == " prefix" } as ? Setting <* , Char >
56+ ? : return @executeWithResult failure(" Prefix setting is not a Char or can not be found." )
57+ setting.trySetValue(prefixChar)
58+ return @executeWithResult success()
59+ }
60+ }
61+
62+ execute {
63+ info(
64+ buildText {
65+ literal(" The prefix is currently: ${CommandRegistry .prefix} " )
66+ }
67+ )
68+ }
69+ }
70+ }
0 commit comments