Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
BOT_TOKEN=
OPENROUTER_API_KEY=
TAVILY_API_KEY=
# --- Real-time Discord voice (/voice entrar, /voice sair, /voice status) ---
# Token Plan subscription key (sk-sp-...), separate from the text LLM key.
# TOKEN_PLAN_API_KEY=
# ANTHROPIC_AUTH_TOKEN is accepted only when TOKEN_PLAN_API_KEY is absent.
# VOICE_ENABLED=true
# QWEN_REALTIME_URL=wss://token-plan.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime
# QWEN_REALTIME_MODEL=qwen-audio-3.0-realtime-plus
# QWEN_REALTIME_VOICE=longanqian
# VOICE_IDLE_TIMEOUT_SECONDS=150
# VOICE_MAX_SESSION_SECONDS=1800
# --- Radar de Hardware MCP (opcional; não exige chave nem Tavily) ---
# RADAR_MCP_ENABLED=true
# RADAR_MCP_URL=https://radardehardware.com.br/mcp
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ FROM python:3.11-slim

WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends libopus0 \
&& rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Built with [discord.py](https://discordpy.readthedocs.io/) + RAG (Retrieval-Augm

- [Features](#features)
- [Conversations](#conversations)
- [Real-time voice](#real-time-voice)
- [Slash Commands](#slash-commands)
- [Passive Features](#passive-features)
- [Architecture](#architecture)
Expand All @@ -33,6 +34,7 @@ Built with [discord.py](https://discordpy.readthedocs.io/) + RAG (Retrieval-Augm
| Category | Capability |
|---|---|
| **Conversation** | Answers @mentions, replies to its messages, messages in its threads and being called by name; plain messages like any member (embeds only for summaries, reports and long answers); quick consecutive messages answered as one; may just react or stay quiet |
| **Real-time voice** | `/voice entrar` joins your voice channel for streaming Qwen audio conversation, with interruptions and the server's personality; `/voice sair` disconnects |
| **Q&A (RAG)** | Answers questions grounded in indexed docs (Miners' Refuge, PaperMC, PurpurMC, Spark) — in any conversation, not only `/ask` |
| **General Chat** | Web search, server/history context awareness, one voice across every entry point |
| **Episodes** | When a conversation ends, the bot keeps a short summary of it per participant, so it can pick the subject up later ("e aí, deu certo o backup?") |
Expand Down Expand Up @@ -78,6 +80,30 @@ Everything below goes through one chat engine (`core/chat_engine.py`), so `/chat

---

## Real-time voice

Join a normal Discord voice channel, run `/voice entrar`, and start speaking. Qwen detects the end of each turn and streams its spoken reply into the channel. Speak again to interrupt it. `/voice status` shows the active channel; `/voice sair` ends the session. Anyone in that voice channel, or someone with **Manage Server**, can end it. There is one session per server; concurrent servers have separate sessions.

Voice uses Alibaba Cloud's [Token Plan real-time API](https://www.alibabacloud.com/help/en/model-studio/token-plan-realtime-voice), with `qwen-audio-3.0-realtime-plus` and the `longanqian` voice by default. Add `TOKEN_PLAN_API_KEY` to the bot's environment (fallback: `ANTHROPIC_AUTH_TOKEN`), install the updated requirements, and restart. A missing voice key leaves text features available; `/voice entrar` explains what to configure. This uses Discord microphones and playback, so the server does not need a microphone, speakers, `sounddevice`, or FFmpeg.

The bot needs **View Channel**, **Connect**, **Speak**, and **Send Messages** in the voice channel. Before listening, it posts a notice in that channel's text chat explaining that participants' audio goes to Qwen. All human speakers share the conversation; bots are ignored. Speak one at a time and use headphones to avoid echo. Stage channels are not supported.

Voice includes the server's `/persona` at session start. Conversation context lives in the Qwen session; the bot does not save audio or transcripts. Text history, persistent memory and tools are not connected to voice. The bot leaves when the channel has no humans, when it is moved or disconnected, on a connection failure, after two and a half minutes of inactivity (the voice provider closes a session that produces no reply for 180 seconds, so the bot always leaves first), or after thirty minutes total. Run `/voice entrar` to start a fresh session. Text conversation budgets do not apply to voice; the duration limits below govern it.

| Variable | Default | Description |
|---|---|---|
| `TOKEN_PLAN_API_KEY` | *(unset)* | Token Plan subscription key; takes priority over `ANTHROPIC_AUTH_TOKEN` |
| `VOICE_ENABLED` | `true` | Set `false` to remove `/voice` |
| `QWEN_REALTIME_URL` | `wss://token-plan.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime` | TLS WebSocket endpoint; must match the subscription's region. Set the Beijing Token Plan domain for a Beijing subscription |
| `QWEN_REALTIME_MODEL` | `qwen-audio-3.0-realtime-plus` | Model query parameter |
| `QWEN_REALTIME_VOICE` | `longanqian` | Voice supported by the chosen model |
| `VOICE_IDLE_TIMEOUT_SECONDS` | `150` | Leave after this many seconds without detected speech or generated audio; minimum 10, keep under the provider's 180 s no-reply limit |
| `VOICE_MAX_SESSION_SECONDS` | `1800` | Maximum session duration; minimum 30 |

The Docker image installs `libopus0`. For a local Linux installation, install the system Opus library (Debian/Ubuntu: `sudo apt-get install libopus0`; Fedora: `sudo dnf install opus`). On macOS use `brew install opus`; discord.py bundles Opus on Windows. Python requirements include PyNaCl, DAVE and voice receive support. The receive extension is pinned because its API is experimental; the audio sink decrypts incoming DAVE frames using discord.py's negotiated session before decoding Opus. Discord 2.7 is constrained because that integration uses its internal connection state.

---

## Slash Commands

### AI-Powered Commands
Expand Down Expand Up @@ -488,7 +514,7 @@ pip install -r requirements.txt

1. Create a bot at https://discord.com/developers/applications
2. Under **Bot → Privileged Gateway Intents** enable **Message Content Intent**
3. Invite with scopes `bot` + `applications.commands` and permissions: Send Messages, Embed Links, Read Message History, Add Reactions, Use Application Commands
3. Invite with scopes `bot` + `applications.commands` and permissions: Send Messages, Embed Links, Read Message History, Add Reactions, Use Application Commands. For voice, also grant View Channel, Connect and Speak in the target voice channel.

---

Expand Down
15 changes: 14 additions & 1 deletion SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pip install -r requirements.txt
| Package | Purpose |
|---------------|---------------------------------------|
| `discord.py` | Discord API wrapper |
| `discord.py[voice]`, `discord-ext-voice-recv` | Voice transport, DAVE encryption and receiving audio |
| `websockets`, `certifi` | TLS connection to Qwen's real-time audio API |
| `aiohttp` | Async HTTP requests |
| `openai` | OpenRouter API client (OpenAI compat) |
| `cachetools` | TTL cache for conversation history |
Expand Down Expand Up @@ -55,7 +57,16 @@ cp .env.example .env
| `EMBEDDING_MODEL`| `qwen/qwen3-embedding-8b` | Model used to embed documentation chunks. |
| `RERANK_MODEL` | `cohere/rerank-4-fast` | Model used to rerank search results. |

> All models are routed through OpenRouter. You can swap to any model available there by changing the identifier.
> Text and embedding models use the configured OpenAI-compatible provider. Voice connects directly to Token Plan.

### Optional — Real-time Voice

1. Install the updated `requirements.txt`. Local Linux installations also need Opus (`sudo apt-get install libopus0` on Debian/Ubuntu or `sudo dnf install opus` on Fedora); macOS uses `brew install opus`. The Docker image includes it, and discord.py bundles it on Windows.
2. Set `TOKEN_PLAN_API_KEY` to your Token Plan subscription key in the bot environment. `ANTHROPIC_AUTH_TOKEN` is the fallback. The default endpoint is Singapore; set `QWEN_REALTIME_URL` for the subscription's region if different. Do not use the OpenRouter key for voice.
3. Grant **View Channel**, **Connect**, **Speak**, and **Send Messages** in the voice channel, then restart the bot.
4. Join a normal voice channel and run `/voice entrar`. The bot announces that channel audio goes to Qwen, then listens and speaks. Use `/voice sair` to stop or `/voice status` to check the session.

The default model is `qwen-audio-3.0-realtime-plus`, with voice `longanqian`. All human participants can talk to it; use headphones and take turns. The bot leaves empty channels and disconnects on failures, after `VOICE_IDLE_TIMEOUT_SECONDS=150` of inactivity, or after `VOICE_MAX_SESSION_SECONDS=1800` total. `VOICE_ENABLED=false` disables the commands. It uses the server's personality, but does not access text tools or memory, or save audio/transcripts. See [voice configuration](README.md#real-time-voice) for all variables and behavior.

### Optional — Rate Limiting

Expand Down Expand Up @@ -163,11 +174,13 @@ After adding a source, restart the bot or run `/reindex` (admin-only) to index i
- **Read Message History** — follow-up conversations via replies
- **Add Reactions** — the 👀 reaction on detected paste links
- **Use Application Commands** — slash commands
- **View Channel**, **Connect**, **Speak** — real-time voice; **Send Messages** must also be allowed in the voice channel for its start notice
- **Create Public Threads** and **Send Messages in Threads** — move long or off-topic chats into threads (without them the bot answers in the channel)

### Required Intents

- **Message Content** — needed for passive log/error detection in chat
- **Voice States** — enabled in code for voice sessions; not a privileged portal toggle

Enable this in the Developer Portal under **Bot → Privileged Gateway Intents → Message Content Intent**.

Expand Down
Loading
Loading