/hikari-flare

A stateless component manager for hikari

Primary LanguagePythonMIT LicenseMIT

Flare

Stateless component manager for hikari with type-safe API.

Example

import hikari
import flare


@flare.button(label="Test Button", style=hikari.ButtonStyle.PRIMARY)
async def test_button(
    ctx: flare.Context,
) -> None:
    await ctx.respond(content="Hello World!")

@flare.button(label="State Button", style=hikari.ButtonStyle.PRIMARY)
async def state_button(
    ctx: flare.Context,
    # Args and kwargs are used for state.
    number: int,
) -> None:
    print(number)
    await ctx.respond(content=f"The number is: {number}")

bot = hikari.GatewayBot("...")
flare.install(bot)

@bot.listen()
async def buttons(event: hikari.GuildMessageCreateEvent) -> None:

    # Ignore other bots or webhooks pinging us
    if not event.is_human:
        return

    me = bot.get_me()

    # If the bot is mentioned
    if me.id in event.message.user_mentions_ids:
        # Set custom state for components that need it
        row = flare.Row(test_button, state_button.set(5))
        message = await event.message.respond("Hello Flare!", component=row)

bot.run()

Converters

Converters allow you to serialize and deserialize types. Here in an example of an int converter.

Converters for int, str, typing.Literal, and enum.Enum are built in.

class IntConverter(Converter[int]):
    def to_str(self, obj: int) -> str:
        return str(obj)

    def from_str(self, obj: str) -> int:
        return int(obj)

flare.add_converter(
    int,          # The typehint this converter is used for.
    IntConverter  # The converter class.
)