Normative sources, read before contributing. This file only adds what they cannot tell you:
- CONTRIBUTING.md — checks to run and report, diff boundaries, Conventional Commits format.
- AI_POLICY.md — disclosure for tool-assisted work. Applies to agent-written code.
- DOCUMENTATION.md — how documentation is written, which content belongs where, and the Go doc comment rules CI does not check. Read it before adding exported identifiers.
CI runs these in order (.github/workflows/ci.yml):
test -z "$(gofmt -l .)"
go mod tidy && git diff --exit-code go.mod go.sum
go vet ./...
go test ./...
./custom-gcl run --timeout=5m ./...docs.yml builds the documentation tree and fails on any warning, and both
workflows must pass:
uv sync --frozen
uv run sphinx-build -W -b html docs public- Use
./custom-gcl, nevergolangci-lint. Stockgolangci-lint runaborts withplugin(nilaway): plugin "nilaway" not found, becausenilawayis a module plugin.custom-gclis a gitignored build of golangci-lint v2.13.2 + NilAway from.custom-gcl.yml. Build it once withgolangci-lint customafter cloning, and again after editing.custom-gcl.ymlor the pinned version.nix developprovidesgolangci-lintbut does not build the binary. - Focused runs:
go test ./internal/db/...,go test -run TestExpiry ./internal/db/. Tests are pure unit tests — no Discord or network needed, and the database tests need no server either: they open a SQLite file in a temporary directory.internal/community,internal/coreandinternal/dbhave tests; the other packages report "no test files". Tests of unexported helpers live in the package under test; public-API tests inpackage db_test. just formatappliesgofumpt, which is stricter than thegofmtCI checks. CI deliberately checks onlygofmt, so runjust formatbefore sending a change; the tree is kept gofumpt-clean.pre-commit run --all-filesis a superset ofci.yml(adds staticcheck, gosec, govulncheck). There is no hook for the docs build, so run that by hand.go-vulncheck-repo-modis not in CI and fails whenever the local toolchain trails the patched Go release. That is a toolchain-version signal, not a code defect — bump Go rather than deleting the hook.
One root module, github.com/Neon-Genesis-Linux/pen-bot, one binary:
cmd/pen-fun. internal/core owns process-wide state and startup.
community and moderation import core to register commands; core imports
db to open the database. Nothing imports core back from db or logger.
- Registration order is a contract.
community.Register()and anycore.RegisterIntents(...)must run beforecore.Start()(seecmd/pen-fun/main.go). Both append to package vars ininternal/core/handler.gothatStartreads when syncing commands and opening the gateway, so registering afterwards silently syncs nothing. - A command takes two calls:
core.RegisterCommands(...)for the Discord definition,core.Mux().SlashCommand("/name", h)for the handler. Subcommands nest viacore.Mux().Route("/name", ...)with"/sub"paths — seeinternal/community/xkcd.go. Slash commands only; prefix commands were removed in a breaking change. internal/loggerworks solely through a blank import; itsinitis the whole package. Drop_ ".../internal/logger"and the build still compiles while the bot silently falls back to slog's default handler.internal/moderationis registered bymain.go; it holds/self-timeout.
go vet and go test pass on code ./custom-gcl rejects.
exhaustruct_v5(explicit mode) covers.../pen-bot/.*: every field of your own structs must be set at every literal. Adding one field breaks all its literal sites. Third-party structs are exempt except the three listed in.golangci.ymlignore-patterns.goconstfires at 3 occurrences of a 3+ character string — extract aconst. That is whyxkcdCommandandtldrCommandare constants.intrange:for i := range n.errorlint:errors.Is, not==.goimportslocal-prefixesis the module path, so three groups: stdlib, third-party,github.com/Neon-Genesis-Linux/pen-bot/....reviveruns a curated rule list that deliberately omitsexportedandpackage-comments, so absent doc comments slip through the linter._test.gofiles are exempt fromerrcheck,gosec,unparam,exhaustruct_v5.
- The database is SQLite, through
modernc.org/sqlite. The driver is pure Go, so the build staysCGO_ENABLED=0and the distroless image is unchanged. It depends onmodernc.org/libc, and upstream asks that the two stay in step: Go's module resolution will not select alibcolder than the onemodernc.org/sqliterequires, but a newer one can be selected, and an incompatible one fails to compile rather than misbehaving. - Timestamps are Unix milliseconds in an
INTEGERcolumn, declaredDATETIME. Do not store them as text: the driver writes text while a column default writes an integer, and SQLite then orders the column by kind rather than by instant, so rows come back in the wrong order. The DSN parameters_time_integer_format=unix_milliand_inttotime=1are what make the driver read and write that representation. - The driver converts a timestamp on the way out only when the column's
declared type is
DATE,DATETIMEorTIMESTAMP. An aggregate or an arithmetic expression over such a column has no declared type, so it comes back as an integer and scanning it into atime.Timefails. Select the column itself, or convert the integer in SQL. - The driver reads an integer back as milliseconds only when it is larger than 1e12, which is September 2001. A timestamp before that is silently read as seconds. Every timestamp here is written by the running bot, so this cannot happen; a column holding a historic timestamp would need care.
- Migrations are
//go:embeded frominternal/db/migrations/and applied by the running bot at startup (db.ApplyMigrations). There is no migration CLI. AddNNN_name.sql; the filename is recorded inschema_migrationsand the file runs in one transaction. - A migration file is handed to the driver as one statement, not split on semicolons, so a semicolon inside a string literal is safe. Keep migrations to plain DDL anyway.
- Primary keys are
INTEGER PRIMARY KEY AUTOINCREMENT. TheAUTOINCREMENTis required: without it SQLite reuses an id after a delete, which breaks the keyset pagination that walks a log by(created_at, id). - An index that serves a query has to carry the order the query asks for. The
moderation log index ends with
idbecause the pagination cursor compares the pair(created_at, id): without it SQLite sorts every page again with a temporary B-tree over the whole table.internal/db/migrations_test.goasserts the query plan, so a change that reintroduces the sort fails the build. db.GlobalDB()is filled asynchronously, afterStarthas already returned to its signal wait (internal/core/start.go), and stays nil forever if the database cannot be opened — the bot runs happily without a database. Callers must tolerate nil rather than assume a live handle.- SQLite has one writer. The DSN sets
journal_mode(WAL), abusy_timeoutand_txlock=immediate, and the pool is small. Do not raise the pool size to raise write throughput: writes are serialised regardless. _txlock=immediateis load-bearing, not a default. A deferred transaction that reads and then writes cannot always take the write lock afterwards, and fails withSQLITE_BUSYin a way the busy timeout does not retry.- The cache in
internal/db/cache.gois a process-local map with TTL, not Redis.
- Every setting is an env var read at call time. Nothing loads
.env— there is no dotenv library and.envrcis onlyuse flake.compose.yamlinjects it viaenv_file; for a local run, export the variables yourself. Full list in.env.example. ENV=productionselects INFO, anything else DEBUG (internal/logger).GUILD_IDis optional: set it to sync commands to one guild (instant) instead of globally (slow to propagate).DB_PATHnames the database file.DB_BOT_INSTANCE_IDnames the file whenDB_PATHis unset, so that instances sharing a directory each get their own. The container image setsDB_PATH=/data/pen-bot.dband mounts a volume there.- The compose file is
compose.yaml(Compose v2); usedocker compose up, ordocker compose watchto honour thedevelop.watchrebuild. Keep.dockerignore— it is what keeps.envand the 52MBcustom-gclout of the build context.
- Conventional Commits, lowercase type, optional scope; match the scopes used in
recent
git log. - Do not push, open a pull request, or comment on an issue unless asked.
- Never add
Signed-off-by,Co-developed-by, or any other certification on a contributor's behalf (see AI_POLICY.md). .envis gitignored and secret; keep it out of diffs.