Skip to content

Commit 79268d6

Browse files
committed
create build_embed function to keep the code DRY and make suggested changes
1 parent 6d5f315 commit 79268d6

File tree

1 file changed

+45
-70
lines changed

1 file changed

+45
-70
lines changed

bot/exts/utilities/githubinfo.py

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,46 @@ async def refresh_repos(self) -> None:
317317
self.repos, _ = await self.fetch_data(REPOSITORY_ENDPOINT.format(org="python-discord"))
318318
log.info(f"Loaded {len(self.repos)} repos from Python Discord org into memory.")
319319

320+
def build_embed(self, repo_data: dict) -> discord.Embed:
321+
"""Create a clean discord embed to show repo data."""
322+
embed = discord.Embed(
323+
title=repo_data["name"],
324+
description=repo_data["description"] or "No description provided.",
325+
colour=discord.Colour.og_blurple(),
326+
url=repo_data["html_url"]
327+
)
328+
329+
try:
330+
parent = repo_data["parent"]
331+
embed.description += f"\n\nForked from [{parent['full_name']}]({parent['html_url']})"
332+
except KeyError:
333+
log.debug("Repository is not a fork.")
334+
335+
repo_owner = repo_data["owner"]
336+
embed.set_author(
337+
name=repo_owner["login"],
338+
url=repo_owner["html_url"],
339+
icon_url=repo_owner["avatar_url"]
340+
)
341+
342+
repo_created_at = datetime.strptime(
343+
repo_data["created_at"], "%Y-%m-%dT%H:%M:%SZ"
344+
).replace(tzinfo=UTC).strftime("%d/%m/%Y")
345+
last_pushed = datetime.strptime(
346+
repo_data["pushed_at"], "%Y-%m-%dT%H:%M:%SZ"
347+
).replace(tzinfo=UTC).strftime("%d/%m/%Y at %H:%M")
348+
349+
embed.set_footer(
350+
text=(
351+
f"{repo_data['forks_count']} ⑂ "
352+
f"• {repo_data['stargazers_count']} ⭐ "
353+
f"• Created At {repo_created_at} "
354+
f"• Last Commit {last_pushed}"
355+
)
356+
)
357+
return embed
358+
359+
320360
@github_group.command(name="repository", aliases=("repo",))
321361
async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
322362
"""
@@ -350,9 +390,9 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
350390

351391
# Case 2: Not stored or PyDis, fetch most-starred matching repo
352392
elif fetch_most_starred:
353-
repo_data, _ = await self.fetch_data(FETCH_MOST_STARRED_ENDPOINT.format(name=quote(repo_query)))
393+
repos, _ = await self.fetch_data(FETCH_MOST_STARRED_ENDPOINT.format(name=quote(repo_query)))
354394

355-
if not repo_data["items"]:
395+
if not repos["items"]:
356396
embed = discord.Embed(
357397
title=random.choice(NEGATIVE_REPLIES),
358398
description=f"No repositories found matching `{repo_query}`.",
@@ -361,36 +401,8 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
361401
await ctx.send(embed=embed)
362402
return
363403

364-
repo_item = repo_data["items"][0] # Top result
365-
embed = discord.Embed(
366-
title=repo_item["name"],
367-
description=repo_item["description"] or "No description provided.",
368-
colour=discord.Colour.og_blurple(),
369-
url=repo_item["html_url"]
370-
)
371-
372-
repo_owner = repo_item["owner"]
373-
embed.set_author(
374-
name=repo_owner["login"],
375-
url=repo_owner["html_url"],
376-
icon_url=repo_owner["avatar_url"]
377-
)
378-
379-
repo_created_at = datetime.strptime(
380-
repo_item["created_at"], "%Y-%m-%dT%H:%M:%SZ"
381-
).replace(tzinfo=UTC).strftime("%d/%m/%Y")
382-
last_pushed = datetime.strptime(
383-
repo_item["pushed_at"], "%Y-%m-%dT%H:%M:%SZ"
384-
).replace(tzinfo=UTC).strftime("%d/%m/%Y at %H:%M")
385-
386-
embed.set_footer(
387-
text=(
388-
f"{repo_item['forks_count']} ⑂ "
389-
f"• {repo_item['stargazers_count']} ⭐ "
390-
f"• Created At {repo_created_at} "
391-
f"• Last Commit {last_pushed}"
392-
)
393-
)
404+
repo_data = repos["items"][0] # Top result
405+
embed = self.build_embed(repo_data)
394406

395407
await ctx.send(embed=embed)
396408
return
@@ -408,44 +420,7 @@ async def github_repo_info(self, ctx: commands.Context, *repo: str) -> None:
408420
await ctx.send(embed=embed)
409421
return
410422

411-
# Embed creation for PyDis or regular GitHub repo
412-
embed = discord.Embed(
413-
title=repo_data["name"],
414-
description=repo_data["description"] or "No description provided.",
415-
colour=discord.Colour.og_blurple(),
416-
url=repo_data["html_url"]
417-
)
418-
419-
# Fork info
420-
try:
421-
parent = repo_data["parent"]
422-
embed.description += f"\n\nForked from [{parent['full_name']}]({parent['html_url']})"
423-
except KeyError:
424-
log.debug("Repository is not a fork.")
425-
426-
repo_owner = repo_data["owner"]
427-
embed.set_author(
428-
name=repo_owner["login"],
429-
url=repo_owner["html_url"],
430-
icon_url=repo_owner["avatar_url"]
431-
)
432-
433-
repo_created_at = datetime.strptime(
434-
repo_data["created_at"], "%Y-%m-%dT%H:%M:%SZ"
435-
).replace(tzinfo=UTC).strftime("%d/%m/%Y")
436-
last_pushed = datetime.strptime(
437-
repo_data["pushed_at"], "%Y-%m-%dT%H:%M:%SZ"
438-
).replace(tzinfo=UTC).strftime("%d/%m/%Y at %H:%M")
439-
440-
embed.set_footer(
441-
text=(
442-
f"{repo_data['forks_count']} ⑂ "
443-
f"• {repo_data['stargazers_count']} ⭐ "
444-
f"• Created At {repo_created_at} "
445-
f"• Last Commit {last_pushed}"
446-
)
447-
)
448-
423+
embed = self.build_embed(repo_data)
449424
await ctx.send(embed=embed)
450425

451426
async def setup(bot: Bot) -> None:

0 commit comments

Comments
 (0)