Skip to content

Commit

Permalink
Only run fixTwitterEmbeds if conditions met
Browse files Browse the repository at this point in the history
### Notes
This resolves an issue where Valkyrie was posting `INFO fixTwitterEmbeds` for every message whether it matches the regex pattern or not. Now we only run the twitter embed if it matches.
  • Loading branch information
zuuring committed Nov 26, 2024
1 parent 024233a commit 3e69d25
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions discord-scripts/fix-twitter-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ async function workingTwitterEmbeds(
(url) => !existingUrls.includes(url),
)

if (latestUrls.length === 0) {
return
}

logger.info(`workingTwitterEmbeds: extracted [${latestUrls.length}] URLs`)

if (latestUrls.length > 0) {
try {
// Kill default embeds in favor of ours <_<
await message.suppressEmbeds()

await message.channel.send(latestUrls.join(", "))
} catch (err) {
logger.error(`Error suppressing embeds or sending new links: ${err}`)
}
}
}
Expand All @@ -62,27 +67,37 @@ async function workingTwitterEmbeds(
//
// See https://github.com/FixTweet/FxTwitter for more.
export default function fixTwitterEmbeds(discordClient: Client, robot: Robot) {
// Process only messages that match the Twitter URL pattern
discordClient.on("messageCreate", (message) => {
robot.logger.info(
`fixTwitterEmbeds: processing new message ${message.content}`,
)

workingTwitterEmbeds(message, robot.logger).catch((err) => {
robot.logger.error(
`fixTwitterEmbeds: failed to process new message ${message.content}: ${err}`,
if (message.content?.match(twitterUrlRegExp)) {
robot.logger.info(
`fixTwitterEmbeds: processing new message ${message.content}`,
)
})

workingTwitterEmbeds(message, robot.logger).catch((err) => {
robot.logger.error(
`fixTwitterEmbeds: failed to process new message ${message.content}: ${err}`,
)
})
}
})

discordClient.on("messageUpdate", (oldMessage, newMessage) => {
robot.logger.info(
`fixTwitterEmbeds: processing updated message ${newMessage.content}, ${oldMessage.content}`,
)
if (
newMessage.content?.match(twitterUrlRegExp) ||
oldMessage?.content?.match(twitterUrlRegExp)
) {
robot.logger.info(
`fixTwitterEmbeds: processing updated message ${newMessage.content}`,
)

workingTwitterEmbeds(newMessage, robot.logger, oldMessage).catch((err) => {
robot.logger.error(
`fixTwitterEmbeds: failed to process new message ${newMessage.content}: ${err}`,
workingTwitterEmbeds(newMessage, robot.logger, oldMessage).catch(
(err) => {
robot.logger.error(
`fixTwitterEmbeds: failed to process updated message ${newMessage.content}: ${err}`,
)
},
)
})
}
})
}

0 comments on commit 3e69d25

Please sign in to comment.