Skip to content

Commit

Permalink
Refactor embeds with better logging
Browse files Browse the repository at this point in the history
This changes the `fixTwitterEmbeds` to run a main `processTwitterMessage` where we output the specific user, channel, timestamp and message id to `logger.info` while outputting the entire message to `logger.debug`
  • Loading branch information
zuuring committed Dec 3, 2024
1 parent 7823e98 commit dab0542
Showing 1 changed file with 35 additions and 18 deletions.
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}`,
)

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

0 comments on commit dab0542

Please sign in to comment.