Adapters
These adapters are used to supply data to a TagScript script, most prominently when
using the seed_variables parameter to provide seed variables to the
process() method of a given interpreter
instance. These adapters are also used internally when a script contains a
AssignmentBlock.
Their stored values are accessed through variable blocks in TagScript.
Example: How to use an IntAdapter
1from ya_tagscript import TagScriptInterpreter, adapters, blocks
2
3used_blocks = [
4 blocks.StrictVariableGetterBlock(),
5]
6interpreter = TagScriptInterpreter(used_blocks)
7
8script = "His power level is over {level}!!"
9
10seeds = {
11 "level": adapters.IntAdapter(9000),
12}
13
14response = interpreter.process(script, seed_variables=seeds)
15print(response.body) # His power level is over 9000!!
Example
Using an adapter for a discord.Member in a tiny Discord bot[1]
For this example, assume the invoking user has the ID 123.
Using a MemberAdapter with a discord.py bot
1import discord
2from discord.ext import commands
3
4from ya_tagscript import TagScriptInterpreter, adapters, blocks
5
6intents = discord.Intents.default()
7intents.message_content = True
8
9bot = commands.Bot(command_prefix="$", intents=intents)
10
11used_blocks = [
12 blocks.StrictVariableGetterBlock(),
13]
14interpreter = TagScriptInterpreter(blocks=used_blocks)
15script = "{user(id)}"
16
17
18@bot.command()
19@commands.guild_only()
20async def test(ctx: commands.Context):
21 if isinstance(ctx.author, discord.User):
22 return await ctx.send("Guilds only, friend!")
23
24 seeds = {
25 "user": adapters.MemberAdapter(ctx.author),
26 }
27 response = interpreter.process(script, seed_variables=seeds)
28 print(response.body) # 123
29 return await ctx.send(response.body) # sends message "123" to the invoking channel
30
31
32bot.run("token") # use your own bot's token
Footnotes