Skip to content

Commit 5203c43

Browse files
committed
templates: clean up some of formerly generated markdown
Signed-off-by: Luca Zeuch <[email protected]>
1 parent 84d05ad commit 5203c43

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

content/docs/reference/templates/syntax-and-data.md

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,17 @@ weight = 911
44
description = "Available data in custom commands and a quick syntax refresher."
55
+++
66

7-
Library of base data accessible within custom scripting.
7+
This page lists the available data within your custom commands, such as the user who triggered the command, or the
8+
channel it is running in. For available functions, please see our [list of functions]. In case you need a refresher on
9+
the syntax, please revisit our [learning resources].
810

9-
> "Go is all about type... Type is life." // William Kennedy
10-
11-
## Preface
12-
13-
All available data that can be used in YAGPDB's templating "engine" which is slightly modified version of Go's
14-
stdlib text/template package; more in depth and info about actions, pipelines and global functions like `printf, index,
15-
len,`etc > [https://golang.org/pkg/text/template/](https://golang.org/pkg/text/template/) . This section is meant to be
16-
a concise and to the point reference document for all available templates/functions. **Functions** are covered
17-
in [Function documentation](/docs/reference/templates/functions). For detailed explanations and a syntax guide, please
18-
refer to the [learning resource](/learn/welcome/introduction).
11+
[list of functions]: /docs/reference/templates/functions
12+
[learning resources]: /learn/welcome/introduction
1913

2014
{{< callout context="note" title="Note: Disable \"Smart\" Quotes" icon="outline/info-circle" >}}
2115

2216
Templating system uses standard ASCII quotation marks:
23-
`"` for straight double quotes, `'` for apostrophes or single quotes and`` ` `` for backticks/back quotes; so make
17+
`"` for straight double quotes, `'` for apostrophes or single quotes and `` ` `` for backticks/back quotes; so make
2418
sure no "smart-quotes" are being used.
2519

2620
The difference between back quotes and double quotes in string literals is covered in
@@ -65,11 +59,11 @@ declared, or to the end of custom command if there are no control structures - c
6559
A powerful component of templates is the ability to stack actions - like function calls, together - chaining one after
6660
another. This is done by using pipes `|`. Borrowed from Unix pipes, the concept is simple: each pipeline’s output
6761
becomes the input of the following pipe. One limitation of the pipes is that they can only work with a single value and
68-
that value becomes the last parameter of the next pipeline. \
69-
\
62+
that value becomes the last parameter of the next pipeline.
63+
7064
**Example**: `{{randInt 41| add 2}}` would pipeline`randInt` function's return to addition `add` as second parameter
7165
and it would be added to 2; this more simplified would be like `{{40| add 2}}` with return 42. If written normally, it
72-
would be `{{ add 2 (randInt 41) }}`. Same pipeline but using a variable is also useful one -`{{$x:=40| add 2}}` would
66+
would be `{{ add 2 (randInt 41) }}`. Same pipeline but using a variable is also useful one - `{{$x:=40| add 2}}` would
7367
not return anything as printout, 40 still goes through pipeline to addition and 42 is stored to variable `$x` whereas
7468
`{{($x:=40)| add 2}}` would return 42 and store 40 to `$x`.
7569

@@ -84,7 +78,7 @@ overused**. In most cases, pipes are unnecessary and cause a dip in readability
8478

8579
Context data refers to information accessible via the dot, `{{ . }}`. The accessible data ranges from useful constants
8680
to information regarding the environment in which the custom command was executed, such as the user that ran it, the
87-
channel it was ran in, and so on.
81+
channel it was run in, and so on.
8882

8983
Fields documented as accessible on specific structures, like the context user `.User`, are usable on all values that
9084
share the same type. That is, given a user `$user`, `$user.ID` is a valid construction that yields the ID of the user.
@@ -161,7 +155,7 @@ Similarly, provided a channel `$channel`, `$channel.Name` gives the name of the
161155
| .Guild.OwnerID | Outputs the ID of the owner. |
162156
| .Guild.PreferredLocale | The preferred locale of a guild with the "PUBLIC" feature; used in server discovery and notices from Discord; defaults to "en-US" |
163157
| .Guild.Roles | Outputs all roles and indexing them gives more information about the role. For example `{{len .Guild.Roles}}` gives you how many roles are there in that guild. Role struct has [following fields](https://discordapp.com/developers/docs/topics/permissions#role-object). |
164-
| .Guild.Stickers | A slice of all [sticker objects] in the guild. |
158+
| .Guild.Stickers | A slice of all [sticker objects] in the guild. |
165159
| .Guild.Splash | Outputs the [splash hash](https://discordapp.com/developers/docs/reference#image-formatting) ID of the guild's splash. |
166160
| .Guild.SystemChannelID | The ID of the channel where guild notices such as welcome messages and boost events are posted. |
167161
| .Guild.Threads | Returns all active threads in the guild as a slice of type _\[]dstate.ChannelState_. |
@@ -252,7 +246,7 @@ Interaction functions are covered [here](/docs/reference/templates/functions#int
252246
| .Message.ChannelID | Channel ID this message is in. |
253247
| .Message.Components | Slice of [discordgo.ActionsRow](https://discord.com/developers/docs/interactions/message-components#action-rows)s, which each contain components. Example on indexing the first button or menu under a message: `( index ( index .Message.Components 0 ).Components 0 )` |
254248
| .Message.Content | Text content of this message. |
255-
| .Message.ContentWithMentionsReplaced | Replaces all <@ID> mentions with the username of the mention. |
249+
| .Message.ContentWithMentionsReplaced | Replaces all `<@ID>` mentions with the username of the mention. |
256250
| .Message.EditedTimestamp | The time at which the last edit of the message occurred, if it has been edited. As with .Message.Timestamp, it is of type _discordgo.Timestamp._ |
257251
| .Message.Embeds | Embeds of this message (_slice_ of embed objects). |
258252
| .Message.Flags | Message flags, represented as a bit set. |
@@ -312,7 +306,7 @@ This is available and part of the dot when reaction trigger type is used.
312306
| .ReactionAdded | Returns a boolean type _bool_ true/false indicating whether reaction was added or removed. |
313307
| .ReactionMessage | Returns the message object reaction was added to. Not all regular .Message fields are filled though e.g. .Member. `{{range .ReactionMessage.Reactions}}`<br>`{{.Count}} - {{.Emoji.Name}}` <br>`{{end}}`Returns emoji count and their name.Has an alias `.Message` and it works the same way. |
314308

315-
[Reaction object in Discord documentation](https://discordapp.com/developers/docs/resources/message#reaction-object).\
309+
[Reaction object in Discord documentation](https://discordapp.com/developers/docs/resources/message#reaction-object).
316310
[Emoji object in Discord documentation.](https://discord.com/developers/docs/resources/emoji)
317311

318312
### User
@@ -379,7 +373,7 @@ to `eq`.
379373

380374
Comparison operators always require the same type: i.e comparing `1.23` and `1` would throw **`incompatible types for
381375
comparison`** error as they are not the same type (one is float, the other int). To fix this, you should convert both to
382-
the same type -> for example, `toFloat 1`.
376+
the same type, for example, `toFloat 1`.
383377

384378
{{< /callout >}}
385379

@@ -422,17 +416,18 @@ if one wishes to skip all remaining iterations, the `{{break}}` action may be us
422416

423417
{{< /callout >}}
424418

425-
Affected dot inside `range` is important because methods mentioned above in this documentation:`.Server.ID`,
426-
`.Message.Content` etc are all already using the dot on the pipeline and if they are not carried over to the `range`
427-
control structure directly, these fields do not exists and template will error out. Getting those values inside `range`
428-
and also `with` action would need `$.User.ID` for example.\
429-
\
419+
Affected dot inside `range` is important because methods mentioned above in this documentation like `.Server.ID`,
420+
are all already using the dot on the pipeline and if they are not carried over to the `range` control structure
421+
directly, these fields do not necessarily exists and template will error out. Getting those values inside `range`
422+
and also `with` action would need to use the global syntax (e.g. `$.Server.ID`).
423+
430424
`range` on slices/arrays provides both the index and element for each entry; `range` on map iterates over key/element
431425
pairs. If a `range` action initializes a variable, that variable is set to the successive elements of the iteration.
432426
`range` can also declare two variables, separated by a comma and set by index and element or key and element pair. In
433-
case of only one variable, it is assigned the element.\
434-
\
435-
Like `if`, `range`is concluded with`{{end}}`action and declared variable scope inside `range` extends to that point.\
427+
case of only one variable, it is assigned the element.
428+
429+
Like `if`, `range` is concluded with the `{{end}}` action and declared variable scope inside `range` extends to that
430+
point.
436431

437432
```yag
438433
{{/* range over an integer */}}
@@ -464,7 +459,7 @@ Hello!
464459
```
465460

466461
This program iterates ten *thousand* times, adding a newline and a tab character on every iteration to the output---we
467-
can fix this error by telling the bot to throw away (or "strip") whitespace characters by using the trim indicator `-`:
462+
can fix this error by telling the bot to throw away (or "strip") whitespace characters by using the trim indicator `-`.
468463

469464
```yag
470465
{{ range 10000 }}
@@ -490,10 +485,10 @@ Similar to an `if` action with an associated `else` branch, the `try`-`catch` co
490485
`try` branch and the `catch` branch. First, the code in the `try` branch is ran, and if an error is raised by a function
491486
during execution, the `catch` branch is executed instead with the context (`.`) set to the offending error.
492487

493-
To check for a specific error, one can compare the result of the `Error` method with a predetermined message. (For
488+
To check for a specific error, one can compare the result of the `Error` method with a predetermined message. For
494489
context, all errors have a method `Error` which is specified to return a message describing the reason that the error
495-
was thrown.) For example, the following example has different behavior depending on whether "Reaction blocked" is in the
496-
message of the error caught.
490+
was thrown. The following example has different behavior depending on whether "Reaction blocked" is in the message of
491+
the error caught.
497492

498493
```yag
499494
{{ try }}
@@ -543,8 +538,8 @@ appropriate effect, like in a `range` action.
543538

544539
`with` lets you assign and carry pipeline value with its type as a dot (`.`) inside that control structure, it's like a
545540
shorthand. If the value of the pipeline is empty, dot is unaffected and when an `else` or `else if` action is used,
546-
execution moves on to those branches instead, similar to the `if` action. \
547-
\
541+
execution moves on to those branches instead, similar to the `if` action.
542+
548543
Affected dot inside `with` is important because methods mentioned above in this documentation:`.Server.ID`,
549544
`.Message.Content` etc are all already using the dot on the pipeline and if they are not carried over to the `with`
550545
control structure directly, these fields do not exists and template will error out. Getting those values inside `with`
@@ -593,7 +588,7 @@ To define an associated template, use the `define` action. It has the following
593588

594589
{{< callout context="danger" title="Danger: Associated Templates at Top Level" icon="outline/alert-octagon" >}}
595590

596-
**Warning:** Template definitions must be at the top level of the custom command program; in other words, they cannot be
591+
Template definitions must be at the top level of the custom command program; in other words, they cannot be
597592
nested in other actions (for example, an if action.) That is, the following custom command is invalid:
598593

599594
```yag
@@ -653,7 +648,7 @@ will result in execution of the custom command being stopped at the point the `r
653648

654649
### Execution
655650

656-
To execute a custom command, one of three methods may be used: `template`, `block`, or `execTemplate`.
651+
To execute an associated template, one of three methods may be used: `template`, `block`, or `execTemplate`.
657652

658653
#### Template action
659654

@@ -684,7 +679,7 @@ Below is an example of the `template` action in action:
684679
{{ template "sayHi" "YAG" }} {{/* hi there, YAG */}}
685680
```
686681

687-
Trim markers: `{{- ... -}}`were used in above example because whitespace is considered as part of output for associated
682+
Trim markers: `{{- ... -}}` were used in above example because whitespace is considered as part of output for associated
688683
template definitions (and actions in general).
689684

690685
#### Block action
@@ -853,13 +848,13 @@ You can have max 50 \* user_count (or 500 \* user_count for premium) values in t
853848
new write functions will fail, this value is also cached, so it won't be detected immediately when you go above nor
854849
immediately when you're under again.
855850

856-
Patterns are basic PostgreSQL patterns, not RegEx: An underscore `(_)` matches any single character; a percent sign
857-
`(%)` matches any sequence of zero or more characters.
851+
Patterns are basic PostgreSQL patterns, not RegEx: An underscore `_` matches any single character; a percent sign
852+
`%` matches any sequence of zero or more characters.
858853

859-
Keys can be max 256 bytes long and has to be strings or numbers. Values can be anything, but if their serialized
860-
representation exceeds 100kB a `short write` error gets raised.
854+
Keys can be at most 256 bytes long and has to be strings or numbers. Values can be anything, but if their serialized
855+
representation exceeds 100 kB a `short write` error is raised.
861856

862-
You can just pass a `userID`of 0 to make it global (or any other number, but 0 is safe).
857+
You can just pass a `userID` of 0 to make it global (or any other number, but 0 is safe).
863858

864859
There can be 10 database interactions per CC, out of which dbTop/BottomEntries, dbCount, dbGetPattern, and dbDelMultiple
865860
may only be run twice. (50,10 for premium users).

0 commit comments

Comments
 (0)