Add auto GitHub repo embedding
This commit is contained in:
parent
7b43711da1
commit
dae062257a
4 changed files with 82 additions and 1 deletions
|
@ -23,8 +23,8 @@ class Wulkabot(commands.Bot):
|
|||
)
|
||||
|
||||
async def setup_hook(self) -> None:
|
||||
await self.load_extensions(cogs)
|
||||
self.http_client = aiohttp.ClientSession()
|
||||
await self.load_extensions(cogs)
|
||||
|
||||
async def on_connect(self) -> None:
|
||||
print(f"Connected as {self.user}")
|
||||
|
|
60
wulkabot/cogs/github.py
Normal file
60
wulkabot/cogs/github.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
"""
|
||||
Wulkabot
|
||||
Copyright (C) 2022-present Stanisław Jelnicki
|
||||
"""
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
# from discord import app_commands
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
from ..utils import github
|
||||
|
||||
GITHUB_REPO = re.compile(r"(?:\s|^)(?P<owner>[\w-]+)/(?P<repo>[\w-]+)(?:\s|$)", re.ASCII)
|
||||
|
||||
|
||||
def match_repo(text: str) -> tuple[str, str] | None:
|
||||
if match := GITHUB_REPO.search(text):
|
||||
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_count = repo["stargazers_count"]
|
||||
forks_count = repo["forks_count"]
|
||||
watchers = repo["watchers"]
|
||||
footer = f"⭐ {stargazers_count} 🍴 {forks_count} 👀 {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:
|
||||
super().__init__()
|
||||
self.github = github.GitHub(aiohttp.ClientSession(base_url="https://api.github.com"))
|
||||
|
||||
async def cog_unload(self) -> None:
|
||||
await self.github.close()
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_message(self, message: discord.Message) -> None:
|
||||
if message.author.bot:
|
||||
return
|
||||
|
||||
if match := match_repo(message.content):
|
||||
if repo := await self.github.fetch_repo(*match):
|
||||
await message.reply(embed=github_repo_embed(repo))
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(GitHub())
|
21
wulkabot/utils/github.py
Normal file
21
wulkabot/utils/github.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
"""
|
||||
Wulkabot
|
||||
Copyright (C) 2022-present Stanisław Jelnicki
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
|
||||
|
||||
class GitHub:
|
||||
def __init__(self, http_client: aiohttp.ClientSession) -> None:
|
||||
self._http = http_client
|
||||
|
||||
async def fetch_repo(self, owner: str, repo: str) -> dict[str, Any] | None:
|
||||
response = await self._http.get(f"/repos/{owner}/{repo}")
|
||||
if response.ok:
|
||||
return await response.json()
|
||||
|
||||
async def close(self):
|
||||
await self.close()
|
Loading…
Reference in a new issue