Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache max size and sweepers #255

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
TextChannel,
GatewayIntentBits,
ChannelType,
PermissionFlagsBits
PermissionFlagsBits,
Options
} from 'discord.js';
import config from './config';
import * as router from './commands/router';
Expand All @@ -18,19 +19,55 @@ import {
setupPatreonTrigger
} from './patreon-trigger';

const CACHED_ACTIVE_USER_IDS = new Set<string>(config.discord.cache.excludeUsers);
const CACHED_ACTIVE_GUILD_IDS = new Set<string>(config.discord.cache.excludeGuilds);

class Bot {
client: Client;

constructor() {
this.client = new Client({
shards: 'auto',
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessageTyping
],
shards: 'auto'
makeCache: Options.cacheWithLimits({
// Disable caching for these
ThreadManager: { maxSize: 0 },
MessageManager: { maxSize: 0 },
PresenceManager: { maxSize: 0 },
VoiceStateManager: { maxSize: 0 },
GuildInviteManager: { maxSize: 0 },
GuildStickerManager: { maxSize: 0 },
ThreadMemberManager: { maxSize: 0 },
// Keep some cached items
UserManager: {
maxSize: 1000,
keepOverLimit: user => CACHED_ACTIVE_USER_IDS.has(user.id)
},
GuildMemberManager: {
maxSize: 200,
keepOverLimit: member => CACHED_ACTIVE_USER_IDS.has(member.user.id)
},
GuildEmojiManager: {
maxSize: 1,
keepOverLimit: i => CACHED_ACTIVE_GUILD_IDS.has(i.guild.id)
}
}),
sweepers: {
guildMembers: {
interval: 60 * 60,
filter: () => member => !CACHED_ACTIVE_USER_IDS.has(member.user.id)
},
users: {
interval: 60 * 60,
filter: () => user => !CACHED_ACTIVE_USER_IDS.has(user.id)
}
}
});
}

Expand Down
17 changes: 17 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ export default {
modLogs: env.DISCORD_DEV_MODSLOGS_CHANNEL_ID || '830199626630955039',
patreonInfo: env.DISCORD_DEV_PATREON_INFO_CHANNEL_ID || '1173680059526152272',
flaggedPlayerReviews: env.DISCORD_DEV_FLAGGED_REVIEWS_CHANNEL_ID || '1086637095415722169'
},
cache: {
excludeUsers: [
'719720369241718837' // The bot itself
],
excludeGuilds: [
env.DISCORD_DEV_GUILD_ID ?? '',
'679454777708380161', // The WOM Discord server
'719722124301828138', // Psikoi's Emoji/Test server
'847787694213824512', // Rorro's Emoji/Test server
'1159473162342371458', // Rorro's Role server 1
'1159473499585400923', // Rorro's Role server 2
'1159473768859709450', // Rorro's Role server 3
'1159473857237897267', // Rorro's Role server 4
'1159473909708636161', // Rorro's Role server 5
'1159473951781683240' // Rorro's Role server 6
]
}
}
};