Skip to content

starspacegroup/spacebot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

584 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

SpaceBot logo

SpaceBot ๐Ÿš€

A powerful, self-hosted Discord bot platform built with SvelteKit 2 (Svelte 5) and deployed on Cloudflare Pages. Create custom slash commands, build event-driven automations, and monitor all Discord activity through a beautiful admin dashboard.

SpaceBot Dashboard

โœจ Features

๐Ÿค– Discord Bot

  • Custom Slash Commands โ€” Create your own commands with parameters, choices, and custom responses
  • Event-Driven Automations โ€” Trigger actions automatically when Discord events occur
  • Full Gateway Support โ€” Real-time event capture via Discord.js gateway connection
  • Interactions Endpoint โ€” HTTP-based slash command handling via Cloudflare Workers
  • Context Menu Commands โ€” Message context menu registration and routing with server-side permission enforcement
  • AI Chat for Managers โ€” Server managers can DM the bot for AI-powered assistance. In production this runs on Cloudflare Workers AI (optionally through an AI Gateway for analytics/caching); in local development you can point it at an Ollama model instead (AI_PROVIDER=ollama, default gemma3:4b).
  • AI DM Autopilot โ€” When enabled, gateway DMs are enqueued onto a Cloudflare Queue and handled by a standalone orchestrator worker (see docs/ai-autopilot.md).

๐ŸŒ Web Dashboard

  • Server Selection โ€” Manage all servers where you're an admin
  • Event Logs โ€” View detailed logs of all Discord activity (members, messages, voice, moderation, etc.)
  • Automation Builder โ€” Visual interface to create event โ†’ action automations
  • Command Builder โ€” Design custom slash commands with the automation action system
  • Dark/Light Theme โ€” Beautiful UI with theme toggle support
  • Operational UX โ€” Global toasts, skeleton loading states, route-level error handling, and reduced-motion-aware transitions

๐Ÿงฉ Automation & Extensibility

  • Local Runner โ€” A standalone Bun CLI users install on their own machine. It connects to the server over WebSocket, runs jobs as shell commands inside a path allowlist, and streams results back (see docs/local-runner-v2.md).
  • Superadmin Workflows โ€” Scheduled, server-spanning workflow templates driven by cron_expression values stored in D1 (see docs/superadmin-workflows.md).
  • MCP Server โ€” A standalone Model Context Protocol server (mcp-server/) that exposes SpaceBot data/operations to MCP-aware clients.

๐Ÿ” Authentication & Security

  • Discord OAuth2 โ€” Secure login with Discord credentials
  • Sliding Session TTL โ€” Configurable cookie lifetime via SESSION_TTL_SECONDS
  • Admin Access Control โ€” Only server admins can manage their servers
  • Request Signature Verification โ€” All Discord interactions are cryptographically verified
  • Browser Hardening โ€” Same-origin checks for cookie-authenticated JSON mutations plus HTML security headers and report-only CSP

โšก Cloudflare-Native

  • Cloudflare Pages โ€” Deployed on Cloudflare's global edge network
  • D1 Database โ€” SQLite-based serverless database for logs and configurations
  • Workers AI โ€” AI-powered DM responses via Cloudflare's free LLM models
  • AI Gateway โ€” Optional analytics, caching, and rate limiting for AI requests
  • Zero Cold Starts โ€” Fast response times worldwide

Roadmap Operations

Repository-ready external integrations are configured with environment variables, not hardcoded credentials:

  • PUBLIC_SUPPORT_DISCORD_URL, PUBLIC_BOT_VOTE_URL, and PUBLIC_BOT_REVIEW_URL power public community CTAs.
  • SENTRY_DSN, SENTRY_ENVIRONMENT, and SENTRY_TRACES_SAMPLE_RATE enable Sentry.
  • RATE_LIMIT_ENABLED, RATE_LIMIT_DEFAULT_WINDOW_SECONDS, and RATE_LIMIT_DEFAULT_MAX_REQUESTS enable D1-backed API limits.

