TextCommandFilter

@InterfacedService(acceptMultiple = true)
interface TextCommandFilter : Filter(source)

Prevents text command execution by returning an error object to the command executor.

Filters run when a command variation is about to be executed, i.e., after the permissions/rate limits... were checked.

Combining filters

Filters can be combined with and/or (static methods for Java users).

Requirements

  • Register your instance as a service with @BService. This is not required if you pass the instance directly to the command builder.

  • Implement either check (Java) or checkSuspend (Kotlin).

  • (Optional) Set your filter as a command-specific filter by disabling global.

Execution order

The execution order of global filters is determined by the priority of the service, while command-specific filters use the insertion order.

Example - Accepting commands only in a single channel

Note: For the example's sake, I will reply directly on each failed condition, however, I recommend having a separate function/class to handle rejections, as to not duplicate code on each rejection case.

@BService
class MyTextCommandFilter : TextCommandFilter {

override val global: Boolean get() = true

override suspend fun checkSuspend(
event: MessageReceivedEvent,
commandVariation: TextCommandVariation,
args: String
): String? {
if (event.channel.idLong != 722891685755093076) {
event.message.reply("Can only run commands in <#722891685755093076>").await()
return "Wrong channel"
}
return null
}
}

@BService
public class MyTextCommandFilter implements TextCommandFilter {

@Override
public boolean getGlobal() {
return true;
}

@Nullable
@Override
public String check(@NotNull MessageReceivedEvent event, @NotNull TextCommandVariation commandVariation, @NotNull String args) {
if (event.getChannel().getIdLong() != 722891685755093076L) {
event.getMessage().reply("Can only run commands in <#722891685755093076>").queue();
return "Wrong channel";
}
return null;
}
}

See also

@InterfacedService

Properties

Link copied to clipboard

Description of the filter, used for logging purposes (like when a filter rejects a command).

Link copied to clipboard
abstract val global: Boolean

Whether this filter is global or command-specific.

Functions

Link copied to clipboard
Link copied to clipboard
open fun check(event: MessageReceivedEvent, commandVariation: TextCommandVariation, args: String): String?

Checks if this text command should run, returns null if this filter passes, or a reason for the rejection, used for logging purposes.

Link copied to clipboard
open suspend fun checkSuspend(event: MessageReceivedEvent, commandVariation: TextCommandVariation, args: String): String?

Checks if this text command should run, returns null if this filter passes, or a reason for the rejection, used for logging purposes.

Link copied to clipboard