diff --git a/litecord/blueprints/channel/pins.py b/litecord/blueprints/channel/pins.py index c7007c4..851fb0a 100644 --- a/litecord/blueprints/channel/pins.py +++ b/litecord/blueprints/channel/pins.py @@ -5,6 +5,9 @@ from litecord.blueprints.checks import channel_check, channel_perm_check from litecord.snowflake import snowflake_datetime from litecord.types import timestamp_ +from litecord.system_messages import send_sys_message +from litecord.enums import MessageType + bp = Blueprint('channel_pins', __name__) @@ -63,6 +66,10 @@ async def add_pin(channel_id, message_id): } ) + await send_sys_message(app, channel_id, + MessageType.CHANNEL_PINNED_MESSAGE, + message_id, user_id) + return '', 204 diff --git a/litecord/storage.py b/litecord/storage.py index 04dfe87..0bb1cce 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -699,6 +699,9 @@ class Storage: # TODO: res['embeds'] res['embeds'] = [] + # TODO: res['member'] for partial member data + # of the author + pin_id = await self.db.fetchval(""" SELECT message_id FROM channel_pins diff --git a/litecord/system_messages.py b/litecord/system_messages.py new file mode 100644 index 0000000..e083902 --- /dev/null +++ b/litecord/system_messages.py @@ -0,0 +1,38 @@ +from litecord.snowflake import get_snowflake +from litecord.enums import MessageType + + +async def _handle_pin_msg(app, channel_id, pinned_id, author_id): + """Handle a message pin.""" + new_id = get_snowflake() + + await app.db.execute( + """ + INSERT INTO messages + (id, channel_id, guild_id, author_id, + webhook_id, content, message_type) + VALUES + ($1, $2, NULL, $3, NULL, '', + $4) + """, + new_id, channel_id, author_id, + MessageType.CHANNEL_PINNED_MESSAGE.value + ) + + return new_id + + +async def send_sys_message(app, channel_id: int, m_type: MessageType, + *args, **kwargs): + """Send a system message.""" + handler = { + MessageType.CHANNEL_PINNED_MESSAGE: _handle_pin_msg, + }[m_type] + + message_id = await handler(app, channel_id, *args, **kwargs) + + message = await app.storage.get_message(message_id) + + await app.dispatcher.dispatch( + 'channel', channel_id, 'MESSAGE_CREATE', message + )