|
| 1 | +const axios = require("axios"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +// Load the config from the config.json file |
| 6 | +const configPath = path.join(__dirname, "../config.json"); |
| 7 | +const config = JSON.parse(fs.readFileSync(configPath, "utf-8")); |
| 8 | + |
| 9 | +// Check if Discord integration is enabled |
| 10 | +const discordConfig = config.features.discordIntegration; |
| 11 | +const webhookURL = discordConfig.webhookURL; |
| 12 | +const isDiscordEnabled = discordConfig.enableFeature; |
| 13 | + |
| 14 | +// Helper function to convert hex to decimal |
| 15 | +function hexToDecimal(hex) { |
| 16 | + // Remove the leading '#' if present |
| 17 | + hex = hex.replace("#", ""); |
| 18 | + return parseInt(hex, 16); |
| 19 | +} |
| 20 | + |
| 21 | +// Function to send a message to Discord |
| 22 | +async function sendDiscordNotification(message) { |
| 23 | + if (!isDiscordEnabled) { |
| 24 | + console.log("Discord integration is disabled. Skipping notification."); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + await axios.post(webhookURL, { |
| 30 | + content: message, // The message you want to send |
| 31 | + }); |
| 32 | + console.log("Notification sent to Discord!"); |
| 33 | + } catch (error) { |
| 34 | + console.error("Error sending Discord notification:", error); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Function to send an embed to Discord |
| 39 | +async function sendDiscordEmbed(title, description, hexColor = "#FF0000") { |
| 40 | + if (!isDiscordEnabled) { |
| 41 | + console.log( |
| 42 | + "Discord integration is disabled. Skipping embed notification." |
| 43 | + ); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const colorDecimal = hexToDecimal(hexColor); // Convert hex color to decimal |
| 48 | + |
| 49 | + try { |
| 50 | + await axios.post(webhookURL, { |
| 51 | + embeds: [ |
| 52 | + { |
| 53 | + title: title, |
| 54 | + description: description, |
| 55 | + color: colorDecimal, // Use the decimal color value |
| 56 | + footer: { |
| 57 | + text: "Built by www.kevintrinh.dev", // Add footer text |
| 58 | + }, |
| 59 | + timestamp: new Date().toISOString(), |
| 60 | + }, |
| 61 | + ], |
| 62 | + }); |
| 63 | + console.log("Embed notification sent to Discord!"); |
| 64 | + } catch (error) { |
| 65 | + console.error("Error sending Discord embed notification:", error); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Function to send an embed for a followed user object |
| 70 | +async function sendFollowedUserDiscordEmbed(user) { |
| 71 | + if (!isDiscordEnabled) { |
| 72 | + console.log( |
| 73 | + "Discord integration is disabled. Skipping user embed notification." |
| 74 | + ); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const colorDecimal = hexToDecimal("#55FF55"); // Green color in decimal |
| 79 | + |
| 80 | + try { |
| 81 | + await axios.post(webhookURL, { |
| 82 | + embeds: [ |
| 83 | + { |
| 84 | + title: `Successfully followed ${user.login} (${user.id})`, |
| 85 | + thumbnail: { |
| 86 | + url: user.avatar_url, // Set the user's avatar as the embed thumbnail |
| 87 | + }, |
| 88 | + fields: [ |
| 89 | + { |
| 90 | + name: "Profile URL", |
| 91 | + value: `[${user.login}](${user.html_url})`, // Embed the link to the user's profile |
| 92 | + inline: true, // Keep fields on the same line |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "User ID", |
| 96 | + value: user.id.toString(), // Display user ID |
| 97 | + inline: true, // Keep fields on the same line |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "Followed At", |
| 101 | + value: new Date().toLocaleString(), // Display current time and date |
| 102 | + inline: true, // Keep fields on the same line |
| 103 | + }, |
| 104 | + ], |
| 105 | + color: colorDecimal, // Use the green color |
| 106 | + footer: { |
| 107 | + text: "Built by www.kevintrinh.dev", // Add footer text |
| 108 | + }, |
| 109 | + timestamp: new Date().toISOString(), |
| 110 | + }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + //console.log(`Embed notification sent to Discord for user ${user.login}!`); |
| 114 | + } catch (error) { |
| 115 | + console.error("Error sending Discord embed notification:", error); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// Function to send an embed for an unfollowed user object |
| 120 | +async function sendUnfollowedUserDiscordEmbed(user) { |
| 121 | + if (!isDiscordEnabled) { |
| 122 | + console.log( |
| 123 | + "Discord integration is disabled. Skipping unfollowed user embed notification." |
| 124 | + ); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const colorDecimal = hexToDecimal("#FF5555"); // Custom color in decimal |
| 129 | + |
| 130 | + try { |
| 131 | + await axios.post(webhookURL, { |
| 132 | + embeds: [ |
| 133 | + { |
| 134 | + title: `Successfully unfollowed ${user.login} (${user.id})`, |
| 135 | + thumbnail: { |
| 136 | + url: user.avatar_url, // Set the user's avatar as the embed thumbnail |
| 137 | + }, |
| 138 | + fields: [ |
| 139 | + { |
| 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 |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "Followed On", |
| 146 | + value: new Date(user.followed_on).toLocaleString(), // Format the followed_on date |
| 147 | + inline: true, // Single line for followed date |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "Unfollowed On", |
| 151 | + value: new Date().toLocaleString(), // Display current time and date |
| 152 | + inline: true, // Single line for unfollowed date |
| 153 | + }, |
| 154 | + ], |
| 155 | + color: colorDecimal, // Use the custom color |
| 156 | + footer: { |
| 157 | + text: "Built by www.kevintrinh.dev", // Add footer text |
| 158 | + }, |
| 159 | + timestamp: new Date().toISOString(), |
| 160 | + }, |
| 161 | + ], |
| 162 | + }); |
| 163 | + //console.log(`Embed notification sent to Discord for unfollowed user ${user.login}!`); |
| 164 | + } catch (error) { |
| 165 | + console.error("Error sending Discord embed notification:", error); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +module.exports = { |
| 170 | + sendDiscordNotification, |
| 171 | + sendDiscordEmbed, |
| 172 | + sendFollowedUserDiscordEmbed, |
| 173 | + sendUnfollowedUserDiscordEmbed, |
| 174 | +}; |
0 commit comments