Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only run fixTwitterEmbeds if conditions are met #330

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's include the message id here so we can cross-correlate to the other logs.

)

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,
)
})
}
})
}
Loading