Documentación / Developers / Cookbook / 5) AFK status
BuildSDK 0.10.1

5) AFK status

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

/afk <reason> sets the user's status; the bot replies when they're mentioned. Parsing mentions reads message text, so this recipe needs events:message_content.

import re

from yourbot_sdk.events import MessageCreate

@plugin.on_slash_command("afk")
def set_afk(ctx: Context, event: dict):
    opts = {o["name"]: o["value"] for o in event.get("options", [])}
    reason = opts.get("reason") or "afk"
    ctx.kv.set(f"afk:{event['user_id']}", reason, ttl_seconds=86400)
    ctx.interaction.respond(content=f"Got it, set you AFK ({reason}).")

@plugin.on_event("message_create")
def notify_mentions(ctx: Context, event: MessageCreate):
    if event.get("author_bot"):
        return
    for uid in set(re.findall(r"<@!?(\d+)>", event.get("content", ""))):
        reason = ctx.kv.get(f"afk:{uid}")
        if reason:
            ctx.discord.send_message(channel_id=event["channel_id"],
                content=f"<@{uid}> is AFK: {reason}")
YourBot docs Reference tables are generated from the code that is running. Ask in Discord Suggest a correction