MCPcopy
hub / github.com/codebymitch/TitanBot / cleanupExpiredApplications

Function cleanupExpiredApplications

src/utils/database.js:1085–1129  ·  view source on GitHub ↗
(client, guildId)

Source from the content-addressed store, hash-verified

1083}
1084
1085export async function cleanupExpiredApplications(client, guildId) {
1086 try {
1087 if (!client.db || typeof client.db.list !== 'function') {
1088 return { removed: 0, scanned: 0 };
1089 }
1090
1091 const settings = await getApplicationSettings(client, guildId);
1092 const retentionDays = getApplicationRetentionDays(settings);
1093 const prefix = `guild:${guildId}:applications:`;
1094 let keys = await client.db.list(prefix);
1095
1096 if (!Array.isArray(keys)) {
1097 if (typeof keys === 'object' && keys !== null) {
1098 keys = Object.keys(keys).filter(key => key.startsWith(prefix));
1099 } else {
1100 return { removed: 0, scanned: 0 };
1101 }
1102 }
1103
1104 const applicationKeyPattern = new RegExp(`^guild:${guildId}:applications:[^:]+$`);
1105 const applicationKeys = keys.filter(key => applicationKeyPattern.test(key));
1106
1107 const now = Date.now();
1108 let removed = 0;
1109
1110 for (const key of applicationKeys) {
1111 const app = unwrapReplitData(await client.db.get(key, null));
1112 if (!app) {
1113 continue;
1114 }
1115
1116 if (isApplicationExpired(app, retentionDays, now)) {
1117 const deleted = await deleteApplication(client, guildId, app.id, app.userId);
1118 if (deleted) {
1119 removed += 1;
1120 }
1121 }
1122 }
1123
1124 return { removed, scanned: applicationKeys.length };
1125 } catch (error) {
1126 logger.error(`Error cleaning expired applications for guild ${guildId}:`, error);
1127 return { removed: 0, scanned: 0 };
1128 }
1129}
1130
1131export async function saveApplicationSettings(client, guildId, settings) {
1132 const key = getApplicationSettingsKey(guildId);

Callers 3

getApplicationFunction · 0.85
getUserApplicationsFunction · 0.85
getApplicationsFunction · 0.85

Calls 7

getApplicationSettingsFunction · 0.85
unwrapReplitDataFunction · 0.85
isApplicationExpiredFunction · 0.85
deleteApplicationFunction · 0.85
listMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected