Discord
People talk to kōjō in Discord by mentioning the bot in a channel of a server or by direct message. kōjō answers with a reply to the message and asks for approvals with buttons.
See Surfaces for how Discord compares with the other chat platforms, and Who may trigger what for what a request may do, depending on who sent it.
How it connects
kōjō opens the connection, to Discord’s gateway, and receives messages and button clicks over it. Discord needs no route to kōjō: no public URL, no webhook, no interactions endpoint.
One bot serves one server, the one named by guild_id. Messages from any other server the bot was added to are ignored, and a direct message is only taken from a member of that server. The server is what bounds who can reach kōjō, the way a workspace does in Slack.
kōjō keeps one WebSocket connection to Discord’s gateway and calls Discord’s HTTP API to post. Both are outbound, on port 443: the API at discord.com, the gateway at gateway.discord.gg and, to resume, at the host under discord.gg that Discord names when the session starts.
sequenceDiagram
participant K as kōjō
participant A as Discord HTTP API
participant G as Discord gateway
K->>A: GET /gateway/bot
A-->>K: gateway URL, session starts left today
K->>G: connect
G-->>K: Hello with the heartbeat interval
K->>G: Identify with the token and the two intents
G-->>K: Ready with a session ID and a resume URL
loop every heartbeat interval
K->>G: Heartbeat with the last sequence number
G-->>K: Heartbeat ACK
end
G-->>K: MESSAGE_CREATE, a mention or a direct message
K->>A: POST the reply
G-->>K: INTERACTION_CREATE, a click on a button
K->>A: POST the acknowledgement, then PATCH the message
Note over K,G: The connection drops, or Discord asks to reconnect
K->>G: connect to the resume URL, Resume with the session ID and sequence number
G-->>K: the events that were missed, then Resumed
- Heartbeats. kōjō sends one per interval, the first after a random part of it. When Discord does not acknowledge a heartbeat by the time the next is due, kōjō treats the connection as dead and opens a new one.
- Resume. After a drop, a reconnect request or a close that allows it, kōjō resumes the session, and Discord replays the events sent in between. An event that arrives twice is handled once.
- New session. When Discord says the session cannot be resumed, kōjō waits one to five seconds and identifies again. Discord allows 1,000 new sessions a day; when none are left, kōjō waits for the limit to reset.
- Backoff. The pause before a new connection starts at about one second and doubles, up to one minute, while connections keep failing.
- Stops. kōjō stops with an error, instead of reconnecting, when Discord refuses the token or closes the connection over the intents or the API version. A restart cannot fix those; the configuration has to change.
- Rate limits. kōjō reads the rate limit headers of every answer and holds a call back while its bucket is used up. A call Discord answers with
429is repeated after theretry_afterit names.
Set up
-
In the developer portal choose New Application. Copy the Application ID from the General Information page, and leave Interactions Endpoint URL empty: with a URL there, Discord sends button clicks to it instead of the gateway.
-
On the Bot page, choose Reset Token and copy the token. It is shown once. Turn Public Bot off, so that only you can add the bot to a server.
-
On the same page, leave the three Privileged Gateway Intents off. kōjō identifies with two intents, neither of them privileged:
Intent Delivers GUILD_MESSAGESMessages in the server’s channels and threads DIRECT_MESSAGESDirect messages to the bot Without the Message Content intent Discord still delivers the text of direct messages and of messages that mention the bot, and those are the only messages kōjō acts on. Button clicks need no intent.
-
Print the invite URL and open it as someone who may manage the server:
kojo setup discord --application-id 1200000000000000001The URL asks for the
botscope and these permissions:Permission Used for View Channel Receive the messages that mention the bot Send Messages Answer in channels Send Messages in Threads Answer in Discord threads Read Message History Post the answer as a reply to the message that asked Channels the bot’s role cannot view are closed to it. Where it lacks Read Message History it answers with a plain message instead of a reply.
-
Collect the IDs. In Discord turn on Developer Mode (User Settings → Advanced), then right-click the server and choose Copy Server ID. For a role, open Server Settings → Roles and choose Copy Role ID from the role’s menu.
-
Add Discord to
kojo.yaml:chat: discord: bot_token: ${DISCORD_BOT_TOKEN} application_id: "1200000000000000001" guild_id: "1100000000000000001" # the server kōjō serves roles: # Discord role ID: kōjō role "1100000000000000002": operator # @maintainers "1100000000000000003": skill:edit # @docs users: # Discord user ID: email of an rbac entry "1300000000000000001": ana@example.com -
Run
kojo doctor. Itschat discordcheck verifies that the token belongs to a bot, that it is the bot ofapplication_idwhen that is set, that the bot is a member of the server, and that every role ID underrolesexists there.
| Key | Required | Meaning |
|---|---|---|
bot_token |
yes | The bot’s token. |
guild_id |
yes | The ID of the server kōjō serves. |
application_id |
no | The application’s ID. When set, kojo doctor checks the token against it. |
roles |
no | Discord role ID → kōjō role. |
users |
no | Discord user ID → email address of a kōjō user. |
Quote the IDs. They are numbers of 17 to 20 digits, and kōjō reads them as text.
Roles
Slack tells a bot the email address of who wrote, and kōjō looks that address up in its own access settings. Discord gives a bot no email address, so the roles a person has in the Discord server decide instead.
rolesmaps a role of the server to one of kōjō’s roles:viewer,skill:edit,operatororadmin. Someone who holds several mapped roles has all of them. The server’s own ID stands for@everyone, so"<server ID>": viewergives every member a role.usersmaps one person to an email address. kōjō then also gives that person the roles the access settings (Settings → Access) hold for the address. Use it for people whose roles are managed in kōjō.- The two add up. Someone who ends up without the operator role is still answered, but the request runs untrusted: read-only skills, nothing published. See who may trigger what.
In a channel of the server, Discord sends the author’s roles with every message, so a role change counts from the next message on. A direct message carries no roles: kōjō asks Discord for the sender’s membership of the server named by guild_id, uses the roles it finds, and keeps the answer for one minute. Someone who is not a member of the server gets no answer.
Whoever can assign a mapped role in Discord can thereby give that kōjō role. Map roles that only the people you trust with that can hand out.
roles and users are part of kojo.yaml, so a change takes effect when kōjō restarts. A value that is not one of kōjō’s roles is refused when the file is loaded.
Mentions, messages and replies
Mention the bot anywhere in a message, in any channel or thread of the server the bot can see, or write to it directly:
@kojo why does the nightly build of github/acme/api fail?
Each message becomes one session, unless it goes to a session already at work (below). The mention is removed from the text, and the repository is chosen as in Slack.
kōjō answers with a Discord reply to the message that asked, in the same channel. A reply works the same way in a channel, in a thread and in a direct message, and it notifies the person who asked when the answer arrives, which can be minutes later. To continue, reply to kōjō’s answer: a reply mentions its author unless you turn the ping off, so it reaches kōjō without typing the mention again. While the session is at work, an operator’s reply to the message that started it, or to one of kōjō’s messages that answer it, goes to that session: its next step reads it. Once the session is done, or when the reply comes from someone without the operator role, a follow-up is a new session. See Talking to a session at work.
What kōjō does not act on:
- Messages in the server that do not mention the bot. A mention of a role,
@everyoneor@hereis not a mention of the bot. - Messages of other bots and of webhooks, edits of earlier messages, and system messages such as joins and pins.
- Messages from servers other than
guild_id, and direct messages from people who are not members of it.
When kōjō cannot take a request, for example because its database is unreachable, it says so in a reply.
Messages kōjō writes
kōjō writes Markdown, and Discord renders its own dialect of it. The adapter translates the differences:
| In the text | In Discord |
|---|---|
| Headings of level 1 to 3 | Headings |
| Headings of level 4 to 6 | A bold line |
| A table | A code block with aligned columns |
__text__ |
**text** (double underscores underline in Discord) |
| A link to a relative path, or an image | The label followed by the target, or a plain link |
| A task list | A list with ☐ and ☑ |
| A fenced code block | Unchanged, with its language |
One Discord message holds 2,000 characters. A longer text continues in further messages, cut at a blank line where possible; a code block that spans the cut is closed and opened again with its language. Only the first message is a reply.
Nothing in a message kōjō writes notifies anyone except the person it replies to: @everyone, @here, and mentions of roles and users are shown but do not ping. Link previews are turned off.
Approvals
When the merge policy asks a human, or someone asks for a schedule, kōjō posts the question with Approve and Deny buttons as a reply to the request and parks the session until someone clicks or the question’s deadline passes. The questions, and what happens without an answer, are the ones listed under Slack.
A click is acknowledged at once, within the three seconds Discord allows, and decided after that:
- Someone with the operator or admin role decides. The buttons are replaced by a line under the question that says what was decided and by whom. The first valid click stands.
- A click from anyone else changes nothing. The buttons stay, kōjō replies to the question that the operator role is needed, and the attempt is written to the audit log.
The roles of who clicked are found as for a message: from the click itself in the server, by a lookup in the server for a click in a direct message.
Schedules
To send a schedule’s result to Discord, name a channel as notify: discord:<channel ID>, or a person as notify: discord:user:<user ID> for a direct message.