-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Yousef Z edited this page Apr 19, 2023
·
5 revisions
Qalib (Arabic: قالب), which means template in Arabic, is the official name of this library.
Discord-Qalib is an extension that wraps around and extends discord.py Context instance, allowing for templated responses so that front-end facing text is separated from the source code and placed in a file in a structured manner (i.e. .xml
, .json
).
A simple example .xml
file would be
<discord>
<message key="balance">
<embed>
<title>Hello {player.name}</title>
<colour>cyan</colour>
<fields>
<field>
<title>Balance Remaining</title>
<value>${player.balance}</value>
</field>
</fields>
</embed>
</message>
</discord>
stored as balance.xml
in the templates directory
and can be used in the source code as such
from dataclasses import dataclass
from typing import Literal
from discord.ext import commands
import qalib
from qalib.template_engines import formatter
bot = commands.AutoShardedBot(command_prefix="!", intents=discord.Intents.all())
Messages = Literal["balance"]
@dataclass
class Player:
name: str
balance: float
@bot.command()
@qalib.qalib_context(formatter.Formatter(), "templates/balance.xml")
def balance(ctx: qalib.QalibContext[Messages], name: str):
await ctx.rendered_send("balance", keywords={"player": Player(name, 1000.0)})
🃏 Discord-Qalib 2023