From efefb0cc2feb43cdfddec535c0c51b561c93e719 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Thu, 11 Oct 2018 22:12:51 -0300 Subject: [PATCH] blueprints.channels: use ChannelDispatcher for MESSAGE_* events - storage: add Storage.guild_from_channel --- litecord/blueprints/channels.py | 22 +++++++++++++--------- litecord/storage.py | 7 +++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index 7a649ce..5bc0b84 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -285,7 +285,9 @@ async def create_message(channel_id): # we really need dispatch_channel to make dm messages work, # since they aren't part of any existing guild. payload = await app.storage.get_message(message_id) - await app.dispatcher.dispatch_guild(guild_id, 'MESSAGE_CREATE', payload) + + await app.dispatcher.dispatch('channel', channel_id, + 'MESSAGE_CREATE', payload) # TODO: dispatch the MESSAGE_CREATE to any mentioning user. @@ -330,8 +332,8 @@ async def edit_message(channel_id, message_id): # only dispatch MESSAGE_UPDATE if we actually had any update to start with if updated: - await app.dispatcher.dispatch_guild(guild_id, - 'MESSAGE_UPDATE', message) + await app.dispatcher.dispatch('channel', channel_id, + 'MESSAGE_UPDATE', message) return jsonify(message) @@ -355,13 +357,15 @@ async def delete_message(channel_id, message_id): WHERE messages.id = $1 """, message_id) - await app.dispatcher.dispatch_guild(guild_id, 'MESSAGE_DELETE', { - 'id': str(message_id), - 'channel_id': str(channel_id), + await app.dispatcher.dispatch( + 'channel', channel_id, + 'MESSAGE_DELETE', { + 'id': str(message_id), + 'channel_id': str(channel_id), - # for lazy guilds - 'guild_id': str(guild_id), - }) + # for lazy guilds + 'guild_id': str(guild_id), + }) return '', 204 diff --git a/litecord/storage.py b/litecord/storage.py index cb89c3c..a1dc2d0 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -840,3 +840,10 @@ class Storage: res.append(drow) return res + + async def guild_from_channel(self, channel_id: int): + return await self.db.fetchval(""" + SELECT guild_id + FROM guild_channels + WHERE id = $1 + """, channel_id)