📚
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

Was this helpful?

With Action

It will shorten your codes but it may make the codes harder to read for new coders

with lets you assign and carry pipeline value with its type as a dot . inside that control structure, it's like a shorthand. If the value of the pipeline is empty, dot is unaffected and when {{else}} is used, that branch is executed instead.

Affected dot inside with 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 with control structure directly, these fields do not exists and template will error out. Getting those values inside with and also range action would need $.User.ID for example.

Like if and range actions, with is concluded using {{end}} and variable scope extends to that point.

  • with seems easy, but it may confuse novice coders when your command is complex.

{{/* Shows the scope and how dot is affected by object's value in pipeline */}}
{{ $x := "42" }} {{ with and ($z:= seq 0 5) ($x := seq 0 10) }} 
len $x: `{{ len $x }}` 
{{/* "and" function uses $x as last value for dot */}}
same as len dot: `{{ len . }}` 
but len $z is `{{ len $z }}` {{ end }}
Outer-scope $x len however: {{ len $x }}
{{/* when there's no value, dot is unaffected */}}
{{ with false }} dot is unaffected {{ else }} printing here {{ .CCID }} {{ end }}

Example

  • To demonstrate the simple usage of with:

{{with .CmdArgs}}
{{/*output the arguments after the trigger*/}}
{{.}}
{{else}}
{{/*output "Nothing" if there is no argument after the trigger*/}}
Nothing
{{end}}
  • To demonstrate how $ works inside with block:

{{$yag := (getMember 204255221017214977)}}
{{with $yag}}
{{.User.Mention}} versus {{$.User.Mention}}
{{end}}

The . inside the with block will be referred as what the (getMember 204255221017214977) outputs. .User.Mention here will mention YAGPDB, while $.User.Mention will mention the user.

  • Harder example of with:

{{$x := (randInt 10)}}
{{with (add $x 1)}}
	{{$y := add 1 .}}
	{{with $y}}
		{{$z := sub . 3}}
		{{with $z}}
			{{if gt . 5}}
				{{with (add 10 .)}}
					{{sendMessage nil (joinStr " " "Inside the `with (add 10 .)`, it will output" .)}}
				{{end}}
				{{sendMessage nil (joinStr " " "after the `with (add 10 .)`, it will output" .)}}
			{{else}}
				{{sendMessage nil (joinStr " " "`else` part of the `if gt . 5`, it will output" .)}}
			{{end}}
			{{sendMessage nil (joinStr " " "`$z` is" .)}}
		{{end}}
		{{sendMessage nil (joinStr " " "`$y` is" .)}}
	{{end}}
	{{sendMessage nil (joinStr " " "after `with (add $x 1)`, it will output" .)}}
{{end}}
{{sendMessage nil (joinStr " " "Original `$x` is" $x)}}

Though this command doesn't need any with in fact, you can spot that it is quite hard to read the codes when there are too many with functions inside a command. Putting too many with functions inside a command will be a hell for experts and novices alike to read.

PreviousRange ActionNextCustom Embeds

Last updated 4 years ago

Was this helpful?