litecord: add system_messages module

- channel.pins: send a CHANNEL_PINNED_MESSAGE system message
This commit is contained in:
Luna Mendes 2018-11-22 17:08:28 -03:00
parent 7aff586790
commit 2adc88e9e4
3 changed files with 48 additions and 0 deletions

View File

@ -5,6 +5,9 @@ from litecord.blueprints.checks import channel_check, channel_perm_check
from litecord.snowflake import snowflake_datetime from litecord.snowflake import snowflake_datetime
from litecord.types import timestamp_ from litecord.types import timestamp_
from litecord.system_messages import send_sys_message
from litecord.enums import MessageType
bp = Blueprint('channel_pins', __name__) 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 return '', 204

View File

@ -699,6 +699,9 @@ class Storage:
# TODO: res['embeds'] # TODO: res['embeds']
res['embeds'] = [] res['embeds'] = []
# TODO: res['member'] for partial member data
# of the author
pin_id = await self.db.fetchval(""" pin_id = await self.db.fetchval("""
SELECT message_id SELECT message_id
FROM channel_pins FROM channel_pins

View File

@ -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
)