Skip to content

Commit f0b1017

Browse files
Merge pull request #3 from mercurialworld/embed-colours
add pretty embed colours to user and repo embeds
2 parents d863400 + 0421ab7 commit f0b1017

File tree

6 files changed

+8904
-5
lines changed

6 files changed

+8904
-5
lines changed

bot/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ dependencies = [
1818
"pfzy>=0.3.4",
1919
"more-itertools>=10.5.0",
2020
"fluent-runtime>=0.4.0",
21+
"pyyaml>=6.0.2",
22+
"pylette>=4.0.0",
2123
]
2224

2325
[tool.rye]

bot/src/ghutils/cogs/app_commands/github.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from githubkit.exception import GitHubException, RequestFailed
1717
from githubkit.rest import Issue, IssuePropPullRequest, PullRequest, SimpleUser
1818
from more_itertools import consecutive_groups, ilen
19+
from Pylette import extract_colors # pyright: ignore[reportUnknownVariableType]
1920
from yarl import URL
2021

2122
from ghutils.common.__version__ import VERSION
@@ -149,6 +150,7 @@ async def repo(
149150
title=repo.full_name,
150151
description=repo.description,
151152
url=repo.html_url,
153+
color=self.bot.get_language_color(repo.language or ""),
152154
).set_image(
153155
url=image_url,
154156
)
@@ -181,10 +183,21 @@ async def user(
181183
)
182184
num_stars: int = result["user"]["starredRepositories"]["totalCount"]
183185

186+
# Pylette ints are actually int64s, thanks NumPy
187+
user_rgb = [
188+
int(val)
189+
for val in extract_colors(
190+
user.avatar_url, palette_size=1, sort_mode="frequency"
191+
)
192+
.colors[0]
193+
.rgb
194+
]
195+
184196
embed = (
185197
Embed(
186198
description=user.bio,
187199
url=user.html_url,
200+
color=Color.from_rgb(*user_rgb),
188201
)
189202
.set_thumbnail(url=user.avatar_url)
190203
.add_field(name="Repositories", value=user.public_repos, inline=True)

bot/src/ghutils/core/bot.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
from contextlib import asynccontextmanager
33
from dataclasses import dataclass
44
from datetime import datetime
5+
from typing import Any
56

6-
from discord import CustomActivity, Intents, Interaction
7+
import yaml
8+
from discord import Color, CustomActivity, Intents, Interaction
79
from discord.app_commands import AppCommandContext, AppInstallationType
810
from discord.ext import commands
911
from discord.ext.commands import Bot, Context, NoEntryPointError
@@ -13,6 +15,7 @@
1315
from ghutils import cogs
1416
from ghutils.common.__version__ import VERSION
1517
from ghutils.db.models import UserGitHubTokens
18+
from ghutils.resources import load_resource
1619
from ghutils.utils.imports import iter_modules
1720

1821
from .env import GHUtilsEnv
@@ -46,6 +49,7 @@ def __post_init__(self):
4649
)
4750
self.engine = create_engine(self.env.db_url)
4851
self.start_time = datetime.now()
52+
self.language_colors = self._load_language_colors()
4953

5054
@classmethod
5155
def of(cls, interaction: Interaction):
@@ -117,3 +121,21 @@ async def github_app(self, user_id: int | Interaction):
117121

118122
def _get_default_installation_app(self):
119123
return GitHub(self.env.gh.get_default_installation_auth())
124+
125+
def _load_language_colors(self) -> dict[str, Color]:
126+
logger.info("Loading repo language colors")
127+
langs: dict[str, dict[str, Any]] = yaml.load(
128+
load_resource("languages.yml"), Loader=yaml.CLoader
129+
)
130+
131+
return {
132+
language: Color.from_str(info["color"])
133+
for language, info in langs.items()
134+
if "color" in info
135+
}
136+
137+
# i'm not allowed to add the u to colour smh
138+
def get_language_color(self, language: str) -> Color:
139+
gh_default: Color = Color.from_str("#1B1F24")
140+
141+
return self.language_colors.get(language, gh_default)

0 commit comments

Comments
 (0)