Useful checks:

bun run docs:api
bun run audit:deps
bun run images:check
bun run verify:http3

๐Ÿ“ธ Screenshots

๏ฟฝ Discord Login

Discord Login

Secure Discord OAuth2 authentication for accessing the admin dashboard.

๐Ÿ“Š Server Admin (Dark Mode)

Server Admin Dark

View event logs, statistics, and quick access to automations and commands.

๐Ÿ“Š Server Admin (Light Mode)

Server Admin Light

The dashboard also supports a clean light theme.

โšก Automations

Automations List

Create event-driven automations that trigger on Discord events like member joins, message creates, voice state changes, and more.

๐Ÿ› ๏ธ Automation Editor

Automation Editor

Visual interface for configuring triggers, conditions, and actions for your automations.

๐ŸŽฎ Custom Commands

Commands

Build custom slash commands with parameters and tie them to automation actions.

โœ๏ธ Command Editor

Command Editor

Design slash commands with options, parameters, and custom responses through an intuitive editor.

๐Ÿ“ Event Logs

Event Logs

Comprehensive logging of all Discord events with filtering and search.

๐Ÿ› ๏ธ Tech Stack

Layer Technology
Framework SvelteKit 2 (Svelte 5)
Edge runtime Cloudflare Pages/Workers
Package manager Bun
Database Cloudflare D1 (SQLite)
Bot Library Discord.js 14
AI Cloudflare Workers AI / AI Gateway (prod); Ollama (dev only)
Styling Custom CSS with CSS Variables
Auth Discord OAuth2

๐Ÿš€ Getting Started

