Skip to content

ak811/threads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Threads (Discord Bot)

A Discord bot that automatically creates a thread for messages posted in selected text channels, so conversations don’t turn into one endless scroll of chaos.

It supports:

  • Per-message threads (every qualifying message becomes a new thread), or
  • First message per user per day (one thread per user per channel per UTC day)

Configuration is stored in a local SQLite database (via aiosqlite) and managed through Discord commands.


Features

Auto-threading

When enabled for a channel, the bot can:

  • Create a thread from a qualifying message
  • Generate a thread title from a template (supports tokens)
  • Enforce a channel-level cooldown (rate limiting)
  • Restrict who it applies to:
    • everyone
    • non_staff (skips admins / managers / manage messages)
    • roles (only specific role IDs allowed)
  • Ignore:
    • bot messages
    • command messages (based on your prefix)
    • replies (only top-level messages)

Cleanup behavior

  • If the root message is deleted, the bot attempts to delete the associated thread, and removes the mapping from the DB.
  • If a thread is deleted manually, the bot cleans up the mapping row.

Command Overview

Prefix is controlled by COMMAND_PREFIX (default: !).

User-facing

  • !threads or !th
    Shows help and template token tips.

  • !threads list
    Shows configured channels for the server.

  • !threads info #channel
    Shows the effective config for a channel.

Admin-only (Administrator or Manage Server)

  • !threads enable [#channel]
    Enables auto-threading for the channel (defaults to the channel you run the command in).

  • !threads disable [#channel]
    Disables auto-threading for the channel.

Settings

  • !threads set mode #channel <per_message|first_message_per_user_per_day>

  • !threads set archive #channel <60|1440|4320|10080>
    (minutes: 1 hour, 1 day, 3 days, 7 days)

  • !threads set template #channel <template...>
    Max ~200 characters.

  • !threads set scope #channel <everyone|non_staff|roles>

  • !threads roles add #channel @Role

  • !threads roles remove #channel @Role

  • !threads set cooldown #channel <seconds>

  • !threads set min_snippet #channel <int>

  • !threads set only_top_level #channel <on|off>

  • !threads set ignore_bots #channel <on|off>

  • !threads set ignore_commands #channel <on|off>


Template Tokens

Thread titles are built from a template string.

Supported tokens:

  • {author}: display name of the message author
  • {snippet}: cleaned snippet from message content
  • {channel}: channel name
  • {message_id}: root message ID

Default template:

💬 {author} — {snippet}

Notes:

  • Markdown, inline/multiline code blocks, and links are stripped from {snippet}.
  • Title length is capped at 60 characters.

Requirements

  • Python 3.10+ recommended
  • discord.py>=2.4.0
  • aiosqlite>=0.19.0
  • python-dotenv>=1.0.1
  • uvloop is optional (used automatically when available; ignored on Windows)

See requirements.txt.


Setup

1) Create a bot in the Discord Developer Portal

  1. Create an application + bot
  2. Copy the bot token
  3. Invite the bot to your server using the OAuth2 URL Generator

2) Enable required intents

This bot uses:

  • Server Members Intent (intents.members = True)
  • Message Content Intent (intents.message_content = True)

These may need to be enabled in the Developer Portal depending on your bot’s size/verification status.

3) Configure environment variables

Copy the sample env file:

cp .env.example .env

Edit .env:

DISCORD_TOKEN=your_token_here
COMMAND_PREFIX=!
DB_PATH=cogs/data/threads.db

DB_PATH is where the SQLite DB will live. The bot will create the folder if it doesn’t exist.


Install & Run

Windows (PowerShell)

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
python bot.py

macOS / Linux

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python bot.py

You should see:

Threads is online as <botname> (prefix: !)

Permissions

For smooth operation, the bot typically needs (per enabled channel):

  • View Channel
  • Read Message History
  • Send Messages
  • Add Reactions (optional, used for 🧵 reaction)
  • Create Public Threads (or Create Private Threads if you adapt it)
  • Manage Threads (needed if you want it to reliably delete threads)

If the bot lacks permissions, it will silently fail to create/delete threads (because Discord forbids it, not because the bot is “shy”).


How Auto-Threading Works

On every message in a guild text channel:

  1. Load channel config from the DB
  2. If not enabled, exit
  3. Apply filters (ignore bots, ignore commands, only top-level, scope rules)
  4. Enforce mode:
    • per_message: always eligible
    • first_message_per_user_per_day: only the user’s first message that UTC day
  5. Enforce cooldown (channel-level throttle)
  6. Create thread:
    • Name from template
    • Archive duration from config
  7. Persist mapping root_message_id -> thread_id for cleanup later

Database

SQLite with migrations under:

cogs/db/migrations/

Tables (high level):

  • guilds: guild metadata
  • channels: per-channel configuration
  • channel_roles: role whitelist when scope is roles
  • throttles: last thread creation timestamps per channel
  • first_message_log: used for the per-user-per-day mode
  • audit_log: records config actions
  • thread_roots: mapping root message → thread for cleanup

Migrations are run automatically on startup (PRAGMA user_version is used).


Example Configuration

Enable auto-threading in a channel:

!threads enable #general

Thread title template:

!threads set template #general 🧵 {author}: {snippet}

Restrict to non-staff:

!threads set scope #general non_staff

Enable role-only mode:

!threads set scope #general roles
!threads roles add #general @Members

First thread per user per day:

!threads set mode #general first_message_per_user_per_day

Add a 30-second cooldown:

!threads set cooldown #general 30

License

MIT (See LICENSE).

About

Threads (Discord Bot)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors