Enhance memory usage - #61
Open
CataDev0 wants to merge 3 commits into
Open
Conversation
…s, Bounds and Sweeping regularily.
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce KaikiBot’s long-running memory footprint by tightening Discord.js caching behavior (limits + sweepers) and reducing per-guild cache allocations, while also adding developer documentation around Discord privileged intents.
Changes:
- Add Discord.js cache limits and sweepers (members/messages), and adjust code paths to fetch users/members on demand.
- Optimize emote-react cache storage by introducing a shared “empty guild” sentinel and per-guild eviction on
guildDelete. - Add
KaikiUtil.mapWithConcurrency(with tests) and apply it to member/user processing to reduce API bursts; add intents documentation.
Reviewed changes
Copilot reviewed 20 out of 21 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/tests/KaikiUtil.test.ts | Adds unit tests for the new mapWithConcurrency utility. |
| src/tests/KaikiCache.test.ts | Adds tests for shared empty sentinel + ensureGuildCache behavior. |
| src/struct/db/Database.ts | Re-sync guild DB row into in-memory map; updates KaikiCache construction. |
| src/struct/Constants.ts | Adds cache/sweeper constants and a music queue limit constant; removes unused constants. |
| src/services/Webserver.ts | Notes intentional under-reporting due to member sweeping. |
| src/services/MusicService.ts | Enforces a maximum music queue length. |
| src/services/DiscordBotListService.ts | Switches DM target lookup from cache to on-demand fetch (LRU-capped cache). |
| src/services/AnniversaryRolesService.ts | Fetches members explicitly and processes them with bounded concurrency; uses Sets. |
| src/listeners/guildDelete.ts | Evicts per-guild in-memory entries on guild removal to prevent accumulation. |
| src/listeners/guildCreate.ts | Re-hydrates dad-bot exclusions after re-join; avoids redundant member fetch. |
| src/lib/KaikiUtil.ts | Introduces mapWithConcurrency helper. |
| src/lib/Kaiki/KaikiSapphireClient.ts | Uses mapWithConcurrency + fetch-on-demand for reminders; removes periodic member fetch loop. |
| src/lib/Kaiki/KaikiClientConfig.ts | Configures Discord.js cache limits and sweepers. |
| src/lib/Cache/KaikiCache.ts | Adds shared empty cache sentinel + ensureGuildCache; adjusts emote-react population. |
| src/data/distros.json | Removes distro data file. |
| src/commands/Server settings/removeEmoteReact.ts | Updates emote-react cache removal logic and sentinel fallback. |
| src/commands/Server settings/addEmoteReact.ts | Ensures cache is populated/converted from sentinel before writes. |
| src/commands/Owner only/serverlist.ts | Adds owner-only paginated server listing command. |
| src/commands/Fun/ship.ts | Makes user selection resilient to swept member cache. |
| README.md | Links new Discord intents documentation. |
| docs/INTENTS.md | Adds privileged intents documentation for developers. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+44
to
+50
| const guildCache = this.client.cache.emoteReactCache.get(message.guildId); | ||
|
|
||
| if (trigger.includes(" ")) { | ||
| this.client.cache.emoteReactCache | ||
| .get(message.guildId) | ||
| ?.get(ERCacheType.HAS_SPACE) | ||
| ?.delete(trigger); | ||
| guildCache?.get(ERCacheType.HAS_SPACE)?.delete(trigger); | ||
| } else { | ||
| this.client.cache.emoteReactCache | ||
| .get(message.guildId) | ||
| ?.get(ERCacheType.NO_SPACE) | ||
| ?.delete(trigger); | ||
| guildCache?.get(ERCacheType.NO_SPACE)?.delete(trigger); | ||
| } |
Comment on lines
+19
to
+20
| const results: R[] = new Array(items.length); | ||
| let nextIndex = 0; |
| if (key.includes(" ")) { | ||
| // Escape regex special characters to avoid accidental patterns | ||
| const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
| const regex = new RegExp(`\\b${escaped}\\b`, "gi"); |
|
|
||
| # Discord privileged intents | ||
|
|
||
| If the bot reaches the 10k users threshold, then applying for priviliged intents is necessary for the bot to conitnue to function. |
Comment on lines
+18
to
+21
| **DONT MENTION PREFIX BASED COMMANDS** | ||
| 1. ~~**Prefix-based command system (core functionality).** KaikiBot's primary interaction model is per-guild configurable prefix commands. Each server can set its own custom prefix, and users invoke commands like `+daily`, `+info`, or `+config` by typing them in chat. Detecting the prefix and parsing arguments (including multi-word arguments such as search phrases for `+urban` or message templates for welcome messages) requires reading message content.~~ | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimize memory usage in the bot by improving cache management and implementing regular sweeping. Additionally, include documentation for Discord privileged intents to assist developers in understanding their usage.