Skip to content

Commit 5b62757

Browse files
committed
templates: clean up some of formerly generated markdown
Signed-off-by: Luca Zeuch <[email protected]>
1 parent b5e533a commit 5b62757

File tree

1 file changed

+37
-43
lines changed

1 file changed

+37
-43
lines changed

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

Lines changed: 37 additions & 43 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.
@@ -127,7 +121,6 @@ Similarly, provided a channel `$channel`, `$channel.Name` gives the name of the
127121
| .Channel.Topic | The topic of the channel. |
128122
| .Channel.Type | The type of the channel. Explained further in [Discord's channel documentation](https://discord.com/developers/docs/resources/channel#channel-object-channel-types) |
129123

130-
131124
[Channel object in Discord documentation](https://discordapp.com/developers/docs/resources/channel#channel-object).
132125

133126
[Channel functions documentation](functions#channel).
@@ -162,7 +155,7 @@ Similarly, provided a channel `$channel`, `$channel.Name` gives the name of the
162155
| .Guild.OwnerID | Outputs the ID of the owner. |
163156
| .Guild.PreferredLocale | The preferred locale of a guild with the "PUBLIC" feature; used in server discovery and notices from Discord; defaults to "en-US" |
164157
| .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). |
165-
| .Guild.Stickers | A slice of all [sticker objects] in the guild. |
158+
| .Guild.Stickers | A slice of all [sticker objects] in the guild. |
166159
| .Guild.Splash | Outputs the [splash hash](https://discordapp.com/developers/docs/reference#image-formatting) ID of the guild's splash. |
167160
| .Guild.SystemChannelID | The ID of the channel where guild notices such as welcome messages and boost events are posted. |
168161
| .Guild.Threads | Returns all active threads in the guild as a slice of type _\[]dstate.ChannelState_. |
@@ -253,7 +246,7 @@ Interaction functions are covered [here](/docs/reference/templates/functions#int
253246
| .Message.ChannelID | Channel ID this message is in. |
254247
| .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 )` |
255248
| .Message.Content | Text content of this message. |
256-
| .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. |
257250
| .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._ |
258251
| .Message.Embeds | Embeds of this message (_slice_ of embed objects). |
259252
| .Message.Flags | Message flags, represented as a bit set. |
@@ -303,7 +296,7 @@ This is available and part of the dot when reaction trigger type is used.
303296
| .ReactionAdded | Returns a boolean type _bool_ true/false indicating whether reaction was added or removed. |
304297
| .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. |
305298

306-
[Reaction object in Discord documentation](https://discordapp.com/developers/docs/resources/message#reaction-object).\
299+
[Reaction object in Discord documentation](https://discordapp.com/developers/docs/resources/message#reaction-object).
307300
[Emoji object in Discord documentation.](https://discord.com/developers/docs/resources/emoji)
308301

309302
### User
@@ -356,7 +349,7 @@ to `eq`.
356349

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

361354
{{< /callout >}}
362355

@@ -399,17 +392,18 @@ if one wishes to skip all remaining iterations, the `{{break}}` action may be us
399392

400393
{{< /callout >}}
401394

402-
Affected dot inside `range` is important because methods mentioned above in this documentation:`.Server.ID`,
403-
`.Message.Content` etc are all already using the dot on the pipeline and if they are not carried over to the `range`
404-
control structure directly, these fields do not exists and template will error out. Getting those values inside `range`
405-
and also `with` action would need `$.User.ID` for example.\
406-
\
395+
Affected dot inside `range` is important because methods mentioned above in this documentation like `.Server.ID`,
396+
are all already using the dot on the pipeline and if they are not carried over to the `range` control structure
397+
directly, these fields do not necessarily exists and template will error out. Getting those values inside `range`
398+
and also `with` action would need to use the global syntax (e.g. `$.Server.ID`).
399+
407400
`range` on slices/arrays provides both the index and element for each entry; `range` on map iterates over key/element
408401
pairs. If a `range` action initializes a variable, that variable is set to the successive elements of the iteration.
409402
`range` can also declare two variables, separated by a comma and set by index and element or key and element pair. In
410-
case of only one variable, it is assigned the element.\
411-
\
412-
Like `if`, `range`is concluded with`{{end}}`action and declared variable scope inside `range` extends to that point.\
403+
case of only one variable, it is assigned the element.
404+
405+
Like `if`, `range` is concluded with the `{{end}}` action and declared variable scope inside `range` extends to that
406+
point.
413407

414408
```yag
415409
{{/* range over an integer */}}
@@ -441,7 +435,7 @@ Hello!
441435
```
442436

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

446440
```yag
447441
{{ range 10000 }}
@@ -467,10 +461,10 @@ Similar to an `if` action with an associated `else` branch, the `try`-`catch` co
467461
`try` branch and the `catch` branch. First, the code in the `try` branch is ran, and if an error is raised by a function
468462
during execution, the `catch` branch is executed instead with the context (`.`) set to the offending error.
469463

470-
To check for a specific error, one can compare the result of the `Error` method with a predetermined message. (For
464+
To check for a specific error, one can compare the result of the `Error` method with a predetermined message. For
471465
context, all errors have a method `Error` which is specified to return a message describing the reason that the error
472-
was thrown.) For example, the following example has different behavior depending on whether "Reaction blocked" is in the
473-
message of the error caught.
466+
was thrown. The following example has different behavior depending on whether "Reaction blocked" is in the message of
467+
the error caught.
474468

475469
```yag
476470
{{ try }}
@@ -520,8 +514,8 @@ appropriate effect, like in a `range` action.
520514

521515
`with` lets you assign and carry pipeline value with its type as a dot (`.`) inside that control structure, it's like a
522516
shorthand. If the value of the pipeline is empty, dot is unaffected and when an `else` or `else if` action is used,
523-
execution moves on to those branches instead, similar to the `if` action. \
524-
\
517+
execution moves on to those branches instead, similar to the `if` action.
518+
525519
Affected dot inside `with` is important because methods mentioned above in this documentation:`.Server.ID`,
526520
`.Message.Content` etc are all already using the dot on the pipeline and if they are not carried over to the `with`
527521
control structure directly, these fields do not exists and template will error out. Getting those values inside `with`
@@ -570,7 +564,7 @@ To define an associated template, use the `define` action. It has the following
570564

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

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

576570
```yag
@@ -630,7 +624,7 @@ will result in execution of the custom command being stopped at the point the `r
630624

631625
### Execution
632626

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

635629
#### Template action
636630

@@ -661,7 +655,7 @@ Below is an example of the `template` action in action:
661655
{{ template "sayHi" "YAG" }} {{/* hi there, YAG */}}
662656
```
663657

664-
Trim markers: `{{- ... -}}`were used in above example because whitespace is considered as part of output for associated
658+
Trim markers: `{{- ... -}}` were used in above example because whitespace is considered as part of output for associated
665659
template definitions (and actions in general).
666660

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

833-
Patterns are basic PostgreSQL patterns, not RegEx: An underscore `(_)` matches any single character; a percent sign
834-
`(%)` matches any sequence of zero or more characters.
827+
Patterns are basic PostgreSQL patterns, not RegEx: An underscore `_` matches any single character; a percent sign
828+
`%` matches any sequence of zero or more characters.
835829

836-
Keys can be max 256 bytes long and has to be strings or numbers. Values can be anything, but if their serialized
837-
representation exceeds 100kB a `short write` error gets raised.
830+
Keys can be at most 256 bytes long and has to be strings or numbers. Values can be anything, but if their serialized
831+
representation exceeds 100 kB a `short write` error is raised.
838832

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

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

0 commit comments

Comments
 (0)