Skip to content

Commit

Permalink
Refactor discord.js to update DISCORD_MESSAGE_EMBEDS handling and fix…
Browse files Browse the repository at this point in the history
… message embeds bug
  • Loading branch information
Adammatthiesen committed Nov 28, 2024
1 parent 24dd165 commit 001deb0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions scripts/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ if (!GITHUB_ACTION || !DISCORD_WEBHOOK) {
console.log("DISCORD_MESSAGE", DISCORD_MESSAGE);
console.log("DISCORD_MESSAGE_EMBEDS", DISCORD_MESSAGE_EMBEDS);

// Convert the array of strings to an array of objects
if (DISCORD_MESSAGE_EMBEDS) {
try {
DISCORD_MESSAGE_EMBEDS = JSON.parse(DISCORD_MESSAGE_EMBEDS);
} catch (error) {
console.error("Failed to parse DISCORD_MESSAGE_EMBEDS", error);
}
function parseDiscordEmbeds(jsonString) {
try {
// Fix common JSON formatting issues:
const fixedJsonString = jsonString
.replace(/,\s*}/g, '}') // Remove trailing commas in objects
.replace(/,\s*]/g, ']') // Remove trailing commas in arrays
.replace(/\\n/g, '') // Remove literal newline escape sequences
.replace(/'/g, '"'); // Replace single quotes with double quotes (for JSON)

// Parse the fixed JSON string
return JSON.parse(fixedJsonString);
} catch (error) {
throw new Error(`Failed to parse JSON: ${error.message}`);
}
}

function getBody() {
if (DISCORD_MESSAGE_EMBEDS) {
if (DISCORD_MESSAGE) {
return {
content: DISCORD_MESSAGE,
embeds: DISCORD_MESSAGE_EMBEDS,
embeds: parseDiscordEmbeds(DISCORD_MESSAGE_EMBEDS),
};
}
return {
Expand Down

0 comments on commit 001deb0

Please sign in to comment.