|
5 | 5 | from typing import Any |
6 | 6 |
|
7 | 7 | import yaml |
8 | | -from discord import Color, CustomActivity, Intents, Interaction |
| 8 | +from discord import Color, CustomActivity, Emoji, Intents, Interaction |
9 | 9 | from discord.app_commands import AppCommandContext, AppInstallationType |
10 | 10 | from discord.ext import commands |
11 | 11 | from discord.ext.commands import Bot, Context, NoEntryPointError |
|
21 | 21 | from .env import GHUtilsEnv |
22 | 22 | from .translator import GHUtilsTranslator |
23 | 23 | from .tree import GHUtilsCommandTree |
24 | | -from .types import LoginState |
| 24 | +from .types import CustomEmoji, LoginState |
25 | 25 |
|
26 | 26 | logger = logging.getLogger(__name__) |
27 | 27 |
|
@@ -50,6 +50,7 @@ def __post_init__(self): |
50 | 50 | self.engine = create_engine(self.env.db_url) |
51 | 51 | self.start_time = datetime.now() |
52 | 52 | self.language_colors = self._load_language_colors() |
| 53 | + self._custom_emoji = dict[CustomEmoji, Emoji]() |
53 | 54 |
|
54 | 55 | @classmethod |
55 | 56 | def of(cls, interaction: Interaction): |
@@ -139,3 +140,34 @@ def get_language_color(self, language: str) -> Color: |
139 | 140 | gh_default: Color = Color.from_str("#1B1F24") |
140 | 141 |
|
141 | 142 | return self.language_colors.get(language, gh_default) |
| 143 | + |
| 144 | + async def fetch_custom_emojis(self): |
| 145 | + logger.info("Fetching custom emojis") |
| 146 | + |
| 147 | + application_emojis = { |
| 148 | + emoji.name: emoji for emoji in await self.fetch_application_emojis() |
| 149 | + } |
| 150 | + |
| 151 | + self._custom_emoji.clear() |
| 152 | + for custom_emoji in CustomEmoji: |
| 153 | + if emoji := application_emojis.get(custom_emoji.name): |
| 154 | + self._custom_emoji[custom_emoji] = emoji |
| 155 | + else: |
| 156 | + logger.warning( |
| 157 | + f"Failed to find custom emoji (try running `sync emoji` command): {custom_emoji.name}" |
| 158 | + ) |
| 159 | + |
| 160 | + async def sync_custom_emojis(self): |
| 161 | + logger.info("Syncing/uploading custom emojis") |
| 162 | + self._custom_emoji.clear() |
| 163 | + for custom_emoji in CustomEmoji: |
| 164 | + self._custom_emoji[custom_emoji] = await self.create_application_emoji( |
| 165 | + name=custom_emoji.name, |
| 166 | + image=custom_emoji.load_image(), |
| 167 | + ) |
| 168 | + |
| 169 | + def get_custom_emoji(self, custom_emoji: CustomEmoji) -> Emoji: |
| 170 | + emoji = self._custom_emoji.get(custom_emoji) |
| 171 | + if emoji is None: |
| 172 | + raise ValueError(f"Failed to find custom emoji: {custom_emoji.name}") |
| 173 | + return emoji |
0 commit comments