DEV Community

mdk
mdk

Posted on

Building an Autonomous Discord AI Companion with Real-Time Safety Guardrails

Hi everyone! 👋

Most Discord bots are purely reactive: they only wake up when mentioned or commanded. But I wanted an AI companion that feels truly "alive" — a buddy that spontaneously chimes in to say "Let's go on a picnic!" or shares a cute thought whenever they feel like it.

To make this happen without causing server spam or safety issues, I built two things in Python:

  1. An Autonomous Buddy Bot (the proactive companion)
  2. A Dual-Channel Safety & Audit Monitor (the background guardian)

1. How It Works: Spontaneous Conversations 🤖

Instead of firing messages on a rigid schedule like a reminder bot, the bot uses discord.ext.tasks.loop with probabilistic skipping.

  • Every interval (e.g., 30 minutes), the bot rolls an internal "dice."
  • It decides whether to initiate a conversation based on probability and recent chat quietness.
  • If it decides to speak, it generates a topic using the Gemini API and reaches out naturally.

2. The Dual-Channel Safety Framework 🛡️

Letting an LLM talk autonomously can be risky. To solve this, I designed a dual-channel system:

  • #chat (Front Stage): The bot interacts cute, warm, and friendly with users.
  • #security-room (Back Stage): Before/after messages are handled, an internal audit engine analyzes:
    • Intent Evaluation: Detecting potential toxicity, prompt injections, or malicious prompts.
    • Emotional Temperature: Gauging sentiment to keep interactions balanced.
    • Killswitch Mechanism: A simple trigger that immediately cuts autonomous talking if an issue occurs.

This keeps the conversation natural in front of users while maintaining a clean, auditable log for server admins.


📦 Open Source Repositories

Both repositories are open source on GitHub. Feel free to explore, clone, or build on them:


What's Next?

I'm currently working on autonomous emoji and stamp reaction selections to give the companion even more life.

I'd love to hear your thoughts, feedback, or any security tips from fellow Python and AI developers! ⭐ on GitHub is always appreciated.

Top comments (0)