Skip to content

Commit

Permalink
[Destiny] Adds support for querying BlueSky for Bungie news (#345)
Browse files Browse the repository at this point in the history
* Adds support for querying BlueSky for Bungie news

* Fix Codestyle
  • Loading branch information
T14D3 authored Mar 12, 2025
1 parent be6df89 commit b63aa42
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
13 changes: 13 additions & 0 deletions destiny/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
BASE_URL = URL("https://www.bungie.net")
BASE_HEADERS = {"User-Agent": "Red-TrustyCogs-DestinyCog"}

BSKY_URL = "https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed"

COMPONENTS = DestinyComponents(
DestinyComponentType.profiles,
DestinyComponentType.profile_inventories,
Expand Down Expand Up @@ -235,6 +237,17 @@ async def bungie_tweets(self, account: BungieXAccount) -> List[BungieTweet]:
data = await resp.json()
return [BungieTweet(**i) for i in data]

async def get_bsky_posts(self, profile: str) -> list:
posts = []
url = f"{BSKY_URL}?actor={profile}&filter=posts_no_replies"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.json()
if data["feed"]:
for post in data["feed"]:
posts.append(post)
return posts

async def post_url(
self,
url: URL,
Expand Down
34 changes: 34 additions & 0 deletions destiny/destiny.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,40 @@ async def destiny_tweets(
source = BungieTweetsSource(all_tweets)
await BaseMenu(source=source, cog=self).start(ctx=ctx)

@destiny.command(name="bsky")
async def latest_bungie_help_post(self, ctx: commands.Context):
"""Get the latest posts from Bungie Help on BlueSky"""

async with ctx.typing(ephemeral=False):
entries = await self.api.get_bsky_posts("bungiehelp.bungie.net")

embeds = []
for entry in entries:
embed = discord.Embed()
embed.title = entry["post"]["author"]["displayName"]
embed.description = entry["post"]["record"]["text"]

embed.set_footer(
text=datetime.datetime.fromisoformat(
entry["post"]["record"]["createdAt"]
).strftime("%Y-%m-%d %H:%M:%S")
)

embed.set_author(
name=entry["post"]["author"]["displayName"],
icon_url=entry["post"]["author"]["avatar"],
)

embeds.append(embed)
await BaseMenu(
source=BasePages(
pages=embeds,
use_author=True,
footer_pagination=False,
),
cog=self,
).start(ctx=ctx)

async def get_seal_icon(self, record: dict) -> Optional[str]:
if record["parentNodeHashes"]:
node_defs = await self.api.get_definition(
Expand Down
6 changes: 4 additions & 2 deletions destiny/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ async def start(self):


class BasePages(menus.ListPageSource):
def __init__(self, pages: list, use_author: bool = False):
def __init__(self, pages: list, use_author: bool = False, footer_pagination: bool = True):
super().__init__(pages, per_page=1)
self.pages = pages
self.select_options = []
self.footer_pagination = footer_pagination
for count, page in enumerate(pages):
self.select_options.append(
discord.SelectOption(
Expand All @@ -145,7 +146,8 @@ def is_paginating(self):
return True

async def format_page(self, menu: menus.MenuPages, page):
page.set_footer(text=f"Page {menu.current_page + 1}/{self.get_max_pages()}")
if self.footer_pagination:
page.set_footer(text=f"Page {menu.current_page + 1}/{self.get_max_pages()}")
return page


Expand Down

0 comments on commit b63aa42

Please sign in to comment.