A clean-room rebuild of the Tutelar community-management bot on
DiscordPHP dev-master, replacing
the legacy procedural bot (one 600-line class + a bag of closure "variable
functions") with a small, typed, testable core.
The design follows the discord-php-extension
and discord-php-bot-security skills, and borrows patterns from the sibling
projects DiscordPHP-MTG, -NHA and -Sabacc: config-is-data, a JSON
state store with atomic writes, secrets only from the environment, and a
MessageCommandClient subclass that boots a list of feature modules once the
gateway is ready.
| Legacy | Now |
|---|---|
token.php / secret.php / inline $options array |
Config from config.json, token from $TOKEN only |
VarSave() / VarLoad() global array |
Store — JSON, atomic temp-file+rename, stale-.tmp sweep |
perm_check() closure |
Permissions — typed, with a pure anyGranted() core |
status_changer_random + status.txt |
PresenceRotator module, list in config.json |
eight near-identical log_functions.php handlers |
one EventLogger module |
slash invite / whois closures |
SlashCommands module, user- and guild-installable |
| — (no way to see a raw member) | /member json [user] + the Member JSON right-click entry (MemberJson) — the member object as JSON, attached as a file when it outgrows a message |
| custom reaction-role engine | removed — see below |
The legacy bot maintained its own message-reaction → role-grant maps. Discord
now ships this as a first-class feature: Server Settings → Onboarding and the
Channels & Roles tab give members a supported, accessible role/channel
picker with none of the reaction-listener fragility (lost reactions on restart,
missing GUILD_MEMBERS, emoji-key drift, audit-log noise).
Tutelar therefore does not reimplement reaction roles. The
Onboarding module is a thin management
surface over the real thing:
/onboarding view— audit the live flow: every prompt, what roles/channels each option grants, the auto-opt-in channels, enabled state. Read-only./onboarding enable·/onboarding disable— flip onboarding on/off.
Both are gated on Manage Server; the writes additionally need the bot to hold Manage Server + Manage Roles. Actual prompt/option editing stays in Discord's own UI, which is the point.
When a server runs a member-verification form, Discord sends a join request
for every submission. The
Applications module turns that into
something a staff team can act on:
- a card in the
logchannel that pings whoever the server nominated, with the applicant, their account age, every question and answer, and Approve / Deny buttons (Deny takes an optional reason, shown to the applicant); - a configurable ping target —
/applications notifyopens a Discord mentionable select, so any mix of members and roles (up to 10) can be the ones pinged; pick none and it falls back to the server owner, which is the default. The card'sallowed_mentionsnames exactly those ids, so a role pings even when it is not "mentionable" (given Mention @everyone, @here and All Roles) and nothing else in the message pings at all; - optional auto-approval — an application is approved without a human when it passes every rule: a minimum account age, every required question answered, and no moderation cases in this server. Whatever held an application back is listed on its card.
Rules are per-guild and edited from Discord with /applications (Manage
Server); auto-approval is off until someone turns it on:
/applications view
/applications notify
/applications rules auto:true min_account_age:30 require_answers:true clean_record:true
Discord only sends join-request events to a bot holding Kick Members, which is also what approving one takes — without it the module is simply inert.
Tutelar (MessageCommandClient)
├─ Config (readonly, from config.json + env)
├─ Store (runtime per-guild overrides + module scratch)
└─ modules: [ PresenceRotator, SlashCommands, Onboarding, Applications, MemberJson, Moderation, Tickets, ModPanel, EventLogger, … ]
A module is anything implementing Module
(name() + boot(Tutelar $bot)). Tutelar boots them in registration order
after both init and application-init have fired, so $bot->application
and the guild cache are already populated. A module that throws in boot() is
logged and skipped rather than taking the process down.
Add one in bot.php:
$bot->addModule(new PresenceRotator())
->addModule(new SlashCommands())
->addModule(new Onboarding())
->addModule(new Moderation(new CaseBook($baseDir . '/var/moderation.json')))
->addModule(new EventLogger());Everything a bot-run server needs that Discord's own UI doesn't give a bot —
using only the API. One /mod command, guild-only, gated on a moderator
permission (kick / ban / timeout / manage server) and a role
hierarchy check (you can't action yourself, a bot, the owner, or anyone whose
top role isn't below yours).
| Discord gives you | Tutelar adds |
|---|---|
| kick / ban / unban / timeout | …and a numbered case for each, posted to modlog |
| a 90-day, unqueryable audit log | /mod case <n>, /mod modlogs @user, /mod reason <n> — a durable case book in var/moderation.json |
| — (no warning concept) | /mod warn, /mod warnings, /mod delwarn, with escalation: 3 → 1h timeout, 5 → 1d, 7 → kick, 10 → ban. The panel's History button lists the cases with a Delete button on each, so voiding one never needs its number |
| — (no scheduled unban) | /mod ban … duration:7d and a 30-second sweep that lifts it and closes the case |
| — (no private notes) | /mod note @user text |
| bulk delete (all-or-nothing, ≤100, <14 days) | /mod purge count [user] [contains] [bots] — filtered client-side, old messages skipped |
| manual permission-overwrite editing | /mod lock / /mod unlock — records and restores the prior send_messages state |
| slowmode in channel settings | /mod slowmode <seconds> |
| a "report" that goes to Discord Trust & Safety | a Report to mods message command that opens a private ticket channel for this server's mod team |
| — (no ticketing) | /ticket [user] [reason] and the report flow spin up a fresh channel at the server root — no category, visible only to roles with a moderator permission. Buttons: Close / Warn user / Add user. Every step is logged; Close files the full transcript to modlog as a .txt and deletes the channel. |
Set guilds.<id>.channels.modlog in config.json (it falls back to log).
Requires PHP 8.3+ and the sibling ../DiscordPHP and ../DiscordPHP-Http
checkouts (wired as Composer path repos).
cp env.example .env # then edit
COMPOSER_ROOT_VERSION=dev-main composer install
php bot.php.env / environment:
| var | meaning |
|---|---|
TOKEN |
required — bot token |
OWNER_ID |
owner Discord id (owner-only commands) |
TUTELAR_CONFIG |
config file path (default config.json) |
TUTELAR_STATE_PATH |
state file path (default var/state.json) |
config.json (committable — no secrets) holds github, the presence rotation
list + presence_interval, and per-guild channels / roles defaults keyed by
guild id:
EventLogger stays inert for a guild until channels.log is set here. The
Store (var/state.json) can layer runtime overrides on top of these defaults;
the command that would edit them at runtime is still on the to-do list, so for
now edit config.json and restart.
Privileged intents: bot.php requests GUILD_MEMBERS and MESSAGE_CONTENT
— enable both for the application in the Discord Developer Portal, or the gateway
will refuse the connection.
vendor/bin/phpunitPure logic only — no gateway, no network. phpunit.xml runs strict
(beStrictAboutCoversAnnotation, failOnRisky, failOnWarning); every test
method carries a @covers.
Done: rotating presence, per-guild channel/role config, event logging,
/whois · /invite · /ping · /member json, native onboarding management,
join-application review with optional auto-approval, and the full Moderation
module (see above).
Planned as further modules (kept out of this cut deliberately):
- Auto-moderation — a layer over Discord AutoMod: invite-link filter, join-rate raid gate, new-account gate. (The legacy bot's word-list ban system lives here.)
- SS13 integration —
discord2ckeyverifier link,/ckey, the verifier HTTP endpoint. - WebAPI — the legacy
webapi.phpsurface, as a module owning its ownReact\Httpserver. - Twitch relay — TwitchPHP bridge (was commented out in the legacy bot).
- Tiered
!smessage commands + a per-guild config command to replace the hand-edited templates and reach theStoresetters at runtime. - MySQL/PDO layer — only if a module actually needs relational storage; the JSON store covers everything so far.
MIT — see LICENSE.md. Original Tutelar bot by Valithor Obsidion / VZGCoders.
{ "github": "https://github.com/…", "presence_interval": 120, "presence": [{ "name": "over the server", "type": 3, "state": "idle" }], "guilds": { "1234567890": { "channels": { "log": "1112223334445556667" } } } }