diff --git a/README.md b/README.md index b279a421..fbcbc911 100644 --- a/README.md +++ b/README.md @@ -302,3 +302,7 @@ can then run hubot with the adapter. Where `` 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. diff --git a/discord-scripts/fix-twitter-embed.ts b/discord-scripts/fix-twitter-embed.ts index 438c07cf..25367189 100644 --- a/discord-scripts/fix-twitter-embed.ts +++ b/discord-scripts/fix-twitter-embed.ts @@ -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}`) } } } @@ -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, ) - }) + } }) }