Values, conversion and missing data
Values have a kind, such as text, a number or a list. The kind determines how a value appears in a message and which operations can use it.
| Value | Example | Emission |
|---|---|---|
| nil | nil |
Empty text |
| Boolean | true, false |
true or false |
| Number | 12, 1.25, 2e3 |
Invariant decimal text |
| String | "Hello" |
Its text |
| List | [1, 2, 3] |
Format explicitly, for example with $join |
| Map | { count: 2 } |
Read a key, such as @args.count |
| Discord entity | @user |
Its defined display, such as a mention |
Lists keep items in order, as in ["Music", "Drawing"]. Maps store values by key, as in { room: 2, shift: "morning" }. Quote keys that contain spaces. If @schedule holds that map, @schedule.room and @schedule["room"] both give 2. Use $join or $each to put the items in a message.
Common conversions
Section titled “Common conversions”An expression can emit a number next to ordinary template text. Multiplication also accepts numeric text when it can be converted.
$("2.5" * 4)Total: $(10)$upper(12)Result:
10Total: 1012Numeric text uses a decimal point without thousands separators, with optional whitespace around the number. Malformed text causes an error instead of becoming zero. Infinity, NaN and numeric text above 9,007,199,254,740,991 are also rejected.
Use + to add numbers, concatenate strings or lists, or append a value to a list. "2" + 2 gives "22", and ["Music"] + "Drawing" gives ["Music", "Drawing"]. # counts list items, map keys, or Unicode scalar values. in finds a list value, map key, or substring.
Calculations use double arithmetic, which is unsuitable for exact currency calculations. Discord IDs stay in entities or strings so that their digits remain exact; obtain one with .id and keep it out of arithmetic.
Lists compare by item and order, maps by keys and values. Membership uses the same comparison.
Comparing values
Section titled “Comparing values”Equality distinguishes kinds, so 1 == "1" is false even though the string can be converted for a calculation. Ordering two strings uses ordinal character order, while compatible numbers and numeric text are compared numerically. The bot’s language settings do not change these comparisons.
Missing is different from unknown
Section titled “Missing is different from unknown”An optional argument, a missing map key or an index outside a list can yield nil. Use ?? to supply text for that case:
$(@args.optional ?? "Not supplied")Result:
Not suppliedAn unknown reference reports a spelling or scope error in text and in expressions. Use $("@nickname") to display the spelling as text. An available name with no value yields nil.
Reading through a nil value returns nil without evaluating the index. List indexes start at zero, and map keys are case-sensitive. Read Discord properties directly, such as @user.name and @channel.id.
Truthiness and fallback
Section titled “Truthiness and fallback”false, nil, zero, empty text, and empty collections are false in conditions. Use $if(#@items) to check whether a list has entries. Use $if(@count > 0) if negative numbers should also fail.
The fallback operator ?? has a narrower purpose: it replaces nil without replacing other values that might be useful in the message.
$(false ?? "fallback")$(nil ?? "fallback")Result:
falsefallbackIn the first line, false is a supplied value and stays in the result. The second line uses the fallback because its value is missing.