Documentation / Guides
A Discord chess bot in 60 lines
answer /analyze with a board and the best line
What it does
/analyze <fen> replies with the evaluation, best move and the top line. Uses discord.js and the API; no engine process.
import { Client, GatewayIntentBits, SlashCommandBuilder } from "discord.js";
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on("interactionCreate", async (i) => {
if (!i.isChatInputCommand() || i.commandName !== "analyze") return;
await i.deferReply();
const res = await fetch("https://stockfishapi.com/v1/eval", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.STOCKFISH_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ fen: i.options.getString("fen", true), depth: 14, multipv: 1 }),
});
const d = await res.json();
if (!res.ok) return i.editReply(`${d.title}: ${d.detail}`);
const score = d.eval.type === "mate" ? `M${d.eval.value}` : (d.eval.value / 100).toFixed(2);
await i.editReply(`**${score}** best ${d.bestmove} · ${d.pvs[0].moves.join(" ")} (depth ${d.depth}, ${d.cost_units} units)`);
});
client.login(process.env.DISCORD_TOKEN);
Create one key per bot with POST /v1/keys {"label":"discord","scopes":["eval"],"budget_units":20000} so a leaked token has a ceiling.
Get a key with one call: POST /v1/signup {"email":"you@example.com"}. Free plan, depth 14, 2,000 units a day.