Add GitHub embed color (#1)

* add github embed colour

sdfghjkl

* obama's last name
This commit is contained in:
ribxne 2022-03-30 22:16:11 +02:00 committed by GitHub
parent bee5bfa2a1
commit f4f56478d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,7 +21,22 @@ def match_repo(text: str) -> tuple[str, str] | None:
return (match["owner"], match["repo"])
def github_repo_embed(repo: dict[str, Any]) -> discord.Embed:
class GitHub(commands.Cog):
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}"
@ -32,19 +46,23 @@ def github_repo_embed(repo: dict[str, Any]) -> discord.Embed:
footer = f"{stargazers} 🍴 {forks} 👀 {watchers}"
return (
discord.Embed(title=repo["full_name"], url=repo["html_url"], description=description)
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)
)
class GitHub(commands.Cog):
def __init__(self) -> None:
super().__init__()
self.github = github.GitHub(aiohttp.ClientSession(base_url="https://api.github.com"))
async def cog_unload(self) -> None:
await self.github.close()
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:
@ -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))