Control Flow onlyIf

$onlyIf[]

Condition guard that stops command execution if the condition evaluates to false. Optionally sends an error message before stopping.

Syntax
$onlyIf[condition] or $onlyIf[condition;errorMessage]

$onlyIf is the fundamental guard function in BDFD. It acts as a gatekeeper: if the condition passes, the command continues; if it fails, the command stops dead. This is the building block for permissions checks, input validation, and any scenario where you need to abort early.

How It Works

  1. The condition is evaluated as a boolean expression.
  2. If the condition is true → execution continues to the next line.
  3. If the condition is false → the optional errorMessage is sent (if provided), then execution stops immediately via BotCreatorActionType.stop. No further code in the command runs.

Two-Argument Form

$onlyIf[condition;errorMessage]

When errorMessage is provided and the condition fails, the message is sent to the channel before stopping. This is the recommended form — it gives feedback to the user about why the command failed.

Single-Argument Form

$onlyIf[condition]

When no error message is provided and the condition fails, execution stops silently. The user receives no response. Use this when you want to silently reject invalid input without cluttering the chat.

Common Patterns

Permission Guards

$onlyIf[$hasPerms[$authorID;BanMembers];❌ BanMembers permission required.]
$onlyIf[$authorID!=$mentioned[1];❌ You cannot ban yourself.]

Input Validation

$onlyIf[$isNumber[$message]==true;❌ Please enter a number.]
$onlyIf[$message>=1;❌ The number must be >= 1.]
$onlyIf[$message<=100;❌ The number must be <= 100.]

Channel/Role Restrictions

$onlyIf[$channelID==123456789012345678;❌ This command can only be used in <#123456789012345678>.]

Comparison with $if / $stop

Without $onlyIf:

$if[$hasPerms[$authorID;Administrator]==false]
❌ Permission denied.
$stop
$endif

With $onlyIf (equivalent, cleaner):

$onlyIf[$hasPerms[$authorID;Administrator];❌ Permission denied.]

$onlyIf is the idiomatic way to write guards — it is shorter, more readable, and signals intent clearly: “only continue if this condition holds.”