Skip to content

Commit

Permalink
[Destiny] Fix new tweets lookups which require a new format for new a…
Browse files Browse the repository at this point in the history
…ccounts.

- Add support for Destiny2Team and DestinyTheGame twitter account lookup and include them in the automatic updates.
  • Loading branch information
TrustyJAID committed Mar 27, 2024
1 parent 8342fb8 commit 61b4e2b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
5 changes: 3 additions & 2 deletions destiny/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .converter import (
STRING_VAR_RE,
BungieTweet,
BungieXAccount,
DestinyActivityModeGroup,
DestinyActivityModeType,
DestinyComponents,
Expand Down Expand Up @@ -166,8 +167,8 @@ async def request_url(
log.error("Could not connect to the API: %s", resp.status)
raise Destiny2APIError

async def bungie_help(self) -> List[BungieTweet]:
url = "https://bungiehelp.org/data.json"
async def bungie_tweets(self, account: BungieXAccount) -> List[BungieTweet]:
url = f"https://bungiehelp.org/data/{account.path}"
async with self.session.get(url) as resp:
data = await resp.json()
return [BungieTweet(**i) for i in data]
Expand Down
15 changes: 15 additions & 0 deletions destiny/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ class BungieTweet:
edits_remaining: Optional[str]
unix: float
url: str
media: List[dict]

@property
def time(self) -> datetime:
return datetime.fromtimestamp(self.unix, tz=timezone.utc)


class BungieXAccount(Enum):
BungieHelp = "BungieHelp"
DestinyTheGame = "DestinyTheGame"
Destiny2Team = "Destiny2Team"

@property
def path(self):
return f"{self.value}.json"


@dataclass
Expand Down
38 changes: 26 additions & 12 deletions destiny/destiny.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from .api import DestinyAPI, MyTyping
from .converter import (
BungieXAccount,
DestinyActivity,
DestinyCharacter,
DestinyClassType,
Expand Down Expand Up @@ -174,16 +175,19 @@ async def on_oauth_receive(self, user_id: int, payload: dict):

@tasks.loop(seconds=300)
async def tweet_checker(self):
try:
tweets = await self.bungie_help()
except Exception:
log.exception("Error Checking bungiehelp.org")
return
if len(tweets) < 1:
all_tweets = []
for account in BungieXAccount:
try:
all_tweets.extend(await self.bungie_tweets(account))
except Exception:
log.exception("Error Checking bungiehelp.org")
continue
all_tweets.sort(key=lambda x: x.time)
if len(all_tweets) < 1:
return

guilds = await self.config.all_guilds()
article_keys = [a.id for a in tweets]
article_keys = [a.id for a in all_tweets]
for guild_id, data in guilds.items():
guild = self.bot.get_guild(guild_id)
if guild is None:
Expand All @@ -193,7 +197,7 @@ async def tweet_checker(self):
continue
if not channel.permissions_for(guild.me).send_messages:
continue
for tweet in tweets:
for tweet in all_tweets:
if tweet.id in data["posted_tweets"]:
continue
await channel.send(content=tweet.url)
Expand Down Expand Up @@ -361,7 +365,9 @@ async def destiny_set_tweets(
)
return
try:
tweets = await self.bungie_help()
tweets = []
for account in BungieXAccount:
tweets.extend(await self.bungie_tweets(account))
except Destiny2APIError:
await ctx.send(
_("There was an error getting the current news posts. Please try again later.")
Expand Down Expand Up @@ -879,16 +885,24 @@ async def destiny_news(self, ctx: commands.Context) -> None:

@destiny.command(name="tweets")
@commands.bot_has_permissions(embed_links=True)
async def destiny_tweets(self, ctx: commands.Context) -> None:
async def destiny_tweets(
self, ctx: commands.Context, account: Optional[BungieXAccount] = None
) -> None:
"""
Get the latest news articles from Bungie.net
"""
async with MyTyping(ctx, ephemeral=False):
try:
tweets = await self.bungie_help()
if account is None:
all_tweets = []
for account in BungieXAccount:
all_tweets.extend(await self.bungie_tweets(account))
all_tweets.sort(key=lambda x: x.time, reverse=True)
else:
all_tweets = await self.bungie_tweets(account)
except Destiny2APIError as e:
return await self.send_error_msg(ctx, e)
source = BungieTweetsSource(tweets)
source = BungieTweetsSource(all_tweets)
await BaseMenu(source=source, cog=self).start(ctx=ctx)

async def get_seal_icon(self, record: dict) -> str:
Expand Down

0 comments on commit 61b4e2b

Please sign in to comment.