Skip to content

Commit

Permalink
Add an additional embed for quoted posts
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanC committed May 6, 2024
1 parent e57c716 commit 0f0738a
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions bluebird.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ def WatchPosts(
details: dict = X.GetPost(username, post["postId"])

if details:
Bluebird.NotifyPost(username, details)
embeds: list[DiscordEmbed] = Bluebird.BuildEmbed(username, details)

if quote := details.get("quote"):
embeds.extend(
Bluebird.BuildEmbed(username, quote, isQuote=True)
)

Bluebird.Notify(embeds)

self.state[username] = latest["timestamp"]

Expand All @@ -168,11 +175,14 @@ def WatchPosts(
logger.debug(f"https://x.com/{username}/status/{latest["postId"]}")
logger.debug(f"[@{username}] {self.state}")

def NotifyPost(username: str, post: dict) -> None:
"""Send a Discord Embed object for the specified X post."""

if not (webhook := environ.get("DISCORD_WEBHOOK_URL")):
return
def BuildEmbed(
username: str,
post: dict,
isReply: bool = False,
isQuote: bool = False,
isRepost: bool = False,
) -> list[DiscordEmbed]:
"""Build a Discord embed object for the provided X post."""

embeds: list[DiscordEmbed] = []

Expand All @@ -183,13 +193,21 @@ def NotifyPost(username: str, post: dict) -> None:
f"https://x.com/{post["author"]["screen_name"]}/status/{post["id"]}"
)

if (isReply) or (post["replying_to"]):
primary.set_title("Reply on X")
elif isQuote:
primary.set_title("Quote on X")
elif (isRepost) or (post["text"] and post["text"].startswith("RT @")):
primary.set_title("Repost on X")
else:
primary.set_title("Post on X")

primary.set_color("1D9BF0")
primary.set_author(
f"{post["author"]["name"]} (@{post["author"]["screen_name"]})",
url=f"https://x.com/{post["author"]["screen_name"]}",
icon_url=post["author"]["avatar_url"],
)
primary.set_title("Post on X")
primary.set_url(postUrl)
primary.set_footer(post["source"], icon_url="https://i.imgur.com/hZbC8my.png")
primary.set_timestamp(post["created_timestamp"])
Expand Down Expand Up @@ -240,6 +258,14 @@ def NotifyPost(username: str, post: dict) -> None:
embeds.append(primary)
embeds.extend(extras)

return embeds

def Notify(embeds: list[DiscordEmbed]) -> None:
"""Send a Discord Embed object for the specified X post."""

if not (webhook := environ.get("DISCORD_WEBHOOK_URL")):
return

DiscordWebhook(url=webhook, embeds=embeds, rate_limit_retry=True).execute()


Expand Down

0 comments on commit 0f0738a

Please sign in to comment.