Skip to content
Control Flow if

$if / $elseIf / $else / $endif — Conditional Branching

The $if family of tokens provides conditional branching in BDFD scripts. These are structural tokens handled at the BDFD parser level, not dispatched as regular function calls. They allow you to execute different blocks of commands based on runtime conditions.

Syntax

bdfd
$if[condition].. $elseIf[condition].. $else.. $endif

How It Works

When the parser encounters a $if[condition] token, it evaluates the condition. If the condition is true, it executes the block immediately following $if, up to the next $elseIf, $else, or $endif. If the condition is false, it skips forward:

  1. It checks any subsequent $elseIf[condition] blocks in order.
  2. If no condition matches, the optional $else block is executed.
  3. $endif is always required to close the if-block, even if only $if and $else are used.

Condition Syntax

The condition text is first evaluated (functions are expanded), then interpreted. It must either be exactly true or false, or contain a comparison left operator right; anything else raises the error Invalid condition. The operator is selected outside Discord tokens, in this order: == / !=, then >= / <=, then > / <, taking the first occurrence within the selected group. If both sides are numbers they are compared numerically, otherwise they are compared as text (lexicographic order).

Supported Comparison Operators

Operator Meaning Example
== Equal to $getUserVar[score]==100
!= Not equal to $getUserVar[name]!=Guest
> Greater than $getUserVar[coins]>0
< Less than $getUserVar[hp]<25
>= Greater or equal $getUserVar[level]>=10
<= Less or equal $getUserVar[wins]<=3

Logical Operators

Combine multiple conditions with $and and $or. Each takes 1 to 100 conditions and returns true or false:

$if[$and[$getUserVar[gold]>=100;$getUserVar[rank]>=5]==true]
$if[$or[$checkContains[$message;ping];$checkContains[$message;pong]]==true]

Inline Check Functions

Use $checkCondition[condition] (one condition, returns true or false) or $checkContains[text;phrase;...] inside an $if:

$if[$checkCondition[$getUserVar[age]>=18]==true]
$if[$checkContains[$message;admin]==true]

Inline Form

With two or three arguments, $if[condition;then;else] is a regular function: it returns then when the condition is true, otherwise else (an empty string if else is omitted). It needs no $endif.

Structural Rules

  • $endif is mandatory — every $if must have exactly one $endif.
  • $else is optional — at most one per $if block.
  • $elseIf is optional — you can chain as many as you need.
  • Nesting is supported — you can place $if...$endif inside another $if block.
  • Conditions are evaluated sequentially — the first matching branch wins; subsequent branches are skipped.

Common Pitfalls

  • Forgetting $endif causes a parse error (“Missing $endif.”).
  • The block form $if[condition]...$endif is accepted inside the argument of another function, for example $title[$if[$var[x]==1]one$else other$endif] or $description[Total $if[$var[n]>0]more$endif]. The block closes inside its own argument: a ; ends the argument, so $description[$if[1==1]a;b$endif] fails with Missing $endif.. Write \; for a literal semicolon ($description[$if[1==1]a\;b$endif]). Blocks can be nested and mixed with the inline form. Loops ($for, $loop, $while), $try, $jsonForEach and $func are still refused in an argument (Unsupported native function $for.). Before 2026-10-22 this form failed with Invalid argument count for $if: 1..
  • Using = instead of == for equality — BDFD requires double equals.
  • Both sides are compared as numbers when they are finite decimals (1==1.0 is true; the comparison is exact, with no rounding: 12345678901234567890==12345678901234567891 is false), otherwise as text (10>9abc is false; 0x10==16 is false because hexadecimal is not a number).
  • The operator is searched outside Discord tokens (<@id>, <@&id>, <#id>, <:name:id>, <t:...>, </cmd:id>), so <@1>!=<@1> is false. Priority: == and !=, then >= and <=, then > and <, at the first occurrence.
  • A condition without a comparison operator (other than true / false) is an error.

Examples

Conditional Response Flow

$if[$getUserVar[coins]>=100]
  $title[Purchase Successful]
  $description[You bought the VIP pass for 100 coins!]
  $color[#57F287]
$else
  $title[Insufficient Funds]
  $description[You need 100 coins, but only have **$getUserVar[coins]**.]
  $color[#ED4245]
$endif

Try it

Paste an example into the playground to preview its message (functions that act on Discord are not run), or build the same thing with blocks.