diff --git a/litecord/system_messages.py b/litecord/system_messages.py index 33a10a0..0c1ebfb 100644 --- a/litecord/system_messages.py +++ b/litecord/system_messages.py @@ -17,11 +17,14 @@ along with this program. If not, see . """ +from logbook import Logger + from litecord.snowflake import get_snowflake from litecord.enums import MessageType +log = Logger(__name__) -async def _handle_pin_msg(app, channel_id, pinned_id, author_id): +async def _handle_pin_msg(app, channel_id, _pinned_id, author_id): """Handle a message pin.""" new_id = get_snowflake() @@ -41,12 +44,108 @@ async def _handle_pin_msg(app, channel_id, pinned_id, author_id): return new_id +# TODO: decrease repetition between add and remove handlers +async def _handle_recp_add(app, channel_id, author_id, peer_id): + new_id = get_snowflake() + + await app.db.execute( + """ + INSERT INTO messages + (id, channel_id, author_id, webhook_id, + content, message_type) + VALUES + ($1, $2, $3, NULL, $4, $5) + """, + new_id, channel_id, author_id, + f'<@{peer_id}>', + MessageType.RECIPIENT_ADD.value + ) + + return new_id + + + +async def _handle_recp_rmv(app, channel_id, author_id, peer_id): + new_id = get_snowflake() + + await app.db.execute( + """ + INSERT INTO messages + (id, channel_id, author_id, webhook_id, + content, message_type) + VALUES + ($1, $2, $3, NULL, $4, $5) + """, + new_id, channel_id, author_id, + f'<@{peer_id}>', + MessageType.RECIPIENT_REMOVE.value + ) + + return new_id + + +async def _handle_gdm_name_edit(app, channel_id, author_id): + new_id = get_snowflake() + + gdm_name = await app.db.fetchval(""" + SELECT name FROM group_dm_channels + WHERE id = $1 + """, channel_id) + + if not gdm_name: + log.warning('no gdm name found for sys message') + return + + await app.db.execute( + """ + INSERT INTO messages + (id, channel_id, author_id, webhook_id, + content, message_type) + VALUES + ($1, $2, $3, NULL, $4, $5) + """, + new_id, channel_id, author_id, + gdm_name, + MessageType.RECIPIENT_ADD.value + ) + + return new_id + + +async def _handle_gdm_icon_edit(app, channel_id, author_id): + new_id = get_snowflake() + + await app.db.execute( + """ + INSERT INTO messages + (id, channel_id, author_id, webhook_id, + content, message_type) + VALUES + ($1, $2, $3, NULL, $4, $5) + """, + new_id, channel_id, author_id, + '', + MessageType.CHANNEL_ICON_CHANGE.value + ) + + return new_id + + async def send_sys_message(app, channel_id: int, m_type: MessageType, *args, **kwargs) -> int: """Send a system message.""" - handler = { - MessageType.CHANNEL_PINNED_MESSAGE: _handle_pin_msg, - }[m_type] + try: + handler = { + MessageType.CHANNEL_PINNED_MESSAGE: _handle_pin_msg, + + # gdm specific + MessageType.RECIPIENT_ADD: _handle_recp_add, + MessageType.RECIPIENT_REMOVE: _handle_recp_rmv, + MessageType.CHANNEL_NAME_CHANGE: _handle_gdm_name_edit, + MessageType.CHANNEL_ICON_CHANGE: _handle_gdm_icon_edit + }[m_type] + except KeyError: + raise ValueError('Invalid system message type') message_id = await handler(app, channel_id, *args, **kwargs)