Discord AI Agent: Self-Hosted Setup With POLYROB
A Discord AI agent is an AI assistant you talk to in Discord, in a DM or in a server channel. It acts for you: it uses tools, keeps memory and answers in the same chat. POLYROB runs one on your own machine. The command polyrob discord connects to Discord’s gateway websocket as a bot, so it needs no public URL, no webhook and no open port.
Most guides on this topic show how to wire a hosted bot builder into a server. This one covers the other case: your own agent, on your own hardware, that you reach from Discord. Every POLYROB claim comes from the public repository at release v1.7.9, and the files are listed at the end.
What is a Discord AI agent?
A bot relays a message to a model and posts the text back. An agent also does work: it browses, reads and writes files, runs scheduled jobs and remembers you between sessions. In POLYROB, Discord is one way to reach the same agent. It answers in the terminal, the web console and eight chat apps, and it keeps one memory across them. For the wider picture of running an agent yourself, read the guide to a self-hosted AI agent.
How does POLYROB connect to Discord?
Through the Discord gateway, a websocket your machine opens to Discord (Discord, Gateway documentation). Because your machine makes the call, nothing has to reach it from outside. That is the main difference from WhatsApp, where Meta calls a public webhook.
The gateway code is a small hand-written client:
- It asks for four intents: guilds, guild messages, direct messages and message content. Together they make the number 37377.
- It sends a heartbeat on the interval Discord gives it. If a beat goes unanswered, it closes the socket and reconnects.
- It reconnects with a wait that doubles from 1 second up to 60 seconds. The wait resets only after Discord confirms the session, so a socket that opens and closes at once does not hammer the gateway.
- It does not resume a dropped session. It signs in again, and the file header says the cost is at most the messages sent during the gap. A dedup store,
discord_dedup.db, makes redelivery safe. - Each incoming message runs as its own task. A long turn does not block the read loop, so the heartbeat keeps being answered.
What do you need before you start?
A bot token and a model. There is no extra to install: the Discord surface has no optional dependency group in the surface catalog. Create the application in the Discord developer portal (Discord developer applications), add a bot, and turn on the MESSAGE CONTENT privileged intent. Then invite the bot to your server with the bot scope.
| Setting | What it does |
|---|---|
DISCORD_BOT_TOKEN | The bot token. You can pass --token instead; the option wins over the variable. |
OWNER_DISCORD_ID | Your numeric Discord user ID. A sender whose ID equals it is the owner on Discord. |
GROUP_CHAT_ENABLED | Off by default. Needed before the bot answers in a server channel. |
How do you start it?
Install POLYROB, export the token and run the command.
curl -fsSL https://polyrob.dev/install.sh | bash
export DISCORD_BOT_TOKEN=your-bot-token
polyrob discord
It prints discord bot online, then listening. If the token is missing, the command stops with one message that names --token and DISCORD_BOT_TOKEN. To check a token without starting the agent, run polyrob surfaces probe discord. It reads the bot’s own profile and its application flags, and it tells you if the MESSAGE CONTENT intent is off. Without that intent, Discord closes the gateway with code 4014 on every connect.
If you run several chat apps, polyrob gateway starts every enabled surface in one process. Discord is skipped with a warning when the token is missing.
Who can talk to it?
Only you, until you say otherwise. On every network surface except Telegram, a DM from anyone who is not the owner or a paired sender is denied, even when the correspondent model is off. Set OWNER_DISCORD_ID to your user ID so your messages count as the owner’s.
The bot also ignores other bots, and it ignores its own messages. It posts with mention parsing turned off, so a reply that contains @everyone or a role name does not ping anyone. The AI agent security guide explains the access model behind this.
Can you use it in a server channel?
Yes, once you opt in twice. Turn on GROUP_CHAT_ENABLED, then allow each channel by ID:
polyrob config set GROUP_CHAT_ENABLED true
polyrob owner groups allow discord 123456789012345678
The start-up line then reads group chat ON, and only allowlisted channels respond. An unlisted channel is dropped silently: nothing is stored and nothing is sent. Inside an allowed channel the bot answers when it is mentioned, by default. Each sender has a role (owner, admin, member or blocked), and a room turn runs on a narrower toolset than a DM, with no owner facts, no wallet and no memory recall. The groups guide is the full reference.
What does it do in the chat?
- Typing and progress. The bot shows the typing state and renews it every 8 seconds, because Discord’s own state lasts about ten. It also adds a reaction to your message while it works, removes it when the turn ends, and leaves a different one if the turn failed.
- Long replies. Discord caps a message at 2,000 characters. The surface splits longer text into several messages, and a reply to your message keeps the reply link.
- Streaming. The surface buffers partial output and sends one message at the end, rather than editing a message as text arrives.
- Attachments in. A photo or file arrives with the message. A message with an attachment and no text still counts as a message. Files are fetched only from Discord’s own CDN hosts, and only if the agent needs the bytes.
- Files out. Files the agent makes are uploaded as attachments after the text. A missing file never drops the text.
- Voice notes. The comparison page lists Discord among the surfaces whose voice notes are transcribed.
- Scheduled messages. A cron job can post to a channel, and it can open a DM first when the target is a user ID.
Where does the Discord surface fall short?
- It runs only while the process runs. If the machine sleeps or the process stops, the bot goes quiet. Messages sent during the gap are not replayed.
- No session resume. A dropped connection means a fresh sign-in, as described above.
- Rooms have no memory of the conversation. The groups guide says a Discord room records no lines, so the agent answers each message without the room’s history. Allowlists, roles and modes work. Rooms are fully useful on Telegram only.
- It is not a moderation bot. The Discord client in the code sends and edits messages, shows typing, reacts, opens DMs and uploads files. It has no ban, kick or role calls.
- A fatal close stops it. If Discord rejects the token or the intents (codes 4004, 4010 to 4014), the gateway stops and logs why, instead of looping, because a loop can get a token reset. You fix the cause and restart.
- It needs a model key. POLYROB does not host a model. You use your own provider key, or a local model.
Can the same agent run in other chat apps?
Yes. POLYROB has eight chat surfaces: Telegram, email, WhatsApp, Discord, Slack, Signal, Feishu/Lark and DingTalk. The Telegram AI agent guide covers the phone-first case with a full room history, and the Slack AI agent guide covers the workplace case. For the owner-chat-plus-goals setup, see the personal AI agent guide. If channel count decides it for you, check the dated numbers on the comparison page.
Discord AI agent questions
How do I create an AI agent for Discord?
Create an application and a bot in the Discord developer portal, turn on the MESSAGE CONTENT intent, and invite the bot. Then set DISCORD_BOT_TOKEN, set OWNER_DISCORD_ID to your user ID, and run polyrob discord.
How do I contact a Discord agent?
Send the bot a DM. If you are the owner, the DM is your private chat with the agent. In a server, mention the bot in a channel you have allowed with polyrob owner groups allow discord <channel_id>.
Do I need a public URL?
No. The gateway is a websocket that your machine opens, so it works behind a home router or a firewall.
What is the best bot for Discord management?
That depends on what you manage. POLYROB is an agent you talk to, not a moderation tool: its Discord client has no ban, kick or role calls. Use a moderation bot for that job, and POLYROB for work you would ask an assistant to do.
Is it free?
POLYROB is MIT licensed. You pay your model provider, or nothing with a local model. Discord sets its own rules for bots, so read them before you plan around volume.
Where do you start?
Create the bot, run the three commands above, and send it a DM. Then allow one channel if you want it in a server.
Install POLYROB, read the docs, or browse the open-source code. To see what changed in recent releases, check what’s new.
Sources
All POLYROB facts are from the public repository at release v1.7.9 (github.com/theselfruleorg/polyrob).
cli/commands/[email protected](thepolyrob discordcommand, token resolution, start-up lines, group-chat line)surfaces/discord/[email protected](intents, heartbeat, reconnect backoff, no resume, fatal close codes, bot filtering, attachments, owner alias)surfaces/discord/[email protected](typing pulse, reactions, dedup store, DM delivery, CDN fetch)surfaces/discord/[email protected](2,000-character split, reply link, buffered streaming, file upload)surfaces/discord/[email protected](REST calls, mention parsing off)surfaces/discord/[email protected],surfaces/discord/[email protected](polyrob surfaces probe discord, gateway launch)core/surfaces/[email protected](Discord surface row, no extra,OWNER_DISCORD_ID)docs/[email protected](DISCORD_BOT_TOKEN,OWNER_<SURFACE>_ID)docs/guide/[email protected],docs/guide/[email protected](denied DMs, room allowlist, roles, Discord room ledger limit)docs/[email protected](eight chat surfaces, voice notes)- Discord: Gateway documentation and developer applications, linked for setup, not quoted.
POLYROB is an open-source AI agent you run and own. Install it or read the docs.