Skip to content

Commit

Permalink
Only run fixTwitterEmbeds if conditions are met (#330)
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
Shadowfiend authored Dec 4, 2024
2 parents 640b502 + dab0542 commit b04ac65
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,7 @@ can then run hubot with the adapter.
Where `<adapter>` is the name of your adapter without the `hubot-` prefix.

[hubot-adapters]: https://github.com/github/hubot/blob/master/docs/adapters.md

## Debugging

If you'd like to test certain aspects of Valkyrie with a debugger, use `robot.logger.debug` and in your environment variables the flag `export HUBOT_LOG_LEVEL="debug"` which will enable those messages to appear in terminal output.
65 changes: 51 additions & 14 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,59 @@ async function workingTwitterEmbeds(
//
// See https://github.com/FixTweet/FxTwitter for more.
export default function fixTwitterEmbeds(discordClient: Client, robot: Robot) {
const formatMessageDetails = (message: Message) => {
const user = message.author?.tag || "Unknown User"
const channel = message.channel || "Unknown Channel"
const timestamp = message.createdAt.toISOString()
const messageId = message.id
return `User: ${user}, Channel: ${channel}, Timestamp: ${timestamp}, Message ID: ${messageId}`
}

// Process only messages that match the Twitter URL pattern
const processTwitterMessage = async (
message: Message,
logger: typeof robot.logger,
oldMessage?: Message,
) => {
const messageDetails = formatMessageDetails(message)

logger.info(
`fixTwitterEmbeds: processing message details ${messageDetails}`,
)

try {
await workingTwitterEmbeds(message, logger, oldMessage)
} catch (err) {
logger.error(
`fixTwitterEmbeds: failed to process message ${messageDetails}: ${err}`,
)
}
}

discordClient.on("messageCreate", (message) => {
robot.logger.info(
robot.logger.debug(
`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)) {
processTwitterMessage(message, robot.logger)
}
})

discordClient.on("messageUpdate", (oldMessage, newMessage) => {
robot.logger.info(
`fixTwitterEmbeds: processing updated message ${newMessage.content}, ${oldMessage.content}`,
robot.logger.debug(
`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}`,
if (
newMessage.content?.match(twitterUrlRegExp) ||
oldMessage?.content?.match(twitterUrlRegExp)
) {
processTwitterMessage(
newMessage as Message,
robot.logger,
oldMessage as Message,
)
})
}
})
}

0 comments on commit b04ac65

Please sign in to comment.