Add GitHub embed color (#1)
* add github embed colour sdfghjkl * obama's last name
This commit is contained in:
parent
bee5bfa2a1
commit
f4f56478d6
1 changed files with 40 additions and 22 deletions
|
@ -7,11 +7,10 @@ import re
|
|||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
# from discord import app_commands
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
from .. import bot
|
||||
from ..utils import github
|
||||
|
||||
GITHUB_REPO = re.compile(r"(?:\s|^)(?P<owner>[\w-]+)/(?P<repo>[\w-]+)(?:\s|$)", re.ASCII)
|
||||
|
@ -22,30 +21,49 @@ def match_repo(text: str) -> tuple[str, str] | None:
|
|||
return (match["owner"], match["repo"])
|
||||
|
||||
|
||||
def github_repo_embed(repo: dict[str, Any]) -> discord.Embed:
|
||||
description = repo["description"]
|
||||
if homepage := repo["homepage"]:
|
||||
description += f"\n\n{homepage}"
|
||||
stargazers = repo["stargazers_count"]
|
||||
forks = repo["forks_count"]
|
||||
watchers = repo["subscribers_count"]
|
||||
footer = f"⭐ {stargazers} 🍴 {forks} 👀 {watchers}"
|
||||
|
||||
return (
|
||||
discord.Embed(title=repo["full_name"], url=repo["html_url"], description=description)
|
||||
.set_thumbnail(url=repo["owner"]["avatar_url"])
|
||||
.set_footer(text=footer)
|
||||
)
|
||||
|
||||
|
||||
class GitHub(commands.Cog):
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, bot: bot.Wulkabot) -> None:
|
||||
super().__init__()
|
||||
self.bot = bot
|
||||
self.github = github.GitHub(aiohttp.ClientSession(base_url="https://api.github.com"))
|
||||
|
||||
async def cog_load(self) -> None:
|
||||
result = await self.bot.http_client.get(
|
||||
"https://raw.githubusercontent.com/ozh/github-colors/master/colors.json"
|
||||
)
|
||||
self.github_colors: dict[str, dict[str, str | None]] = await result.json(content_type=None)
|
||||
|
||||
async def cog_unload(self) -> None:
|
||||
await self.github.close()
|
||||
|
||||
def github_repo_embed(self, repo: dict[str, Any]) -> discord.Embed:
|
||||
description = repo["description"]
|
||||
if homepage := repo["homepage"]:
|
||||
description += f"\n\n{homepage}"
|
||||
stargazers = repo["stargazers_count"]
|
||||
forks = repo["forks_count"]
|
||||
watchers = repo["subscribers_count"]
|
||||
footer = f"⭐ {stargazers} 🍴 {forks} 👀 {watchers}"
|
||||
|
||||
return (
|
||||
discord.Embed(
|
||||
title=repo["full_name"],
|
||||
url=repo["html_url"],
|
||||
description=description,
|
||||
color=self.get_github_color(repo["language"]),
|
||||
)
|
||||
.set_thumbnail(url=repo["owner"]["avatar_url"])
|
||||
.set_footer(text=footer)
|
||||
)
|
||||
|
||||
def get_github_color(self, language: str | None) -> int | None:
|
||||
if language is None:
|
||||
return None
|
||||
color = self.github_colors[language]["color"]
|
||||
if color is None:
|
||||
return 0xF5AAB9
|
||||
return int(color.removeprefix("#"), 16)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_message(self, message: discord.Message) -> None:
|
||||
if message.author.bot:
|
||||
|
@ -53,8 +71,8 @@ class GitHub(commands.Cog):
|
|||
|
||||
if match := match_repo(message.content):
|
||||
if repo := await self.github.fetch_repo(*match):
|
||||
await message.reply(embed=github_repo_embed(repo))
|
||||
await message.reply(embed=self.github_repo_embed(repo))
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(GitHub())
|
||||
async def setup(bot: bot.Wulkabot):
|
||||
await bot.add_cog(GitHub(bot))
|
||||
|
|
Loading…
Reference in a new issue