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
Changes from 1 commit
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
53 changes: 35 additions & 18 deletions discord-scripts/fix-twitter-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,58 @@ 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.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.

)

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}`,
)
})
processTwitterMessage(message, robot.logger)
}
})

discordClient.on("messageUpdate", (oldMessage, newMessage) => {
robot.logger.debug(
`fixTwitterEmbeds: processing updated message ${newMessage.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 updated message ${newMessage.content}: ${err}`,
)
},
processTwitterMessage(
newMessage as Message,
robot.logger,
oldMessage as Message,
)
}
})
Expand Down
Loading