Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bot/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies = [
"pfzy>=0.3.4",
"more-itertools>=10.5.0",
"fluent-runtime>=0.4.0",
"pyyaml>=6.0.2",
"pylette>=4.0.0",
]

[tool.rye]
Expand Down
13 changes: 13 additions & 0 deletions bot/src/ghutils/cogs/app_commands/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from githubkit.exception import GitHubException, RequestFailed
from githubkit.rest import Issue, IssuePropPullRequest, PullRequest, SimpleUser
from more_itertools import consecutive_groups, ilen
from Pylette import extract_colors # pyright: ignore[reportUnknownVariableType]
from yarl import URL

from ghutils.common.__version__ import VERSION
Expand Down Expand Up @@ -149,6 +150,7 @@ async def repo(
title=repo.full_name,
description=repo.description,
url=repo.html_url,
color=self.bot.get_language_color(repo.language or ""),
).set_image(
url=image_url,
)
Expand Down Expand Up @@ -181,10 +183,21 @@ async def user(
)
num_stars: int = result["user"]["starredRepositories"]["totalCount"]

# Pylette ints are actually int64s, thanks NumPy
user_rgb = [
int(val)
for val in extract_colors(
user.avatar_url, palette_size=1, sort_mode="frequency"
)
.colors[0]
.rgb
]

embed = (
Embed(
description=user.bio,
url=user.html_url,
color=Color.from_rgb(*user_rgb),
)
.set_thumbnail(url=user.avatar_url)
.add_field(name="Repositories", value=user.public_repos, inline=True)
Expand Down
24 changes: 23 additions & 1 deletion bot/src/ghutils/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from contextlib import asynccontextmanager
from dataclasses import dataclass
from datetime import datetime
from typing import Any

from discord import CustomActivity, Intents, Interaction
import yaml
from discord import Color, CustomActivity, Intents, Interaction
from discord.app_commands import AppCommandContext, AppInstallationType
from discord.ext import commands
from discord.ext.commands import Bot, Context, NoEntryPointError
Expand All @@ -13,6 +15,7 @@
from ghutils import cogs
from ghutils.common.__version__ import VERSION
from ghutils.db.models import UserGitHubTokens
from ghutils.resources import load_resource
from ghutils.utils.imports import iter_modules

from .env import GHUtilsEnv
Expand Down Expand Up @@ -46,6 +49,7 @@ def __post_init__(self):
)
self.engine = create_engine(self.env.db_url)
self.start_time = datetime.now()
self.language_colors = self._load_language_colors()

@classmethod
def of(cls, interaction: Interaction):
Expand Down Expand Up @@ -117,3 +121,21 @@ async def github_app(self, user_id: int | Interaction):

def _get_default_installation_app(self):
return GitHub(self.env.gh.get_default_installation_auth())

def _load_language_colors(self) -> dict[str, Color]:
logger.info("Loading repo language colors")
langs: dict[str, dict[str, Any]] = yaml.load(
load_resource("languages.yml"), Loader=yaml.CLoader
)

return {
language: Color.from_str(info["color"])
for language, info in langs.items()
if "color" in info
}

# i'm not allowed to add the u to colour smh
def get_language_color(self, language: str) -> Color:
gh_default: Color = Color.from_str("#1B1F24")

return self.language_colors.get(language, gh_default)
Loading
Loading