Skip to main content

Python SDK for building Tavern bots

Project description

taverns.py

taverns.py

Python SDK for building Tavern bots on the Taverns platform.

Installation

pip install taverns.py
# or from source:
pip install -e .

Requires Python 3.10+.

Quick Start

import os
from taverns import Client, Intents

client = Client(intents=Intents.MESSAGES | Intents.MESSAGE_CONTENT)

@client.event
async def on_ready():
    print(f"Logged in as {client.user.display_name}")

@client.event
async def on_message_create(message):
    if message.sender_id == client.user.id:
        return
    if message.content == "!ping":
        await client.send_message(message.tavern_id, message.channel_id, content="Pong!")

client.run(os.environ["BOT_TOKEN"])

Slash Commands

from taverns import Client, Intents, Interaction
from taverns.commands import command, option
from taverns.types import CommandOptionType

client = Client(intents=Intents.MESSAGES | Intents.INTERACTIONS)

@client.event
async def on_ready():
    await client.register_commands([
        command("ping", "Check latency"),
        command("greet", "Greet someone", options=[
            option("name", "Who to greet", CommandOptionType.STRING, required=True),
        ]),
    ])

@client.event
async def on_interaction_create(interaction: Interaction):
    if interaction.command_name == "ping":
        await interaction.reply("Pong!")
    elif interaction.command_name == "greet":
        name = interaction.get_option("name", "World")
        await interaction.reply(f"Hello, {name}!")

client.run(os.environ["BOT_TOKEN"])

Deferred Responses

@client.event
async def on_interaction_create(interaction: Interaction):
    if interaction.command_name == "slow":
        await interaction.defer_reply()      # Shows "thinking..." to the user
        result = await do_slow_work()
        await interaction.follow_up(f"Done: {result}")

Ephemeral Responses

await interaction.reply("Only you can see this", ephemeral=True)

Webhook Verification

from taverns import verify_webhook_signature

is_valid = verify_webhook_signature(
    raw_body=request.data,
    signature=request.headers["X-Tavern-Signature"],
    secret=WEBHOOK_SECRET,
    timestamp=request.headers.get("X-Tavern-Timestamp"),
)

Intents

Control which events your bot receives:

from taverns import Intents

# Specific intents
client = Client(intents=Intents.MESSAGES | Intents.INTERACTIONS)

# All non-privileged intents
client = Client(intents=Intents.DEFAULT)

# All intents (including privileged)
client = Client(intents=Intents.ALL)

Privileged intents (MESSAGE_CONTENT, MEMBERS) are self-serve for private bots.

REST Client

For direct API access:

# Available after login
channels = await client.rest.get_channels(tavern_id)
members = await client.rest.get_members(tavern_id)
messages = await client.rest.get_messages(tavern_id, channel_id, limit=50)

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

taverns_py-0.2.2.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

taverns_py-0.2.2-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file taverns_py-0.2.2.tar.gz.

File metadata

  • Download URL: taverns_py-0.2.2.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for taverns_py-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e770f3f9247b05022aa189f2c63ba3b8d54742b84caea0b6781c75012e7f60b8
MD5 b0b306bfde5ad2436d17c8bb03b72cad
BLAKE2b-256 88b2c8a595a19062b34d8f234fd86a0004176158b42d0f62a517a4be76e571f0

See more details on using hashes here.

File details

Details for the file taverns_py-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: taverns_py-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for taverns_py-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 00946b0f09d1bbcbb147692a81e9e014f580cad456b90d415e4e92b87e9af4d2
MD5 10204e14d13e6de9c67fdb7be5e969fa
BLAKE2b-256 b0e2bb45a7c73d50832a6189349759616d6c66cd9fb41ab11e1eb7626b3c0093

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page