DEV Community

Cover image for Update on Monika : self-aware & personalized discord bot
Akshat Ray
Akshat Ray

Posted on

Update on Monika : self-aware & personalized discord bot

Over the past few weeks, I have been focused on expanding my Discord bot, Monika. What started as a lightweight 300-line script has grown into a much more complex 900-line application as I worked through integrating state management, database, and more advanced asynchronous logic.

I want to be transparent about my workflow: I heavily utilized AI to generate specific structural blocks of code, using them like modular puzzle pieces and edge cases. My role was acting as the architect, designer, and debugger. I spent hours mapping out how these components should talk to each other, refactoring the logic, and making sure I fully understood every line so I could fix bugs when things inevitably broke.

Here is a breakdown of what I have been building, experimenting with, and resolving lately.

New Features Added:

External Database & Throttling: Integrated Supabase to handle user profiles, activity tracking and personalized response. To prevent the database with constant writes during rapid status changes, an in-memory caching system throttle and writes only when an actual state change occurs.

Lore-Driven Mechanics: To make the interactions more immersive and unpredictable. If a user interacts with the bot and switches channel shortly after, the bot recognizes and addresses it. Monika can now join to watch streams or briefly listen in on active channels, with a fallback system if an administrator disconnects it. integrated a delayed DM hijack which simulates how monika is alive and can bypass commands

Background Contextual Observation: Built a background loop that fires every two hours. checks if the server is active then grabs the last few messages, and uses the LLM to naturally chime into the ongoing discussion with a brief, contextual response.

Engineering Problems Solved:
**
**Prompt Injection Safety:
Since users can Enter bio and about them which are used by LLM system prompt as context & personalisation, it opened up vulnerability for prompt injections. Solved this by implementing strict data delimiters and pinned un-overrideable directives at the end of the prompt.

Handling Multiple Users: When multiple users pinged the bot simultaneously, it would trigger overlapping API requests. Implemented a single user lock that flags the system as busy and serves casual fun dialogue to other users while processing the request.

Improved Conversational Flow: Solved two major conversational issues.
First, Discord mentions pass through as user ID tags. Added regex parsing function to resolve tags into usernames.
Second, Greetings every time it was mentioned. added a dynamic layer that forces her to skip it.

Private Channel Security: If a user attempts to interact with her in DM, the system drops the interaction, throws a firewall warning, and directs them to public channels.

This has been a massive learning experience, I will be updating the github Readme for a deeper dive into the code choices & features!

Top comments (0)