Skip to content

Commit 74f4bd1

Browse files
updated github webhooks and unfollower logic
1 parent e631b0c commit 74f4bd1

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

src/discordWebhook.js

+58-4
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,78 @@ async function sendUnfollowedUserDiscordEmbed(user) {
132132
embeds: [
133133
{
134134
title: `Successfully unfollowed ${user.login} (${user.id})`,
135+
url: user.html_url,
136+
description: `Reason: ${user.unfollow_reason}`,
135137
thumbnail: {
136138
url: user.avatar_url, // Set the user's avatar as the embed thumbnail
137139
},
138140
fields: [
139141
{
140-
name: "Profile URL",
141-
value: `[${user.login}](${user.html_url})`, // Embed the link to the user's profile
142-
inline: true, // Single line for link
142+
name: "Followed On",
143+
value: new Date(user.followed_on).toLocaleString(), // Format the followed_on date
144+
inline: true, // Single line for followed date
143145
},
146+
{
147+
name: "Unfollowed On",
148+
value: new Date().toLocaleString(), // Display current time and date
149+
inline: true, // Single line for unfollowed date
150+
},
151+
{
152+
name: "Reason",
153+
value: user.unfollow_reason, // Display current time and date
154+
inline: true, // Single line for unfollowed date
155+
},
156+
],
157+
color: colorDecimal, // Use the custom color
158+
footer: {
159+
text: "Built by www.kevintrinh.dev", // Add footer text
160+
},
161+
timestamp: new Date().toISOString(),
162+
},
163+
],
164+
});
165+
//console.log(`Embed notification sent to Discord for unfollowed user ${user.login}!`);
166+
} catch (error) {
167+
console.error("Error sending Discord embed notification:", error);
168+
}
169+
}
170+
171+
// Function to send an embed for an unfollowed user object
172+
async function sendMovedUserToUnfollowQueueDiscordEmbed(user) {
173+
if (!isDiscordEnabled) {
174+
console.log(
175+
"Discord integration is disabled. Skipping unfollowed user embed notification."
176+
);
177+
return;
178+
}
179+
180+
const colorDecimal = hexToDecimal("#FFFF55"); // Custom color in decimal
181+
182+
try {
183+
await axios.post(webhookURL, {
184+
embeds: [
185+
{
186+
title: `Added user to unfollow queue: ${user.login} (${user.id})`,
187+
url: user.html_url,
188+
thumbnail: {
189+
url: user.avatar_url, // Set the user's avatar as the embed thumbnail
190+
},
191+
fields: [
144192
{
145193
name: "Followed On",
146194
value: new Date(user.followed_on).toLocaleString(), // Format the followed_on date
147195
inline: true, // Single line for followed date
148196
},
149197
{
150-
name: "Unfollowed On",
198+
name: "Queue Timestamp",
151199
value: new Date().toLocaleString(), // Display current time and date
152200
inline: true, // Single line for unfollowed date
153201
},
202+
{
203+
name: "Reason",
204+
value: user.unfollow_reason, // Display current time and date
205+
inline: true, // Single line for unfollowed date
206+
},
154207
],
155208
color: colorDecimal, // Use the custom color
156209
footer: {
@@ -171,4 +224,5 @@ module.exports = {
171224
sendDiscordEmbed,
172225
sendFollowedUserDiscordEmbed,
173226
sendUnfollowedUserDiscordEmbed,
227+
sendMovedUserToUnfollowQueueDiscordEmbed,
174228
};

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ async function processQueue() {
116116

117117
// Move expired users to the unfollow queue if none to unfollow
118118
await moveExpiredUsersToUnfollowQueue();
119+
// check to see who has followed us back then put them in unfollow queue
120+
await moveFollowBacksToUnfollowQueue();
119121

120122
console.log("Waiting before the next cycle...\n\n\n");
121123
await waitRandomInterval(); // Wait for a random interval...

src/queues.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ async function moveExpiredUsersToUnfollowQueue() {
112112
// Add the user to the unfollow queue with a timestamp
113113
unfollowQueue[user.id] = {
114114
...user,
115-
added_to_queue_on: new Date().toISOString(), // Add a timestamp for when the user was added
115+
moved_to_unfollow_queue_on: new Date().toISOString(), // Add a timestamp for when the user was added
116+
unfollow_reason: "Given time for user to follow back has expired",
116117
};
117118

119+
// log it in disc if webhook feature enabled
120+
await sendMovedUserToUnfollowQueueDiscordEmbed(user);
121+
118122
// Remove the user from pending_follow_back.json
119123
delete pendingFollowBack[userId];
120124
}

src/unfollowUser.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const currentFollowersPath = path.join(
1313
);
1414
const unfollowQueuePath = path.join(__dirname, "../data/unfollow_queue.json");
1515

16-
const { sendUnfollowedUserDiscordEmbed } = require("./discordWebhook");
16+
const {
17+
sendUnfollowedUserDiscordEmbed,
18+
sendMovedUserToUnfollowQueueDiscordEmbed,
19+
} = require("./discordWebhook");
1720

1821
// Path to the JSON file storing past followed and unfollowed users
1922
const pastFollowsUnfollowsPath = path.join(
@@ -122,7 +125,6 @@ async function removeFromUnfollowQueue(userId) {
122125
}
123126
}
124127

125-
// Function to move users from pending follow-back to unfollow queue if they followed back
126128
async function moveFollowBacksToUnfollowQueue() {
127129
try {
128130
// Load pending follow-back list
@@ -157,11 +159,20 @@ async function moveFollowBacksToUnfollowQueue() {
157159
for (const userID in pendingFollowBack) {
158160
if (currentFollowerIds.has(parseInt(userID))) {
159161
// If the user is following back, move them to the unfollow queue
160-
unfollowQueue[userID] = {
162+
const userObject = {
161163
...pendingFollowBack[userID],
162-
movedToUnfollowAt: new Date().toISOString(),
164+
moved_to_unfollow_queue_on: new Date().toISOString(),
165+
unfollow_reason: "User followed back",
163166
};
164167

168+
// Call the logThisUser function with userObject
169+
// logThisUser(userObject);
170+
await sendMovedUserToUnfollowQueueDiscordEmbed(userObject);
171+
//console.log("test");
172+
173+
// Add the user to the unfollow queue
174+
unfollowQueue[userID] = userObject;
175+
165176
// Remove the user from the pending follow-back list
166177
delete pendingFollowBack[userID];
167178
}

0 commit comments

Comments
 (0)