Repository navigation
feat(bot): export and import the settings of a server - #293
Conversation
`/settings server` becomes a group of `menu` (the settings menu it opened until now), `export` and `import`, so a configuration can be copied to another server or kept before changing a lot of it. `export` answers with a JSON file of the settings that do not depend on the server and its auto-moderation rules. Roles, channels and users stay out, and so do the exempt roles and channels of the rules. `import` reads such a file, shows what it would change and asks to confirm before it writes anything. The file is not trusted: every setting goes through the `isValid` of its serializer and every rule through `parseAutoModerationRulePatch`, and a file with a problem is refused whole with all its problems. The write is one database transaction for the settings and the rules, worked out again under their locks when the administrator confirms. Resolves #275
|
4 findings · reviewed 🟠
|
There was a problem hiding this comment.
Important
The settings lock is released before the import writes anything, so the "worked out again under the locks" guarantee does not hold. It is a one-word fix.
Reviewed changes
I read the full diff of the single commit, then traced the lock and transaction paths through functions.ts, rules.ts and the database storage.
/settings serveris now a group. It holdsmenu,exportandimport, and the stale/settings serverreferences (locales, generated typings,confhandler, docs) are updated.- Export.
createConfigurationFilewrites only the settings of portable types plus the rules without their exempt roles and channels. - Import pipeline.
parse.tsvalidates the file whole,plan.tsdiffs it against the server,render.tspreviews it, andapply.tsrecomputes the plan and writes everything in one DB transaction. - Storage.
Transaction#submitandwriteGuildDatatake analongsidecallback, andwriteAutoModerationRulePlanwrites the rules in the same transaction.
I did not find problems in parsing, planning or the storage changes. The paths I traced for rules-only and settings-only plans write correctly. I did not read the tests or the generated typings in detail.
⚠️ applyConfigurationImport is untested
applyConfigurationImport has no test. The new tests cover format, parse, plan, render and the two storage functions, but not the locking and ordering in apply.ts. A test would have caught the early lock release above.
Technical details
# Add a test for `applyConfigurationImport`
## Affected sites
- `projects/bot/src/lib/database/settings/transfer/apply.ts` — no coverage in `projects/bot/tests/lib/database/settings/transfer/`
## Required outcome
- A test that exercises the apply path: settings-only, rules-only and mixed plans, an empty plan writing nothing, and the settings lock still being held while the rules callback runs.claude-sonnet-5-5 | 𝕏
The transaction of the settings is disposed when applyConfigurationImport returns, and the write of the rules was returned without being awaited: the lock was released before the rules were read, so the import planned on settings another write could change meanwhile.
parseAutoModerationRulePatch normalizes the options it reads: a value of the wrong type takes its default, a number is brought within its limits and a bad entry of a list is dropped. A file with `options.words: "spam"` was accepted and emptied the words of the rule it was imported on. The options are now checked as they are written, and each bad value is a problem of the file.
A rule that was migrated with more than 200 entries in a list keeps them, and its export holds them all, but the import read every rule as a new one: the whole list counted as growth and the backup was refused, on the server it was made in too. A list of the file can now be as long as the one the rule of the same name and type has in the server, and is still held to the limit otherwise. The allowance is checked again under the locks when the import is written.
A message is held to the first enabled rule of a type it infringes, and the rules are read by their ID. An import kept the ID of the rules the server had and gave the new ones later IDs, so a warning the file had before a ban could end up after it and the ban won; importing the file again found nothing to do. The rules have no position of their own, so the rules of a type that are new or out of place are made again in the order of the file, after the ones that stay: a rule that is made again keeps its exempt roles and channels and loses its ID and its counters, which the preview says. An order that only differs is now a change.
The import waited for its answer with a MessagePrompter, which keeps the pending answer in the memory of the process that asked. With the sharder the shards share the port of the interactions, so the click on Import could reach another shard, which took the button for an expired one and imported nothing. What the file holds now waits in Redis for five minutes, under the ID of the interaction of the command, and the buttons are answered by the settingsImport interaction handler. The import is taken with getdel, so two clicks write it once, and it is still worked out again under the locks when it is confirmed.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
I reviewed the five fix commits since the prior pullfrog review (the V7 merge in between has no PR-authored logic).
- Settings lock held through the write.
applyConfigurationImportnow doesreturn await writeAutoModerationRules(...), and the newapply.test.tsrecords the lock order so a missingawaitfails. - Rule options validated as written.
checkAutoModerationRuleOptionsrefuses wrong types, out-of-range numbers, bad list entries and unknown options before the patch can repair them, so a broken file can no longer empty a rule's list. - Oversized lists restorable. A list may be as long as the same-name, same-type server rule's list.
findOversizedRuleListsrechecks this under the locks and throwsConfigurationImportStaleError. - Rule order preserved per type. Rules out of order are deleted and recreated with their exempt roles and channels. The preview and result counts report them as
moved, and importing the same file twice is a no-op. - Confirmation works across processes. The parsed file is kept in Redis for five minutes and taken with
getdel. The newsettingsImporthandler answers the buttons on whichever process gets the click, after checking the owner and theadministratorprecondition.
claude-sonnet-5-5 | 𝕏
| const parsed = await takePendingImport(guildId, action.ownerId, action.importId); | ||
| if (action.verb === 'cancel') return component.update(close(t('commands/conf:transferImportCancelled'))); | ||
| if (parsed === null) return component.update(close(t('commands/conf:transferImportExpired'))); |
There was a problem hiding this comment.
Repeated clicks hide import results
When an administrator clicks twice before the preview buttons disappear, the second interaction can overwrite a successful import result. takePendingImport removes the saved file for the first click, but a later Cancel still updates the shared message to “Nothing was imported,” and a second Import updates it to the expiration message. If that second update arrives after the success update, the incorrect message remains visible even though the import completed.
This is non-blocking: the write happens only once, but the misleading result can make the administrator repeat the restore or misunderstand which settings are active. When the saved import has already been consumed, acknowledge the second click without changing the original preview, or send an ephemeral reply.
Artifacts
Executable repeated-click handler tests
- Runs seven actual-handler scenarios with shared Discord message mocks and controlled persistence timing to demonstrate when duplicate clicks overwrite success.
Focused handler test configuration
- Reuses repository aliases and setup while selecting only the supplied candidate's generated tests, keeping the final verification narrow.
- Captures the executed command, working directory, exit code, seven passing assertions, and observed final messages, confirming the overwrite behavior.
Observed Discord message transitions
- Records interaction delivery, persistence completion, message updates, and final shared-message snapshots from the executed tests, exposing the decisive update order.
Chromium message-state capture script
- Uses Playwright on Chromium to render recorded handler snapshots into comparable videos, with longer holds on the success and misleading result messages.
▶ Single Import control recording
- Replays the executed single-click control from preview through one mocked persistence commit to the retained success message, establishing the comparison baseline.
- Captures the control's final mock-rendered Discord message after one persistence commit, showing the successful result remains visible.
▶ Repeated Import and Cancel recording
- Replays sequential and concurrent repeated-click traces, emphasizing false expiration or cancellation messages despite one completed persistence commit.
Cancel overwrites success poster
- Captures the concurrent Cancel update applied after success with one persistence commit and the final text Nothing was imported, showing the incorrect terminal result.
Chromium capture execution output
- Captures the rendering command, working directory, exit code, and selected scenarios' final messages, confirming both recordings were generated successfully.
Ran code and verified through T-Rex
Prompt To Fix With AI
This is a comment left during a code review.
Path: projects/bot/src/interaction-handlers/settingsImport.ts
Line: 39-41
Comment:
**Repeated clicks hide import results**
When an administrator clicks twice before the preview buttons disappear, the second interaction can overwrite a successful import result. `takePendingImport` removes the saved file for the first click, but a later Cancel still updates the shared message to “Nothing was imported,” and a second Import updates it to the expiration message. If that second update arrives after the success update, the incorrect message remains visible even though the import completed.
This is non-blocking: the write happens only once, but the misleading result can make the administrator repeat the restore or misunderstand which settings are active. When the saved import has already been consumed, acknowledge the second click without changing the original preview, or send an ephemeral reply.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Dismissed because a newer commit was pushed; Greptile will re-review the current head.

🔗 Linked issue
Resolves #275
❓ Type of change
📚 Description
Setting the bot up in a second server meant redoing every setting and every auto-moderation rule by hand, and there was no copy to go back to after changing a lot.
/settings server exportanswers with a JSON file of the settings that do not depend on the server and its rules, and/settings server importshows what a file would change and asks before it writes anything.What a reviewer should look at:
automodsection, the rules of the server become the ones of the file. A rule with the same name and type is edited in place and keeps its ID and its exempt roles and channels. Settings the file does not have are left alone.Questions:
/settings serveris now a group. Discord does not allow a subcommand that has subcommands, so the menu moved to/settings server menuandexportandimportsit next to it, as the issue says. Would you rather keep/settings serveras the menu and have/settings exportand/settings import?
Confidence Score: 5/5
No blocking findings remain in this review; the outstanding repeated-click issue affects the displayed result rather than the import itself and is non-blocking.Reviews (4) · Last reviewed commit: "Merge branch 'V7' into feat/settings-exp..." · Reviewed by Greptile