Working with data in bot scripts is based on various data types. This allows you to conveniently organize information and perform calculations.
All numbers in the system are represented as real numbers (with a floating point). This means that they can contain fractional parts. The minimum and maximum values of numbers depend on the environment in which the interpreter is running.
Example:
$x = 2.5 * 4 // Simple calculation with numbers
Strings are sequences of characters enclosed in single ' or double " quotes.
Important difference: special characters can be used inside double quotes, which are interpreted in a special way:
| Special sequence | Description |
|---|---|
\n |
Line break |
\r |
Carriage return |
\t |
Horizontal tab |
\" |
Double quotation mark |
Within single quotation marks, characters are interpreted literally, with the exception of the sequence \', which means a single quotation mark.
Example:
say(“Hello\n\”bear!\“”) // Double quotes: support for special characters
say(‘Hello\n\'bear!\’') // Single quotes: everything is interpreted as is
Boolean values have only two states: true (true) and false (false). They are often used in conditional expressions.
Example:
$x = true // $x contains TRUE
$y = !$x // $y contains FALSE (inversion of value)
Objects are complex data structures with built-in properties and methods.
A dot (.) is used to access properties and methods. For example, you can get the text of the first client message from the queue using the ClientMessage object.
Example:
$first = queue.first() // Get the ClientMessage object
$firstMessage = $first.message // Access the message property
You can also dynamically call object methods.
Example:
// Random method selection
$n = rand(0, 1)
$method = [“first”, “last”].get($n)
// Call the method
$firstOrLast = queue.$method
A tuple is an ordered and immutable collection. Tuple elements can be retrieved but cannot be modified.
They are created using parentheses.
Example:
$items = (1, 2, 3) // Tuple of three elements
$empty = () // Empty tuple
$single = (‘a’,) // Tuple of one element (comma is required)
Methods for working with tuples:
.count() — number of elements..first() — first element..last() — last element..get(index) — element by index.Example:
$count = $items.count() // Number of elements
$first = $items.first() // First element
A list is a mutable collection that can be added to, modified, and cleared.
They are created using square brackets.
Example:
$items = [1, 2, 3] // List of three elements
$empty = [] // Empty list
Methods for working with lists:
.append(item) — add an element to the end..prepend(item) — add an element to the beginning..get(index) — element by index..clear() — delete all elements.Example:
$items.append(4) // Add an element
$first = $items.get(0) // Get the first element
A dictionary is a collection of key-value pairs. They are created using curly brackets.
Example:
$map = {‘a’: 1, ‘b’: 2} // Dictionary with two pairs
$empty = {} // Empty dictionary
Methods for working with dictionaries:
.get(key) — value by key..set(key, value) — set value by key..keys() — list of all keys..values() — list of all values.Example:
$value = $map.get(‘a’) // Get value by key
$map.set(‘c’, 3) // Add a new pair
nil is a special value that means “nothing.” It is used for uninitialized variables or to explicitly indicate the absence of data.
Example:
$x = nil // Assign nil