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
|
from typing import Any
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
# from discord import app_commands
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
|
from .. import bot
|
||||||
from ..utils import github
|
from ..utils import github
|
||||||
|
|
||||||
GITHUB_REPO = re.compile(r"(?:\s|^)(?P<owner>[\w-]+)/(?P<repo>[\w-]+)(?:\s|$)", re.ASCII)
|
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"])
|
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"]
|
description = repo["description"]
|
||||||
if homepage := repo["homepage"]:
|
if homepage := repo["homepage"]:
|
||||||
description += f"\n\n{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}"
|
footer = f"⭐ {stargazers} 🍴 {forks} 👀 {watchers}"
|
||||||
|
|
||||||
return (
|
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_thumbnail(url=repo["owner"]["avatar_url"])
|
||||||
.set_footer(text=footer)
|
.set_footer(text=footer)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_github_color(self, language: str | None) -> int | None:
|
||||||
class GitHub(commands.Cog):
|
if language is None:
|
||||||
def __init__(self) -> None:
|
return None
|
||||||
super().__init__()
|
color = self.github_colors[language]["color"]
|
||||||
self.github = github.GitHub(aiohttp.ClientSession(base_url="https://api.github.com"))
|
if color is None:
|
||||||
|
return 0xF5AAB9
|
||||||
async def cog_unload(self) -> None:
|
return int(color.removeprefix("#"), 16)
|
||||||
await self.github.close()
|
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_message(self, message: discord.Message) -> None:
|
async def on_message(self, message: discord.Message) -> None:
|
||||||
|
@ -53,8 +71,8 @@ class GitHub(commands.Cog):
|
||||||
|
|
||||||
if match := match_repo(message.content):
|
if match := match_repo(message.content):
|
||||||
if repo := await self.github.fetch_repo(*match):
|
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):
|
async def setup(bot: bot.Wulkabot):
|
||||||
await bot.add_cog(GitHub())
|
await bot.add_cog(GitHub(bot))
|
||||||
|
|
Loading…
Reference in a new issue