$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
$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:
- It checks any subsequent
$elseIf[condition]blocks in order. - If no condition matches, the optional
$elseblock is executed. $endifis always required to close the if-block, even if only$ifand$elseare 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
$endifis mandatory — every$ifmust have exactly one$endif.$elseis optional — at most one per$ifblock.$elseIfis optional — you can chain as many as you need.- Nesting is supported — you can place
$if...$endifinside another$ifblock. - Conditions are evaluated sequentially — the first matching branch wins; subsequent branches are skipped.
Common Pitfalls
- Forgetting
$endifcauses a parse error (“Missing $endif.”). - The block form
$if[condition]...$endifis 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 withMissing $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,$jsonForEachand$funcare still refused in an argument (Unsupported native function $for.). Before 2026-10-22 this form failed withInvalid 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.0is true; the comparison is exact, with no rounding:12345678901234567890==12345678901234567891is false), otherwise as text (10>9abcis false;0x10==16is 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.