Range Action
Useful when you want to do same things
Preface
If you want the bot to count from 300 numbers together, what can you do --- use range
. range
allows you to make the bot do the same thing again and again, such as indexing things, sending the same message, getting all of the fields of something, and others.
Take a look at the following codes using range
action to do the same things.
We use the range
to make the bot count automatically.
Let's see the following codes using range
action to index things from a slice.
This time, we use range
to index the emojis the member sends one by one and then make the bot react those emojis on a specific message in a specific channel.
Let's see how range
works when you want to get all of the fields of something.
We range over databases and get the user and the value of each database.
After reading the contents above, you must know that range
can be used in many places. Most important of all, many functions, such as dbTopEntries
, can be combined with range
to get more info.
Info
range
iterates over element values in variety of data structures in pipeline - slices/arrays, maps or channels. The dot .
is set to successive elements of those data structures and output will follow execution. If the value of pipeline has zero length, nothing is output or if an {{else}}
action is used, that section will be executed.
Affected dot inside range
is important because methods mentioned above in this documentation:.Server.ID
, .Message.Content
etc are all already using the dot on the pipeline and if they are not carried over to the range
control structure directly, these fields do not exists and template will error out. Getting those values inside range
and also with
action would need $.User.ID
for example.
range
on slices/arrays provides both the index and element for each entry; range on map iterates over key/element pairs. If a range action initializes a variable, that variable is set to the successive elements of the iteration. Range can also declare two variables, separated by a comma and set by index and element or key and element pair. In case of only one variable, it is assigned the element.
Like if
, range
is concluded with{{end}}
action and declared variable scope inside range
extends to that point.
Go back here and see the codes. Do you understand what the range
function does now?
We range over the database and get some information like the user and the value by {{.User}} and {{.Value}}. Also, you can get more information like the user's id.
range
is a very useful function when you make complex but repeating parts like the dbTopEntries
or something else. But it is different from the for
loop in C++. Don't get confused!
Last updated
Was this helpful?