Skip to content

04Architecture

5 min read

By Wyatt McPherson

What a Discord bot actually costs to run

Most bots cost next to nothing to run. The cost depends on one choice you make before writing any code, which is whether the bot listens to everything or only answers when someone asks.

  • Discord
  • Serverless
  • Ed25519
  • Next.js

How much does it cost to run a Discord bot?

If it only answers slash commands, close to nothing. Discord calls a web address you already host, so you pay a fraction of a cent per command. A bot that reads every message or uses voice needs a small server running all day, and you pay for that even when no one’s using it. Most bots only answer commands, so most don’t need one.

01

Two kinds of bot

Every Discord bot is one of two kinds. A gateway bot opens a WebSocket to Discord and holds it open, receiving every event in every server it’s in: messages, joins, reactions, typing. It has to stay connected all the time, so it’s a process running twenty-four hours a day whether anybody uses it or not.

The other kind waits for Discord to call it. When somebody runs one of your slash commands, Discord posts to an HTTPS endpoint you choose, you answer within three seconds, and nothing of yours runs in between. On any serverless host that’s one function call: milliseconds of compute, counted per command, on hosting your website already pays for.

Most of the bots people describe to me were built as gateway bots but only ever answer commands. If what it does is ‘someone asks it something and it answers’, it doesn’t need to see a single message.

02

What Scorebug’s bot costs

Scorebug’s bot answers seven commands from live sports data. It runs as one route in the marketing site’s existing deployment. A command costs one function call and a few tens of milliseconds, and the card it replies with is cached for a year. A card only depends on its URL, so the same parameters always draw the same pixels.

Built that way, the bot’s share of the bill is too small to notice. There’s no separate host, no container, no keep-alive ping, and no idle process to pay for on days nobody runs a command.

A gateway bot for the same product would have meant a small always-on server, a process supervisor, and a way to reconnect when Discord drops the socket. That’s real money and more to look after, for things the bot never needed to do.

03

Where the cost actually is

Where a bot does cost money is in building it. Most of that is the data it answers from: the query that has to come back inside Discord’s three-second window, the empty state for a team nobody has logged yet, the autocomplete that has to feel instant while somebody is still typing.

The other part is checking requests. Discord signs every request it sends with Ed25519, and your endpoint has to reject anything that fails the check, or anyone who learns your URL can make your bot say things. The check runs before any request reaches the handler, and it’s the part of a Discord build I’m fussiest about.

Checking the signature before anything else runsts
const key = await crypto.subtle.importKey(
  "spki",
  spkiFromRawEd25519(publicKey),
  { name: "Ed25519" },
  false,
  ["verify"],
);

const ok = await crypto.subtle.verify(
  "Ed25519",
  key,
  hexToBytes(signature),
  new TextEncoder().encode(timestamp + rawBody),
);

if (!ok) return new Response("bad signature", { status: 401 });

04

The one permission it needs

A slash-command bot needs one permission: to send messages. It can’t read message history, it can’t see your member list, and it receives nothing except the command somebody typed. Moderators nearly always ask about this first, and it’s an easy answer: the bot can’t see their messages, so it can’t leak them.

It also limits the damage if the token ever leaks. With only Send Messages, someone could post as your bot. With Administrator, they could take over the server.