diff --git a/litecord/blueprints/webhooks.py b/litecord/blueprints/webhooks.py index d17c87e..fb91af0 100644 --- a/litecord/blueprints/webhooks.py +++ b/litecord/blueprints/webhooks.py @@ -318,14 +318,13 @@ async def create_message_webhook(guild_id, channel_id, webhook_id, data): await conn.execute( """ - INSERT INTO messages (id, channel_id, guild_id, webhook_id, + INSERT INTO messages (id, channel_id, guild_id, content, tts, mention_everyone, message_type, embeds) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) """, message_id, channel_id, guild_id, - webhook_id, data['content'], data['tts'], @@ -335,6 +334,15 @@ async def create_message_webhook(guild_id, channel_id, webhook_id, data): data.get('embeds', []) ) + info = data['info'] + + await conn.execute(""" + INSERT INTO message_webhook_info + (message_id, webhook_id, name, avatar) + VALUES + ($1, $2, $3, $4) + """, message_id, webhook_id, info['name'], info['avatar']) + return message_id @@ -352,7 +360,7 @@ async def _create_avatar(webhook_id: int, avatar_url): raise BadRequest('url is not media url') resp, raw = await fetch_raw_img(avatar_url) - raw_b64 = base64.b64encode(raw) + raw_b64 = base64.b64encode(raw).decode() mime = resp.headers['content-type'] b64_data = f'data:{mime};base64,{raw_b64}' @@ -405,8 +413,7 @@ async def execute_webhook(webhook_id: int, webhook_token): 'embeds': await async_map(fill_embed, given_embeds), 'info': { - 'id': webhook_id, - 'name': j.get('name', webhook['name']), + 'name': j.get('username', webhook['name']), 'avatar': avatar } } diff --git a/litecord/embed/messages.py b/litecord/embed/messages.py index 7c4e0c8..e4989d1 100644 --- a/litecord/embed/messages.py +++ b/litecord/embed/messages.py @@ -25,6 +25,7 @@ from pathlib import Path from logbook import Logger from litecord.embed.sanitizer import proxify, fetch_metadata, fetch_embed +from litecord.embed.schemas import EmbedURL log = Logger(__name__) @@ -85,9 +86,14 @@ async def _update_and_dispatch(payload, new_embeds, storage, dispatcher): 'channel', channel_id, 'MESSAGE_UPDATE', update_payload) -def is_media_url(url: str) -> bool: +def is_media_url(url) -> bool: """Return if the given URL is a media url.""" - parsed = urllib.parse.urlparse(url) + + if isinstance(url, EmbedURL): + parsed = url.parsed + else: + parsed = urllib.parse.urlparse(url) + path = Path(parsed.path) extension = path.suffix.lstrip('.') diff --git a/litecord/storage.py b/litecord/storage.py index 31c836f..600e486 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -813,16 +813,26 @@ class Storage: WHERE message_id = $1 """, int(res['id'])) - res['author'] = { - 'id': str(wb_info['id']), + if not wb_info: + log.warning('webhook info not found for msg {}', + res['id']) + + wb_info = wb_info or { + 'id': res['id'], 'bot': True, + 'avatar': None, + 'username': '', 'discriminator': '0000', + } + + res['author'] = { + 'id': str(wb_info['webhook_id']), + 'bot': True, 'username': wb_info['name'], 'avatar': wb_info['avatar'] } else: res['author'] = await self.get_user(res['author_id']) - res.pop('webhook_id') res.pop('author_id')