Skip to content

Commit

Permalink
Add twitter embed fixer
Browse files Browse the repository at this point in the history
When a new twitter.com/x.com link is seen, Valkyrie auto-replies with
the same link switched to fxtwitter.com (see
https://github.com/FixTweet/FxTwitter for the implementation). This
produces an actual Discord embed, as well as auto-expanding t.co links
and doing a couple of other useful things.

Those who wish to can also change the link to always redirect to nitter
instead of twitter (e.g. if you do not stay logged in or if twitter is
broken for you).
  • Loading branch information
Shadowfiend committed Jan 2, 2024
1 parent 45c4d6d commit 133a02c
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions discord-scripts/fix-twitter-embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { PartialMessage, Client, Message, EmbedBuilder } from "discord.js"

async function workingTwitterEmbeds(
message: Message<boolean> | PartialMessage,
) {
if (message.author === null || message.author === undefined) {
return
}

if (!message.author.bot) {
const content = message.content ?? ""
if (content.match(/(https:\/\/(x|twitter).com\/[a-zA-Z0-9%/+]+)/)) {
const allLinks = Array.from(
content.matchAll(/(https:\/\/(x|twitter).com\/[a-zA-Z0-9%/+]+)/g),
)
.map(([twitterLink]) =>
twitterLink.replace(/https:\/\/(x|twitter)/, "https://fxtwitter"),
)
.join(" , ")

await message.channel.send(allLinks)
}

const receivedEmbeds = message.embeds
if (
!receivedEmbeds ||
!receivedEmbeds.find(
(embed) => embed.url && embed.url.includes("github.com"),
)
) {
return
}

await message.suppressEmbeds(true)
const description = receivedEmbeds
.map((embed, i) => `(${i + 1}) [${embed.title}](${embed.url})`)
.join("\n")
const embed = new EmbedBuilder()
.setColor("#0099ff")
.setDescription(description)

message.channel.send({ embeds: [embed] })
}
}

// Follows up any message with 1+ Twitter links (including x.com) with a
// message that includes fxtwitter links, which correctly embed into Discord,
// expand t.co links, and have a couple of other nice features.
export default function fixTwitterEmbeds(discordClient: Client) {
discordClient.on("messageCreate", (message) => {
workingTwitterEmbeds(message)
})

discordClient.on("messageUpdate", (_, newMessage) => {
workingTwitterEmbeds(newMessage)
})
}

0 comments on commit 133a02c

Please sign in to comment.