Skip to content

Commit 9931fbb

Browse files
committed
update: Updated insta post notification logger to stop posting logs flooding console
1 parent eb942e0 commit 9931fbb

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/events/CommandEvents/instaNotificationEvent.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ const rateLimiter = {
2727
}
2828
};
2929

30+
const logRateLimiter = {
31+
lastLogs: {},
32+
33+
shouldLog: function(key, minutes = 30) {
34+
const now = Date.now();
35+
const lastLog = this.lastLogs[key] || 0;
36+
37+
if (now - lastLog > minutes * 60 * 1000) {
38+
this.lastLogs[key] = now;
39+
return true;
40+
}
41+
return false;
42+
}
43+
};
44+
3045
function delay(ms) {
3146
return new Promise(resolve => setTimeout(resolve, ms));
3247
}
@@ -51,28 +66,36 @@ async function getLatestPost(username) {
5166

5267
if (!userResponse.ok) {
5368
if (userResponse.status !== 429) {
54-
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: Instagram API returned ${userResponse.status} for ${username}${color.reset}`);
69+
if (logRateLimiter.shouldLog(`api_error_${username}`, 60)) {
70+
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: Instagram API returned ${userResponse.status} for ${username}${color.reset}`);
71+
}
5572
}
5673
return null;
5774
}
5875

5976
const contentType = userResponse.headers.get('content-type');
6077
if (!contentType || !contentType.includes('application/json')) {
61-
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: Invalid content type ${contentType} for ${username}${color.reset}`);
78+
if (logRateLimiter.shouldLog(`content_type_${username}`, 60)) {
79+
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: Invalid content type ${contentType} for ${username}${color.reset}`);
80+
}
6281
return null;
6382
}
6483

6584
const userData = await userResponse.json();
6685

6786
if (!userData || !userData.data || !userData.data.user) {
68-
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: No user data found for ${username}${color.reset}`);
87+
if (logRateLimiter.shouldLog(`no_user_data_${username}`, 60)) {
88+
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: No user data found for ${username}${color.reset}`);
89+
}
6990
return null;
7091
}
7192

7293
const user = userData.data.user;
7394

7495
if (!user.edge_owner_to_timeline_media?.edges?.length) {
75-
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: No posts found for ${username}${color.reset}`);
96+
if (logRateLimiter.shouldLog(`no_posts_${username}`, 60)) {
97+
console.error(`${color.yellow}[${getTimestamp()}] [INSTA_NOTIFICATION] Warning: No posts found for ${username}${color.reset}`);
98+
}
7699
return null;
77100
}
78101

@@ -97,10 +120,18 @@ async function getLatestPost(username) {
97120
module.exports = {
98121
name: Events.ClientReady,
99122
async execute(client) {
123+
let lastRoutineLog = 0;
124+
100125
const checkInstagramPosts = async () => {
101126
try {
102127
const allGuilds = await InstagramSchema.find();
103-
console.log(`${color.blue}[${getTimestamp()}] [INSTA_NOTIFICATION] Checking posts for ${allGuilds.reduce((total, guild) => total + guild.InstagramUsers.length, 0)} Instagram users${color.reset}`);
128+
const totalUsers = allGuilds.reduce((total, guild) => total + guild.InstagramUsers.length, 0);
129+
130+
const now = Date.now();
131+
if (now - lastRoutineLog > 6 * 60 * 60 * 1000) {
132+
console.log(`${color.blue}[${getTimestamp()}] [INSTA_NOTIFICATION] Checking posts for ${totalUsers} Instagram users${color.reset}`);
133+
lastRoutineLog = now;
134+
}
104135

105136
for (const guildData of allGuilds) {
106137
for (const username of guildData.InstagramUsers) {
@@ -135,7 +166,9 @@ module.exports = {
135166
}
136167
}
137168

138-
console.log(`${color.green}[${getTimestamp()}] [INSTA_NOTIFICATION] Completed Instagram post check${color.reset}`);
169+
if (now - lastRoutineLog < 10000) {
170+
console.log(`${color.green}[${getTimestamp()}] [INSTA_NOTIFICATION] Completed Instagram post check${color.reset}`);
171+
}
139172
} catch (error) {
140173
console.error(`${color.red}[${getTimestamp()}] [INSTA_NOTIFICATION] Error in post checking routine: ${color.reset}`, error);
141174
}

0 commit comments

Comments
 (0)