Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/main/kotlin/com/lambda/command/commands/PrefixCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2025 Lambda
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.lambda.command.commands

import com.lambda.brigadier.CommandResult.Companion.failure
import com.lambda.brigadier.CommandResult.Companion.success
import com.lambda.brigadier.argument.string
import com.lambda.brigadier.argument.value
import com.lambda.brigadier.execute
import com.lambda.brigadier.executeWithResult
import com.lambda.brigadier.required
import com.lambda.command.CommandRegistry
import com.lambda.command.LambdaCommand
import com.lambda.config.Configuration
import com.lambda.config.Setting
import com.lambda.util.Communication.info
import com.lambda.util.extension.CommandBuilder
import com.lambda.util.text.buildText
import com.lambda.util.text.literal

object PrefixCommand : LambdaCommand("prefix",
usage = "prefix <prefix>",
description = "Sets the prefix for Lambda commands. If the prefix does not seem to work, try putting it in double quotes."
) {
// i have no idea why someone would want to use some of these as a prefix
// but ig the people who run 20 clients at once could benefit from this
val ptrn = Regex("^[!\"#$%&'()*+,\\-./:;<=>?@\\[\\\\\\]^_`{|}~]$")
Comment thread
KMatias123 marked this conversation as resolved.
override fun CommandBuilder.create() {
required(string("prefix")) { prefixStr ->
executeWithResult {
val prefix = prefixStr().value()
if (!ptrn.matches(prefix)) {
return@executeWithResult failure("Prefix must be a single non-alphanumeric ASCII character, excluding spaces.")
}
val prefixChar = prefix.first()
val configurable = Configuration.configurableByName("command")?: return@executeWithResult failure("No command configurable found.")
Comment thread
KMatias123 marked this conversation as resolved.
Outdated
val setting = (configurable.settings.find { it.name == "prefix" }?: return@executeWithResult failure("No prefix setting found.")) as Setting<*, Char>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can safe cast using as? instead of wrapping the setting with parenthesis and returning if it is null. Instead you safe cast and returns if the cast is also null

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is correct now right?

setting.trySetValue(prefixChar)
return@executeWithResult success()
}
}

execute {
[email protected](
Comment thread
KMatias123 marked this conversation as resolved.
Outdated
buildText {
literal("The prefix is currently: ${CommandRegistry.prefix}")
}
)
}
}
}