AdvancedTagScript 3.2.4__py3-none-any.whl → 3.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,6 +55,11 @@ from .block import (
55
55
  CountBlock as CountBlock,
56
56
  LengthBlock as LengthBlock,
57
57
  CooldownBlock as CooldownBlock,
58
+ JoinBlock as JoinBlock,
59
+ ListBlock as ListBlock,
60
+ CycleBlock as CycleBlock,
61
+ OrdinalBlock as OrdinalBlock,
62
+
58
63
  )
59
64
  from .interface import (
60
65
  Adapter as Adapter,
@@ -97,6 +102,7 @@ __all__: Tuple[str, ...] = (
97
102
  "helper_parse_if",
98
103
  "helper_parse_list_if",
99
104
  "helper_split",
105
+ "AllowedMentionsBlock",
100
106
  "AllBlock",
101
107
  "AnyBlock",
102
108
  "AssignmentBlock",
@@ -127,6 +133,11 @@ __all__: Tuple[str, ...] = (
127
133
  "LowerBlock",
128
134
  "CountBlock",
129
135
  "LengthBlock",
136
+ "JoinBlock",
137
+ "ListBlock",
138
+ "CycleBlock",
139
+ "OrdinalBlock",
140
+
130
141
  "SafeObjectAdapter",
131
142
  "StringAdapter",
132
143
  "IntAdapter",
@@ -170,7 +181,7 @@ __all__: Tuple[str, ...] = (
170
181
  )
171
182
 
172
183
 
173
- __version__: Final[str] = "3.2.4"
184
+ __version__: Final[str] = "3.3.0"
174
185
 
175
186
 
176
187
  class VersionNamedTuple(NamedTuple):
@@ -103,7 +103,7 @@ class RedBotAdapter(SimpleAdapter["Red"]):
103
103
  Percentage of chunked guilds the bot has.
104
104
 
105
105
  .. warning::
106
- Attributes denoting `(*)` can only be used by the bot owner.
106
+ Attributes denoting ``(*)`` can only be used by the bot owner.
107
107
  """
108
108
 
109
109
  if not _has_redbot:
@@ -3,13 +3,12 @@ from __future__ import annotations
3
3
  from typing import Tuple
4
4
 
5
5
  # isort: off
6
- from .helpers import (
7
- implicit_bool as implicit_bool,
8
- helper_parse_if as helper_parse_if,
9
- helper_parse_list_if as helper_parse_list_if,
10
- helper_split as helper_split,
11
- easier_helper_split as easier_helper_split,
12
- )
6
+ from .helpers import (
7
+ implicit_bool as implicit_bool,
8
+ helper_parse_if as helper_parse_if,
9
+ helper_parse_list_if as helper_parse_list_if,
10
+ helper_split as helper_split,
11
+ )
13
12
 
14
13
  # isort: on
15
14
  from .allowedmentions import (
@@ -89,14 +88,25 @@ from .count import (
89
88
  CountBlock as CountBlock,
90
89
  LengthBlock as LengthBlock,
91
90
  )
91
+ from .joinblock import (
92
+ JoinBlock as JoinBlock,
93
+ )
94
+ from .listblock import (
95
+ ListBlock as ListBlock,
96
+ )
97
+ from .cycleblock import (
98
+ CycleBlock as CycleBlock,
99
+ )
100
+ from .ordblock import (
101
+ OrdinalBlock as OrdinalBlock,
102
+ )
92
103
 
93
104
  __all__: Tuple[str, ...] = (
94
105
  "implicit_bool",
95
- "helper_parse_if",
96
- "helper_parse_list_if",
97
- "helper_split",
98
- "easier_helper_split",
99
- "AllowedMentionsBlock",
106
+ "helper_parse_if",
107
+ "helper_parse_list_if",
108
+ "helper_split",
109
+ "AllowedMentionsBlock",
100
110
  "AllBlock",
101
111
  "AnyBlock",
102
112
  "AssignmentBlock",
@@ -127,4 +137,8 @@ __all__: Tuple[str, ...] = (
127
137
  "LowerBlock",
128
138
  "CountBlock",
129
139
  "LengthBlock",
140
+ "JoinBlock",
141
+ "ListBlock",
142
+ "CycleBlock",
143
+ "OrdinalBlock",
130
144
  )
@@ -12,9 +12,15 @@ __all__: Tuple[str, ...] = ("AssignmentBlock",)
12
12
 
13
13
  class AssignmentBlock(verb_required_block(False, parameter=True)): # type: ignore
14
14
  """
15
- Variables are useful for choosing a value and referencing it later in a tag.
15
+ Variables are useful for storing a value and referencing it later in a tag.
16
16
  Variables can be referenced using brackets as any other block.
17
17
 
18
+ .. note::
19
+ - Variables are not parsed by default. You must use the ``parse`` method to parse a variable.
20
+ - They are one of the most versatile and powerful features of TagScript.
21
+ - By default, a tag cannot store data between invocations. This is where ``variables`` come in.
22
+ - They are the only way to **store** data. And later **retrieve** it within the same invocation.
23
+
18
24
  **Usage:** ``{=(<name>):<value>}``
19
25
 
20
26
  **Aliases:** ``assign, let, var``
@@ -23,8 +29,20 @@ class AssignmentBlock(verb_required_block(False, parameter=True)): # type: igno
23
29
 
24
30
  **Parameter:** name
25
31
 
26
- **Examples:** ::
32
+ **Examples:**
33
+
34
+ .. code-block:: yaml
35
+
36
+ {=(message1):Hi there! How are you?}
37
+ {=(message2):It's a beautiful day today!}
38
+ {=(message3):Did you know that TagScript is a powerful tool?}
27
39
 
40
+ Now, call the variables by their names:
41
+ {message1} # Hi there! How are you?
42
+ {message2} # It's a beautiful day today!
43
+ {message3} # Did you know that TagScript is a powerful tool?
44
+
45
+ More example:
28
46
  {=(prefix):!}
29
47
  The prefix here is `{prefix}`.
30
48
  # The prefix here is `!`.
@@ -32,6 +50,162 @@ class AssignmentBlock(verb_required_block(False, parameter=True)): # type: igno
32
50
  {assign(day):Monday}
33
51
  {if({day}==Wednesday):It's Wednesday my dudes!|The day is {day}.}
34
52
  # The day is Monday.
53
+
54
+ .. caution::
55
+ - You can name variables with **anything** ``except`` existing block names or aliases.
56
+ - They will ``not`` reference the value in payload, if the name is same as an existing block name or alias.
57
+
58
+ ----
59
+
60
+ .. important:: How Argument Parsing Works - In Detail
61
+
62
+ - A variable is essentially a string that can be treated as a sequence of elements (words, numbers, etc.) when accessed.
63
+ These **elements** are split using ``delimiters`` (spaces by default) and are indexed sequentially starting from ``1``.
64
+ - A delimiter is a sequence of one or more characters that are used to split a string into a sequence of elements.
65
+ - Once a variable is assigned, its value can be referenced and ``parsed`` (split and indexed)
66
+ to extract ``specific`` parts. Let's take a look at **how** it works.
67
+ - Parsing out of bounds index will return the whole string.
68
+
69
+ ----
70
+
71
+ .. rubric:: **Basic Argument Parsing**
72
+
73
+ Example
74
+ - "Coolaid is setting up the table. So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves"
75
+
76
+ - So, our ``argument`` or ``args`` in short, would be:
77
+
78
+ .. code-block:: yaml
79
+
80
+ {=(args):Coolaid is setting up the table. So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves}
81
+
82
+ .. note:: ``args`` is just a variable name, perhaps the most common name, but you can name it anything.
83
+
84
+ - Since, the default delimiter is ``space``, so you can access the ``each element`` as follows:
85
+
86
+ .. code-block:: yaml
87
+
88
+ {args(1)} -> Coolaid
89
+ {args(2)} -> is
90
+ {args(3)} -> setting
91
+ {args(4)} -> up
92
+ {args(5)} -> the
93
+ {args(6)} -> table.
94
+
95
+ {args(31)} -> Would return the whole string since it doesn't exist (indexing 31st element is out of bounds).
96
+
97
+ - ``0`` is special and returns the ``last`` element:
98
+
99
+ .. code-block:: yaml
100
+
101
+ {args(0)} -> gloves
102
+
103
+ - Negative indices allow you to access elements from the end of the sequence:
104
+
105
+ .. code-block:: yaml
106
+
107
+ {args(-1)} -> work
108
+ {args(-2)} -> of
109
+ {args(-3)} -> pair
110
+ {args(-4)} -> a
111
+ {args(-5)} -> and
112
+ {args(-6)} -> level,
113
+
114
+ .. rubric:: Prefix Range Access (``+n``)
115
+
116
+ - Prefixing an index with ``+`` returns all elements from the start up to and including that position:
117
+
118
+ .. code-block:: yaml
119
+
120
+ {args(+3)} -> Coolaid is setting
121
+ {args(+7)} -> Coolaid is setting up the table. So,
122
+ {args(+13)} -> Coolaid is setting up the table. So, he grabbed - a cordless drill,
123
+
124
+ .. rubric:: Suffix Range Access (``n+``)
125
+
126
+ - Suffixing an index with ``+`` returns all elements from that position (counting from the start) to the end:
127
+
128
+ .. code-block:: yaml
129
+
130
+ {args(3+)} -> setting up the table. So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves
131
+ {args(7+)} -> So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves
132
+ {args(13+)} -> drill, some screws, a spirit level, and a pair of work gloves
133
+
134
+ .. rubric:: Negative Range Access (``-n+``)
135
+
136
+ - Appending ``+`` to an negative-index returns a range — all elements from that position to the end:
137
+ - Negative indices are **first** resolved from the end of the sequence,
138
+ then range access continues forward to the end.
139
+
140
+ .. code-block:: yaml
141
+
142
+ {args(-1+)} -> work gloves # Since {args(0)} == gloves
143
+ {args(-11+)} -> drill, some screws, a spirit level, and a pair of work gloves
144
+
145
+ .. tip::
146
+ - ``+n`` → from start → n
147
+ - ``n+`` → from n → end (index resolved first)
148
+ - ``-n`` → nth element from end
149
+ - ``-n+`` → nth element from end → then forward to end (index resolved first)
150
+
151
+ -----
152
+
153
+ .. rubric:: **Advanced Argument Parsing**
154
+
155
+ A **custom delimiter** can be passed as the payload to change how
156
+ the value is split. The syntax is ``{variable(index):delimiter}``:
157
+
158
+ .. code-block:: yaml
159
+
160
+ # Using the same argument as before.
161
+ {=(args):Coolaid is setting up the table. So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves}
162
+
163
+ 1st Example:
164
+ {args(1):.} -> Coolaid is setting up the table
165
+ {args(2):.} -> So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves
166
+ {args(3):.} -> Would return the entire string since there is no 3rd element.
167
+
168
+ 2nd Example:
169
+ {args(1):-} -> Coolaid is setting up the table. So, he grabbed
170
+ {args(2):-} -> a cordless drill, some screws, a spirit level, and a pair of work gloves
171
+
172
+ .. note::
173
+ - In the 1st example, the custom delimiter is ``.``, hence the string is split by ``.`` leaving 2 elements.
174
+ - In the 2nd example, the custom delimiter is ``-``, hence the string is split by ``-`` leaving 2 elements.
175
+ - Since in both the examples, there are ``2 elements``, so ``args(3)`` would return the entire string.
176
+ Because the index ``3rd`` element is out of bounds.
177
+
178
+ .. rubric:: **Nested Variables**
179
+
180
+ - Variables can be **nested** to perform multi-level parsing:
181
+
182
+ .. code-block:: yaml
183
+
184
+ {=(raw):A - B, C, D}
185
+ {=(part):{raw(2):-}}
186
+
187
+ # "{raw(2):-}" splits "raw" by "-" and returns the 2nd element -> "B, C, D" (1st element is "A")
188
+ # Therefore, "part" == "B, C, D"
189
+
190
+ {part(1):,} -> B
191
+ {part(2):,} -> C
192
+
193
+ Another Example:
194
+ # What if you want to parse through the things that Coolaid grabbed?
195
+ # If you look closely the "-" delimiter is placed conveniently to separate the items. So, we'll use it:
196
+
197
+ {=(args):Coolaid is setting up the table. So, he grabbed - a cordless drill, some screws, a spirit level, and a pair of work gloves}
198
+ {=(items):{args(2):-}}
199
+
200
+ # "{args(2):-}" splits "args" by "-" and returns the 2nd element -> "a cordless drill, ... and a pair of work gloves"
201
+ # Therefore, "items" == "a cordless drill, some screws, a spirit level, and a pair of work gloves"
202
+
203
+ Items:
204
+ {items(1):,} -> a cordless drill
205
+ {items(2):,} -> some screws
206
+ {items(3):,} -> a spirit level
207
+ {items(4):,} -> and a pair of work gloves
208
+
35
209
  """
36
210
 
37
211
  ACCEPTED_NAMES: Tuple[str, ...] = ("=", "assign", "let", "var")
@@ -12,7 +12,7 @@ __all__: Tuple[str, ...] = ("UpperBlock", "LowerBlock")
12
12
  class UpperBlock(Block):
13
13
  """Converts the given text to uppercase.
14
14
 
15
- **Usage:** ``{upper([text]))}``
15
+ **Usage:** ``{upper([text])}``
16
16
 
17
17
  **Aliases:** ``uppercase, upper``
18
18
 
@@ -22,10 +22,11 @@ class UpperBlock(Block):
22
22
 
23
23
  **Examples:** ::
24
24
 
25
- The text is {lower(ThIs Is A TeXt)}!
25
+ The text is {upper(ThIs Is A TeXt)}!
26
26
  # The text is THIS IS A TEXT!
27
27
 
28
- You have entered {lower({args})}!
28
+ {=(args):Hello World}
29
+ You have entered {upper({args})}!
29
30
  # You have entered HELLO WORLD!
30
31
  """
31
32
 
@@ -52,6 +53,7 @@ class LowerBlock(Block):
52
53
  The text is {lower(ThIs Is A TeXt)}!
53
54
  # The text is this is a text!
54
55
 
56
+ {=(args):HELLO WORLD}
55
57
  You have entered {lower({args})}!
56
58
  # You have entered hello world!
57
59
  """
@@ -114,7 +114,7 @@ class SequentialGather(Awaitable[T]):
114
114
 
115
115
  Returns
116
116
  -------
117
- `List[T]`
117
+ ``List[T]``
118
118
  the result object.
119
119
  """
120
120
 
@@ -28,6 +28,9 @@ class CooldownBlock(verb_required_block(True, payload=True, parameter=True)): #
28
28
  cooldown is exceeded. If no message is passed, the default message will be sent instead.
29
29
  The cooldown message supports 2 blocks: ``key`` and ``retry_after``.
30
30
 
31
+ .. note::
32
+ - Delimiter for the parameter (``<rate>`` and ``<per>``): ``|`` or ``~``. Where ``|`` takes priority over ``~``.
33
+
31
34
  **Usage:** ``{cooldown(<rate>|<per>):<key>|[message]}``
32
35
 
33
36
  **Payload:** key, message
@@ -11,25 +11,35 @@ __all__: Tuple[str, ...] = ("CountBlock", "LengthBlock")
11
11
 
12
12
  class CountBlock(verb_required_block(True, payload=True)): # type: ignore
13
13
  """
14
- The count block will count how much of text is in message.
15
- This is case sensitive and will include substrings, if you
16
- don't provide a parameter, it will count the spaces in the
17
- message.
14
+ The count block counts occurrences of a substring within a message.
15
+ The search is case sensitive and includes overlapping substrings.
16
+
17
+ A payload (the message to search in) is **required**. Optionally,
18
+ pass the text to search for as a parameter. If no parameter is
19
+ provided, the block counts the number of words in the message
20
+ (spaces + 1).
18
21
 
19
22
  **Usage:** ``{count([text]):<message>}``
20
23
 
21
24
  **Aliases:** ``None``
22
25
 
23
- **Payload:** ``message``
26
+ **Payload:** ``message`` (required)
27
+
28
+ **Parameter:** ``text`` (optional, the substring to count)
24
29
 
25
- **Parameter:** text
30
+ **Examples:** ::
26
31
 
27
- .. tagscript::
28
32
  {count(Tag):TagScriptEngine}
29
33
  # 1
30
34
 
31
- {count(Tag): Tag Script Engine TagScriptEngine}
35
+ {count(Tag):Tag Script Engine TagScriptEngine}
32
36
  # 2
37
+
38
+ {count:hello world}
39
+ # 2 (word count: 1 space + 1)
40
+
41
+ {count(123)}
42
+ # Returns {count(123)} — rejected because no payload was provided
33
43
  """
34
44
 
35
45
  ACCEPTED_NAMES: Tuple[str, ...] = ("count",)
@@ -38,28 +48,28 @@ class CountBlock(verb_required_block(True, payload=True)): # type: ignore
38
48
  if ctx.verb.parameter:
39
49
  payload: str = cast(str, ctx.verb.payload)
40
50
  return str(payload.count(ctx.verb.parameter))
41
- return str(len(cast(str, ctx.verb.payload)) + 1)
51
+ return str(cast(str, ctx.verb.payload).count(" ") + 1)
42
52
 
43
53
 
44
- class LengthBlock(verb_required_block(True, payload=True)): # type: ignore
54
+ class LengthBlock(verb_required_block(True, parameter=True)): # type: ignore
45
55
  """
46
- The length block will check the length of the given String.
47
- If a parameter is passed in, the block will check the length
48
- based on what you passed in, w for word, s for spaces.
49
- If you provide an invalid parameter, the block will return -1.
56
+ The length block returns the character count of the given text.
50
57
 
51
58
  **Usage:** ``{length(<text>)}``
52
59
 
53
60
  **Aliases:** ``len``
54
61
 
55
- **Payload:** None
62
+ **Payload:** ``None``
63
+
64
+ **Parameter:** ``text`` (required)
56
65
 
57
- **Parameter:** ``text``
66
+ **Examples:** ::
58
67
 
59
- .. tagscript::
68
+ {len(TagScriptEngine)}
69
+ # 15
60
70
 
61
- {len("TagScriptEngine")}
62
- 15
71
+ {len(hello world)}
72
+ # 11
63
73
  """
64
74
 
65
75
  ACCEPTED_NAMES: Tuple[str, ...] = ("length", "len")
@@ -0,0 +1,74 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Optional, Tuple, cast
4
+
5
+ from ..interface import verb_required_block
6
+ from ..interpreter import Context
7
+
8
+
9
+ __all__: Tuple[str, ...] = ("CycleBlock",)
10
+
11
+
12
+ class CycleBlock(verb_required_block(True, payload=True, parameter=True)): # type: ignore
13
+ """
14
+ The cycle block returns the element in the payload that corresponds to the
15
+ index value in the parameter. If the index is out of bounds, it loops around
16
+ using modulo (``index % list_length``).
17
+
18
+ List and Cycle blocks are another way to parse through a list of values in
19
+ TagScript. They both strictly use either commas ``,`` or tildes ``~`` as the
20
+ delimiters for the list placed in the block's payload. Use tildes when elements
21
+ contain commas. These blocks only function in Tags.
22
+
23
+ Cycles use ``0`` as the index for the first element. Negative values allow
24
+ for backward parsing. The block will return an error message if the value in
25
+ the parameter is not a number.
26
+
27
+ **Usage:** ``{cycle(<index>):<elem>,<elem2>,...}``
28
+
29
+ **Aliases:** ``None``
30
+
31
+ **Payload:** list of elements (comma or tilde separated)
32
+
33
+ **Parameter:** index
34
+
35
+ **Examples:**
36
+
37
+ .. code-block:: yaml
38
+
39
+ {cycle(1):Cake,Candy,Chips,Cookies,Donut}
40
+ # Candy
41
+
42
+ {cycle(13):Cake,Candy,Chips,Cookies,Donut}
43
+ # Cookies
44
+ # (The list has 5 elements. 13 modulo 5 = 3. Index 3 is "Cookies".)
45
+
46
+ {cycle(3):0,1,2}
47
+ # 0
48
+ # (3 modulo 3 = 0. Index 0 is "0".)
49
+
50
+ Negative indices:
51
+ {cycle(-1):Apple,Banana,Cherry}
52
+ # Cherry
53
+
54
+ {cycle(-69):Charlie,Aid,Bob,Dave,Eve,Phen,Steve,Tom,Wendy,Xavier}
55
+ # Aid
56
+ # (-69 modulo 10 = 1. Index 1 is "Aid".)
57
+
58
+ """
59
+
60
+ ACCEPTED_NAMES: Tuple[str, ...] = ("cycle",)
61
+
62
+ def process(self, ctx: Context) -> Optional[str]:
63
+ try:
64
+ index = int(cast(str, ctx.verb.parameter))
65
+ except (ValueError, TypeError):
66
+ return "Invalid index: parameter must be a number."
67
+
68
+ payload = cast(str, ctx.verb.payload)
69
+ if "~" in payload:
70
+ items = payload.split("~")
71
+ else:
72
+ items = payload.split(",")
73
+
74
+ return items[index % len(items)]