Documentación / Developers / Cookbook / 18) Keyword moderation: auto-timeout + embed mod-log
BuildSDK 0.10.1

18) Keyword moderation: auto-timeout + embed mod-log

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

Time out members who use a banned word and post an embed to a mod-log channel. Reads message text (events:message_content) and uses discord:moderate_members (Dangerous tier — expect reviewers to look at it closely). Note plugins can only delete their own messages, so a moderation plugin's tools are timeouts and logging, not message removal.

from yourbot_sdk import SdkPermissionError

@plugin.on_slash_command("banword")
def banword(ctx: Context, event: dict):
    opts = {o["name"]: o["value"] for o in event.get("options", [])}
    words = ctx.kv.get("banned_words") or []
    words.append(str(opts["word"]).lower())
    ctx.kv.set("banned_words", words)
    ctx.interaction.respond(content=f"Added. {len(words)} words on the list.")

@plugin.on_event("message_create")
def watch(ctx: Context, event: dict):
    if event.get("author_bot"):
        return
    content = (event.get("content") or "").lower()
    hit = next((w for w in (ctx.kv.get("banned_words") or []) if w in content), None)
    if not hit:
        return
    try:
        ctx.discord.timeout_member(
            user_id=event["author_id"],
            duration_seconds=600,
            reason=f"Banned word: {hit}",
        )
    except SdkPermissionError as exc:
        ctx.log(f"cannot timeout: {exc}", level="warning")
        return
    log_channel = ctx.kv.get("mod_log_channel_id")
    if log_channel:
        ctx.discord.send_message(channel_id=str(log_channel), embeds=[{
            "title": "Member timed out (10 min)",
            "description": f"<@{event['author_id']}> used a banned word.",
            "color": 0xE05A5A,
            "fields": [
                {"name": "Word", "value": hit, "inline": True},
                {"name": "Channel", "value": f"<#{event['channel_id']}>", "inline": True},
            ],
        }])

Embeds are plain dicts in Discord's embed shape (title, description, color, fields, footer, …) — up to 10 per message.

YourBot docs Reference tables are generated from the code that is running. Ask in Discord Suggest a correction