Project Story

TL;DR

TTrivia is a scalable Twitch trivia bot, written in Rust and designed to host real-time quiz games in multiple Twitch channels.

What I Learned

  • Learned and applied the Rust actor model, which provides an efficient structure for a bot that must handle high-concurrency tasks, such as listening and writing to Twitch chat interactions and processing RabbitMQ messages.
  • Using RabbitMQ to distribute and dynamically scale bot instances, enabling each instance to handle multiple Twitch channels efficiently.
  • Leveraging Svelte 5 with runes for frontend reactivity, along with DaisyUI for styling and TypeScript for type safety.
  • PostgreSQL manages data storage for quiz sessions and user scores, allowing for reliable session tracking and user ranking.
  • Deploying in Kubernetes with Helm charts and ArgoCD for GitOps-based management, and monitoring application performance with Grafana.

How It’s Built

  • Bot and Bot Manager: Implemented in Rust with the actor model, allowing the bot_manager to manage commands such as !join and !leave by routing the commands to bot instances via RabbitMQ’s fanout exchange. Each bot instance can handle multiple channels, adapting dynamically to load.
  • Backend: Built in Rust with Axum, providing APIs for the frontend and managing data in PostgreSQL to track user scores and quiz data. The backend can also remotely trigger bot instances to join or leave Twitch channels when users initiate commands through the optional frontend.
  • Frontend: Developed in Svelte 5 with runes for advanced reactivity, styled with DaisyUI, and utilizing TypeScript. Through the frontend, streamers can manage questions, adjust hint durations, check the bot’s moderation status, and control bot presence in their channel. A default question catalog is also available.
  • Infrastructure: Hosted in Kubernetes, with deployment orchestrated by Helm charts and ArgoCD for GitOps-based configuration. Performance is monitored via Grafana, and although not yet automatic, scaling could be implemented if needed.

How It Works

  1. User Interaction: Users join the TTriviaBot channel on Twitch and use !join to invite the bot to their channel. For command access, users grant moderator status with /mod TTriviaBot in their Twitch chat, and they can check this status with !mod.
  • Alternatively, streamers can log in to the TTrivia website to manage bot interactions. From the site, they can invite the bot to join or leave their channel, confirm mod status, and manage question catalogs and categories.
  1. Channel Management:
  • The bot_manager monitors the TTriviaBot chat for !join and !leave commands, which are forwarded via RabbitMQ to the appropriate bot instance. Each bot instance dynamically manages its assigned channels based on incoming requests.
  1. Gameplay:
  • Once the bot is in a channel, users can start a trivia round with !TTrivia start, initiating a 10-question session. They can end the round with !TTrivia stop.
  • Each question offers up to three hints, after which the bot reveals the answer if unanswered.
  • Points are awarded based on how quickly a correct answer is given, with a maximum of 100 points available per question. As time elapses, the potential points decrease, rewarding players who answer quickly.
  • Players can track scores with !TTrivia rank or view the top five players with !TTrivia top. After each question, the bot announces the winner, and at the end of the round, a session summary is shared.

TODO:

  • Highscore List on Website: Display an interactive highscore list that updates based on the trivia results, accessible to users via the main site.
  • Twitch Highscore Panel: Integrate a highscore panel for Twitch, allowing viewers to track top players within the Twitch interface.
  • Ongoing Testing: Conduct further testing to catch edge cases and ensure stability, especially under high user loads.

Refactor:

  • OAuth Authentication: Transition Twitch OAuth management from a custom implementation to Supabase's built-in authentication. This switch could streamline authorization flows and enhance security, reducing manual configuration.
  • Simplify Message Distribution: Explore replacing RabbitMQ with PostgreSQL's LISTEN and NOTIFY capabilities, potentially using Supabase Realtime to handle message distribution. This change may reduce infrastructure complexity, enabling efficient communication directly through the database and eliminating the need for an additional message broker.

NOTE

The TTriviaBot demo is active in the TTriviaBot channel! Users can try it out using the commands !TTrivia start, !TTrivia stop, !TTrivia rank, and !TTrivia top. Please note that this deployed version is an early release and may stop functioning if the RabbitMQ communication loses connection.

Built With

Share this project:

Updates