Prerequisites

  • Bun 1.3+ (the project's package manager and runtime โ€” used for every script; Node.js is not required)
  • A Discord Application (create one here)
  • A Cloudflare account (sign up here)
  • (Optional) cloudflared for local tunneling

Installation

  1. Clone the repository

    git clone https://github.com/starspacegroup/spacebot.git
    cd spacebot
  2. Install dependencies

    bun install
  3. Configure environment variables

    Copy .env.example to .env and fill in your Discord credentials:

    cp .env.example .env

    Required variables:

    Variable Description
    DISCORD_PUBLIC_KEY Found in your app's "General Information"
    DISCORD_CLIENT_ID Your application's Client ID
    DISCORD_CLIENT_SECRET Found under OAuth2 settings
    DISCORD_BOT_TOKEN Found under "Bot" settings
    ADMIN_USER_IDS Comma-separated Discord user IDs with global admin access
    LOG_LEVEL Logging verbosity: error, warn, info, debug

    Optional AI variables (for DM chat functionality):

    Variable Description
    CLOUDFLARE_ACCOUNT_ID Your Cloudflare account ID
    CLOUDFLARE_AI_TOKEN API token with Workers AI permissions
    CLOUDFLARE_AI_GATEWAY_ID (Optional) AI Gateway ID for analytics/caching
    CLOUDFLARE_AI_MODEL (Optional) Override default LLM model
  4. Set up the database (local development)

    bun run db:migrate:local
  5. Run the development server

    bun run dev

    The app will be available at http://localhost:4269 (the port is pinned in vite.config.js).

  6. Start the Gateway bot (in a separate terminal)

    bun run dev:gateway

    This captures Discord events and processes automations.

Discord Bot Setup

  1. Go to Discord Developer Portal
  2. Select your application โ†’ Bot
  3. Enable Privileged Gateway Intents:
    • โœ… Presence Intent
    • โœ… Server Members Intent
    • โœ… Message Content Intent
  4. Go to OAuth2 โ†’ URL Generator
    • Scopes: bot, applications.commands
    • Permissions: Administrator (or customize as needed)
  5. Use the generated URL to invite the bot to your server

Setting up Interactions Endpoint

For production, configure Discord to send interactions to your Cloudflare Pages URL:

  1. Deploy to Cloudflare Pages (see Deployment)
  2. Go to Discord Developer Portal โ†’ Your Application โ†’ General Information
  3. Set Interactions Endpoint URL to:
    https://your-domain.pages.dev/api/discord/interactions
    

๐Ÿ“ฆ Available Bun Scripts

Script Description
bun run dev Start SvelteKit dev server
bun run dev:wrangler Run with Wrangler (Cloudflare local environment)
bun run dev:gateway Start Discord gateway bot
bun run dev:tunnel Start cloudflared tunnel for local development
bun run build Build for production
bun run db:migrate Run database migrations (production)
bun run db:migrate:local Run database migrations (local)
bun run register-commands Register slash commands with Discord
bun run test Run the Vitest test suite
bun run test:e2e Run the Playwright smoke suite
bun run typecheck Type-check with svelte-check
bun run lint Run ESLint
bun run runner Start the local runner CLI

๐Ÿ”„ Automations

SpaceBot's automation engine lets you create powerful event-driven workflows:

Supported Triggers

  • Member Events โ€” Join, leave, ban, unban, kick, timeout
  • Message Events โ€” Create, edit, delete, bulk delete
  • Voice Events โ€” Join, leave, mute, deafen, stream, video
  • Role Events โ€” Create, delete, update, member role add/remove
  • Channel Events โ€” Create, delete, update
  • Reaction Events โ€” Add, remove
  • Interaction Events โ€” Slash commands, button clicks, modals
  • And many more...

Available Actions

  • ๐Ÿ“จ Send Message โ€” Send a message to a channel
  • ๐Ÿ—‘๏ธ Delete Messages โ€” Delete messages from a user
  • ๐Ÿ—‘๏ธ Delete Messages Mentioning User โ€” Delete messages that mention a specific user
  • ๐Ÿท๏ธ Add/Remove Role โ€” Modify member roles
  • ๐Ÿ‘ข Kick Member โ€” Kick a member from the server
  • ๐Ÿ”จ Ban Member โ€” Ban a member
  • โฐ Timeout Member โ€” Timeout a member
  • ๐Ÿ“ข Send DM โ€” Send a direct message to a user

Template Variables

Use dynamic variables in your messages:

Welcome {user.mention} to {guild.name}!

Available: {user.id}, {user.name}, {user.mention}, {channel.name}, {guild.name}, {option.<name>}, and more.

๐ŸŽฎ Custom Commands

Create custom slash commands through the web dashboard:

  1. Navigate to Admin โ†’ Your Server โ†’ Commands
  2. Click New Command
  3. Configure:
    • Command name and description
    • Parameters (text, numbers, users, channels, roles, etc.)
    • Response message or embed
    • Optional: Tie to an automation action
  4. Click Register with Discord to sync

๐Ÿ“‹ Event Logging

SpaceBot captures and logs all Discord events:

Category Events
๐Ÿ‘ค Member Join, leave, update, nickname changes
๐Ÿ’ฌ Message Create, edit, delete, bulk delete
๐ŸŽค Voice Join, leave, mute, deafen, stream, video
๐Ÿ“ Channel Create, delete, update
๐Ÿท๏ธ Role Create, delete, update, member assignments
๐Ÿ”จ Moderation Ban, unban, kick, timeout
โšก Interaction Commands, buttons, modals, select menus
๐Ÿ“… Events Scheduled event create, update, delete

๐ŸŒ Deployment

Deploy to Cloudflare Pages (Recommended)

  1. Push to GitHub

    git push origin main
  2. Connect to Cloudflare Pages

    • Go to Cloudflare Dashboard
    • Workers & Pages โ†’ Create application โ†’ Pages โ†’ Connect to Git
    • Select your repository
    • Configure:
      • Build command: bun run build
      • Build output: .svelte-kit/cloudflare
  3. Add Environment Variables In Cloudflare Pages Settings โ†’ Environment Variables, add all required variables.

  4. Create D1 Database

    wrangler d1 create spacebot-logs

    Update wrangler.toml with the database ID.

  5. Run Migrations

    bun run db:migrate
  6. Deploy Future pushes to main will auto-deploy.

    AI queue orchestration also auto-deploys via GitHub Actions in .github/workflows/deploy-orchestrator-worker.yml. Configure these repository secrets so worker deploys can run:

    • CLOUDFLARE_API_TOKEN
    • CLOUDFLARE_ACCOUNT_ID
    • ORCHESTRATOR_SPACEBOT_API_BASE
    • AI_AUTOPILOT_INTERNAL_KEY

See DEPLOYMENT.md for detailed instructions.

๐Ÿ“ Project Structure

spacebot/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lib/
โ”‚   โ”‚   โ”œโ”€โ”€ ai/               # AI chat, MCP client, retry policy
โ”‚   โ”‚   โ”œโ”€โ”€ automation/       # Event โ†’ action automation engine
โ”‚   โ”‚   โ”œโ”€โ”€ components/       # Svelte components
โ”‚   โ”‚   โ”œโ”€โ”€ db/               # D1/SQLite access, one module per domain
โ”‚   โ”‚   โ”œโ”€โ”€ discord/          # Gateway client, REST, bot registry, commands
โ”‚   โ”‚   โ”œโ”€โ”€ integrations/     # External integrations (e.g. GitHub) + auth
โ”‚   โ”‚   โ”œโ”€โ”€ server/           # Server-only helpers (cron, workflow runtime)
โ”‚   โ”‚   โ””โ”€โ”€ utils/            # Shared utilities
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”‚   โ”œโ”€โ”€ admin/            # Per-server dashboard (under [serverId]/)
โ”‚   โ”‚   โ”œโ”€โ”€ account/          # User-level AI jobs / workflows / operations
โ”‚   โ”‚   โ””โ”€โ”€ api/              # REST endpoints (+ api/v1, api/runner/ws)
โ”‚   โ”œโ”€โ”€ tests/                # Vitest test suites
โ”‚   โ””โ”€โ”€ hooks.server.ts       # Edge auth + request handling
โ”œโ”€โ”€ orchestrator-worker/      # Cloudflare Queue consumer (AI autopilot, cron)
โ”œโ”€โ”€ mcp-server/               # Standalone MCP server
โ”œโ”€โ”€ scripts/                  # CLIs and tooling (incl. scripts/local-runner/)
โ”œโ”€โ”€ migrations/               # D1 database migrations (immutable once applied)
โ”œโ”€โ”€ _functions/               # Cloudflare Pages Functions
โ”œโ”€โ”€ static/                   # Static assets
โ””โ”€โ”€ docs/                     # Documentation (incl. architecture.md, screenshots/)

See docs/architecture.md for a diagram of how the runtimes fit together, and CLAUDE.md for the authoritative architecture notes.

๐Ÿ“š Documentation

Full documentation is published to GitHub Pages: https://starspacegroup.github.io/spacebot/ (built from docs/).

Highlights: Architecture ยท API ยท Integrations ยท AI Autopilot ยท Local Runner v2 ยท Superadmin Workflows ยท Observability ยท Tutorials.

๐Ÿ”’ Security

  • โœ… Discord request signature verification
  • โœ… Same-origin checks for cookie-authenticated JSON mutations
  • โœ… Security headers and report-only CSP for HTML responses
  • โœ… HTTP-only secure cookies
  • โœ… Configurable sliding session TTL
  • โœ… Admin permission checks
  • โœ… Environment variable secrets
  • โœ… HTTPS via Cloudflare

๐Ÿ—บ๏ธ Roadmap

See ROADMAP.md for planned features and enhancements.

๐Ÿค Contributing

See CONTRIBUTING.md for local setup, the project's hard rules (immutable migrations, Bun-only, the multi-runtime model), and how to run tests before opening a PR.

๐Ÿ“„ License

MIT

๐Ÿ”— Resources

About

An open-source Discord bot hosted on Cloudflare.

Resources

Contributing

Stars

Watchers

Forks

Contributors