auto-thread

Automatically creates Discord threads from new messages in configured channels, using OpenAI to generate descriptive thread titles.

How It Works

Rendering mermaid diagram...

The cron triggers every 1 minute (requires Val Town Pro — free tier minimum is 15 minutes). Inside each invocation, an inner loop polls all watched channels every 5 seconds for ~55 seconds, then exits to leave headroom before the next cron fires. This gives you near-real-time thread creation without needing a persistent server.

When a channel is first added, the bot does not retroactively thread existing messages. It records the latest message as a cursor and only threads new messages going forward.

Files

FileTypePurpose
main.tsintervalCore loop: poll channels → generate title → create thread
sqlite.tsscriptTrack last-processed message ID per channel

Setup

1. Create a Discord Bot

  1. Go to the Discord Developer Portal
  2. Click New Application, give it a name, and click Create
  3. Go to the Bot page in the sidebar
  4. Click Reset Token, confirm, and copy the token
  5. Save the token as the DISCORD_BOT_TOKEN environment variable in this val

2. Invite the Bot to Your Server

  1. In the Developer Portal, go to OAuth2 → URL Generator
  2. Under Scopes, select bot
  3. Under Bot Permissions, select:
    • View Channels
    • Read Message History
    • Send Messages
    • Create Public Threads
    • Send Messages in Threads
  4. Copy the generated URL at the bottom, open it in your browser, and add the bot to your server

3. Configure Channels

Edit the WATCH_CHANNELS array in main.ts with the Discord channel IDs you want to auto-thread.

To get a channel ID: enable Developer Mode in Discord (User Settings → Advanced → Developer Mode), then right-click a channel and click Copy Channel ID.

4. Set the Interval

The interval should be set to 1 minute. Note that the 1-minute interval requires a Val Town Pro subscription (free tier minimum is 15 minutes). If you're on the free tier, set the interval to 15 minutes — the inner loop will still poll every 5 seconds for ~55 seconds each run, but there will be a ~14-minute gap between runs.

Dependencies

  • npm:@discordjs/rest — Discord REST API with rate limiting and retries
  • npm:discord-api-types — Full TypeScript types for the Discord API
  • std/openai — Val Town's OpenAI wrapper
  • std/sqlite — Val Town's project-scoped SQLite