diff --git a/cogs/emote.py b/cogs/emote.py index e0c847c..783f0d1 100644 --- a/cogs/emote.py +++ b/cogs/emote.py @@ -76,7 +76,7 @@ class Emotes(commands.Cog): + self.bot.http.user_agent }) - self.emote_client = EmoteClient(token=self.bot.config['tokens']['discord']) + self.emote_client = EmoteClient(self.bot) with open('data/ec-emotes-final.json') as f: self.ec_emotes = json.load(f) @@ -444,7 +444,7 @@ class Emotes(commands.Cog): image_data = await utils.image.resize_in_subprocess(image_data) if reason is None: reason = 'Created by ' + utils.format_user(self.bot, author_id) - return await self.emote_client.create(guild_id=guild.id, name=name, image=image_data, reason=reason) + return await self.emote_client.create(guild=guild, name=name, image=image_data, reason=reason) @commands.command(aliases=('delete', 'delet', 'rm')) async def remove(self, context, emote, *emotes): diff --git a/utils/emote_client.py b/utils/emote_client.py index 739513e..b18ae36 100644 --- a/utils/emote_client.py +++ b/utils/emote_client.py @@ -53,18 +53,15 @@ class EmoteClient: HTTPStatus.SERVICE_UNAVAILABLE: DiscordServerError, } - def __init__(self, *, token): + def __init__(self, bot): self.guild_rls: Dict[GuildId, GuildRetryTimes] = {} - user_agent = ( - 'EmoteManager-EmoteClient; ' - f'aiohttp/{aiohttp.__version__}; ' - f'{platform.python_implementation()}/{".".join(map(str, sys.version_info))}' - ) self.http = aiohttp.ClientSession(headers={ - 'User-Agent': user_agent, - 'Authorization': 'Bot ' + token, + 'User-Agent': bot.config['user_agent'] + ' ' + bot.http.user_agent, + 'Authorization': 'Bot ' + bot.config['tokens']['discord'], 'X-Ratelimit-Precision': 'millisecond', }) + # internals 🤫 + self._connection_state = bot._connection async def request(self, method, path, guild_id, **kwargs): self._check_rl(method, guild_id) @@ -129,13 +126,17 @@ class EmoteClient: def check_delete(self, guild_id): self._check_rl('DELETE', guild_id) - async def create(self, *, guild_id, name, image: bytes, role_ids=(), reason=None): - return await self.request( - 'POST', f'/guilds/{guild_id}/emojis', - guild_id, + async def create(self, *, guild, name, image: bytes, role_ids=(), reason=None): + data = await self.request( + 'POST', f'/guilds/{guild.id}/emojis', + guild.id, json=dict(name=name, image=image_utils.image_to_base64_url(image), roles=role_ids), reason=reason, ) + # internals 🤫 + # this is A) so we can return a bona-fide, authentic, Emoji object, + # and B) because it's what dpy does to keep the emoji cache up to date for s + return self._connection_state.store_emoji(guild, data) async def delete(self, *, guild_id, emote_id, reason=None): return await self.request('DELETE', f'/guilds/{guild_id}/emojis/{emote_id}', guild_id, reason=reason)