Skip to content

Enhance memory usage - #61

Open
CataDev0 wants to merge 3 commits into
masterfrom
Enhanced-Memory-Usage
Open

Enhance memory usage#61
CataDev0 wants to merge 3 commits into
masterfrom
Enhanced-Memory-Usage

Conversation

@CataDev0

Copy link
Copy Markdown
Owner

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.

Copilot AI lite review requested due to automatic review settings August 13, 2026 21:48
@CataDev0 CataDev0 added enhancement New feature or request help wanted Extra attention is needed labels Aug 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread src/lib/KaikiUtil.ts
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");
Comment thread docs/INTENTS.md

# 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 thread docs/INTENTS.md
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.~~


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants