📚
YAGPDB
  • YAGPDB Coding System - Documentation
  • Getting Started
  • Useful Functions
  • Primary Level
    • The Dot and Variables
      • User
      • Guild / Server
      • Member
      • Channel
      • Message
      • Reaction
      • Time
    • Custom Types
    • Functions
      • Type Conversion
      • String Manipulation
      • Math Functions
      • Message Functions
      • Mentions
      • Role Functions
      • Current User
      • Miscellaneous
      • ExecCC
    • Conditional Branching
  • Intermediate Level
  • Database
  • Range Action
  • With Action
  • Custom Embeds
  • Advanced Level
    • Using RegEx
  • Custom Commands Examples
    • Texas Hold'em
Powered by GitBook
On this page
  • Preface
  • Info

Was this helpful?

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.

{{$args:= parseArgs 2 "Syntax is <start number> <stop number+1>."
  (carg "int" "Start number.")
  (carg "int" "Stop number.")}}
{{$data := ""}}
{{range (seq ($args.Get 0) ($args.Get 1)) }}
{{$data = joinStr "" $data "\n" .}}
{{end}}
{{sendMessage 619530135875485706 $data}}

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.

{{$text := parseArgs 3 ""
  (carg "channel" "")
  (carg "int" "")
  (carg "string" "")}}
{{$channel := ($text.Get 0).ID}}
{{$message := ($text.Get 1)}}
{{$len := (len .Args)}}
{{$args := .Args}}
{{$emoji := 3}}

{{if le (len .Args) 23}}
{{range (seq 3 (toInt $len))}}
{{$emojiname := (index $args $emoji)}}
{{if reFind "([a-z])" $emojiname}}
{{$emojiname = (slice $emojiname 1 (sub (len $emojiname) 1))}}
{{addMessageReactions $channel $message $emojiname}}
{{else}}
{{addMessageReactions $channel $message $emojiname}}
{{end}}
{{$emoji = (add 1 ($emoji))}}
{{end}}
{{else}}
{{sendDM "You can only add 20 emojis."}}
{{end}}

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.

{{range (dbTopEntries "lottery" 100 0)}}
{{.User}} --- {{.Value}}
{{end}}

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

rangeiterates 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, rangeis concluded with{{end}}action and declared variable scope inside range extends to that point.

{{/*range over a slice*/}}
{{range $index, $element := cslice "YAGPDB" "IS COOL!"}}
{{$index}} : {{$element}} {{end}}

{{/*range on a map*/}}
{{range $key, $value := dict "SO" "SAY" "WE" "ALL!"}}
{{$key}} : {{$value}} {{end}}
{{/*range with else and variable scope*/}}
{{range seq 1 1}} no output {{else}} output here {{end}}
{{$x := 42}} {{range $x := seq 2 4}} {{$x}} {{end}} {{$x}}

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!

PreviousDatabaseNextWith Action

Last updated 4 years ago

Was this helpful?

Go back and see the codes. Do you understand what the range function does now?

here