Functions are the main tools for performing complex operations in bot scripts. They provide ready-made solutions for tasks such as working with dates, strings, mathematical calculations, and other operations.
A function can be called by name, passing the necessary parameters. It processes these parameters and returns the result.
Example:
$number = math.rand(5, 10) // Call the math.rand function to get a random number between 5 and 10.
Where:
math is the name of the module.rand is the name of the function.To use a function, you need to:
Call format:
module.function(parameters)
Example:
$result = math.sqrt(16) // $result = 4 // Calculate the square root of 16
Function names can be calculated directly during script execution. This allows you to flexibly choose which function to use.
Example:
$funcIdx = math.rand(0, 1) // Get a random number (0 or 1) to select a function
$number = math.((‘min’, ‘max’).get($funcIdx))(3, 7) // Dynamically select and call the min or max function
// Result: either the minimum or maximum value of 3 and 7
Functions cover a wide range of tasks:
$rounded = math.round(3.14159, 2) // $rounded = 3.14// Example: rounding a number
```
2. String processing — working with text.
```python
// Example: converting a string to lowercase
$text = string.toLower(“Hello World!”) // $text = “hello world!”
// Example: getting the current date
$currentDate = datetime.now()
// Example: sorting a list
$sortedList = list.sort([3, 1, 2]) // $sortedList = [1, 2, 3]
// Example: getting the first message from the client
$firstMessage = queue.first().message
math module contains mathematical operations, and the string module contains string operations.