~ $ cat work/discord-ai-bridge.md
Discord AI Bridge
A Discord bot that answers questions with a model running on my own machine.
AI tools · March – April 2026Built on my own.
- OUTCOME
- Fixed a bug that silenced the bot for everyone while the AI was thinking
- SCOPE
- Solo project
- DECISION
- Move the blocking request off the event loop (the part of a program that handles one task at a time) before considering a full async-client rewrite.
- EVIDENCE
- Source excerpt and an engineering note that explains the failure mode and trade-off.
Built with
- Python
- discord.py
- asyncio
- LM Studio
- REST API
What it is
Discord AI Bridge is a Discord bot that answers questions using an AI that runs on my own computer instead of in the cloud. Type !.ask followed by a question in a chat channel, and the bot replies.
I built it to learn what it takes to put an AI behind a real chat app, and to keep the bot from freezing while the AI thinks.
What it can do
One command
Type !.ask and your question. The bot does the rest.
An AI on my own machine
The answers come from a model called Qwen 3.5 9B, running in a program called LM Studio on my computer. No outside AI service is involved.
Stays responsive
While the AI is thinking, the bot can still answer other people instead of freezing.
Clear error messages
If the AI program is not running, or something else goes wrong, the bot says what happened in the chat.
No accidental pings
Even if an answer contains @everyone, the bot will not notify the whole server.
Secrets stay secret
The bot’s login token lives in a private settings file, not in the code.
How a request flows
- Discord!.ask command
- discord.pyEvent loop stays free
- Worker threadasyncio.to_thread()
- LM StudioQwen 3.5 9B, local
How it works
- 01
Someone types !.ask and a question in the chat.
- 02
The bot passes the question to LM Studio, the program that runs the AI on my computer.
- 03
While it waits, the bot keeps doing other work on a separate track, so nobody is left hanging.
- 04
The answer comes back and the bot posts it in the channel.
- 05
If anything fails along the way, the bot posts a short explanation instead.
Illustration
Why
I wanted to see what it takes to put a locally hosted language model behind a real chat interface, with no cloud API in between.
What I built
- 01
Connected discord.py to LM Studio’s OpenAI-compatible endpoint and a local Qwen 3.5 9B model, triggered by the !.ask command.
- 02
Moved the blocking HTTP call into a worker thread with asyncio.to_thread(), so the bot keeps handling other messages while a reply is generated.
- 03
Turned connection failures, HTTP errors, and unexpected responses into plain messages in the channel instead of silent failures.
The code
try:
# Run the blocking request in a thread so the bot stays responsive
response = await asyncio.to_thread(requests.post, API_URL, json=payload)
response.raise_for_status()
ai_response = response.json()["choices"][0]["message"]["content"]
await message.channel.send(
ai_response, allowed_mentions=discord.AllowedMentions(everyone=False)
)
except requests.exceptions.ConnectionError:
await message.channel.send("⚠️ Error: LM Studio server is not running.")
except requests.exceptions.HTTPError as e:
await message.channel.send(f"⚠️ Server Error: {e.response.status_code}")Outcome
People in the server can ask the local model a question with one command, and the bot stays responsive while it answers.
The trade-off
I kept the requests library and wrapped it in asyncio.to_thread() instead of switching to an async HTTP client like aiohttp. It fixed the blocking with one line, in code I already understood. The cost is a thread per request and no way to cancel one once it starts.
What I learned
Async code only stays async if every slow call respects it. One blocking request can freeze the whole bot.
What I'd fix next
Add a timeout to the model request (today a hung model server holds a thread forever), and split replies longer than Discord’s 2,000-character message limit.