Docs / Developers / Cookbook / 20) Weekly digest on a real schedule (production cron)
BuildSDK 0.10.1

20) Weekly digest on a real schedule (production cron)

These docs are public. To publish a plugin you need a YourBot account: sign in to open the Dev Portal.

The daily-gate pattern (recipe 3) needs traffic to fire. When a job must run at a wall-clock time even on a quiet server, use a cron task: decorate the handler and declare it in your manifest's "cron" array — the platform fires it per installed server in UTC (5-minute floor, 5 entries max; see Cron schedules).

@plugin.on_slash_command("digest-here")
def set_digest(ctx: Context, event: dict):
    ctx.kv.set("digest_channel_id", event["channel_id"])
    ctx.interaction.respond(content="Weekly digest will post here, Fridays 16:00 UTC.")

@plugin.on_event("message_create")
def count(ctx: Context, event: dict):
    if not event.get("author_bot"):
        ctx.metrics.record("messages")

@plugin.cron("0 16 * * 5")        # every Friday, 16:00 UTC
def weekly_digest(ctx: Context):
    channel_id = ctx.kv.get("digest_channel_id")
    if not channel_id:
        return
    total = int(ctx.metrics.total("messages", period="7d"))
    ctx.discord.send_message(channel_id=str(channel_id),
        content=f"📬 This week on the server: {total} messages. See you next Friday!")

And in manifest.json (the platform reads schedules from the manifest, not your code):

"cron": [
  {"spec": "0 16 * * 5", "name": "weekly_digest"}
]

Tip: capabilities and command names are auto-detected from these snippets, so a fresh yourbot new my_plugin plus one recipe uploads cleanly. Commands that take arguments still need their slash_commands options declared — copy them from Manifest declarations. Run yourbot validate before pushing to catch any gaps.

Was this page helpful?
YourBot docs Reference tables are generated from the code that is running. Ask in Discord