Fix handling raw message deletion

This commit is contained in:
Jeremy Zhang 2018-03-24 14:46:32 +00:00
parent f7caa5ede5
commit 21dc5d0925

View File

@ -10,7 +10,6 @@ import asyncio
import sys import sys
import logging import logging
import json import json
import zlib
logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
handler = logging.FileHandler(config.get("logging-location", "titanbot.log")) handler = logging.FileHandler(config.get("logging-location", "titanbot.log"))
logging.getLogger('TitanBot') logging.getLogger('TitanBot')
@ -27,8 +26,6 @@ class Titan(discord.AutoShardedClient):
self.socketio = SocketIOInterface(self, config["redis-uri"]) self.socketio = SocketIOInterface(self, config["redis-uri"])
self.delete_list = deque(maxlen=100) # List of msg ids to prevent duplicate delete self.delete_list = deque(maxlen=100) # List of msg ids to prevent duplicate delete
self.zlib_obj = zlib.decompressobj()
self.buffer_arr = bytearray()
self.discordBotsOrg = None self.discordBotsOrg = None
self.botsDiscordPw = None self.botsDiscordPw = None
@ -198,44 +195,24 @@ class Titan(discord.AutoShardedClient):
async def on_webhooks_update(self, guild, channel): async def on_webhooks_update(self, guild, channel):
await self.database.update_guild(guild) await self.database.update_guild(guild)
async def on_socket_raw_receive(self, msg): async def on_raw_message_edit(self, message_id, data):
if isinstance(msg, bytes): if not self.in_messages_cache(int(message_id)):
self.buffer_arr.extend(msg) channel = self.get_channel(int(data["channel_id"]))
message = await channel.get_message(int(message_id))
if len(msg) >= 4: await self.on_message_edit(None, message)
if msg[-4:] == b'\x00\x00\xff\xff':
try: async def on_raw_message_delete(self, message_id, channel_id):
msg = self.zlib_obj.decompress(self.buffer_arr) if not self.in_messages_cache(int(message_id)):
msg = msg.decode('utf-8')
except:
return
finally:
self.buffer_arr = bytearray()
else:
return
else:
return
msg = json.loads(msg)
if msg["op"] != 0:
return
action = msg["t"]
if action == "MESSAGE_UPDATE":
if not self.in_messages_cache(int(msg["d"]["id"])):
channel = self.get_channel(int(msg["d"]["channel_id"]))
message = await channel.get_message(channel, int(msg["d"]["id"]))
await self.on_message_edit(None, message)
if action == "MESSAGE_DELETE":
if not self.in_messages_cache(int(msg["d"]["id"])):
await asyncio.sleep(1)
await self.process_raw_message_delete(int(msg["d"]["id"]), int(msg["d"]["channel_id"]))
if action == "MESSAGE_DELETE_BULK":
await asyncio.sleep(1) await asyncio.sleep(1)
for msgid in msg["d"]["ids"]: await self.process_raw_message_delete(int(message_id), int(channel_id))
msgid = int(msgid)
if not self.in_messages_cache(msgid): async def raw_bulk_message_delete(self, message_ids, channel_id):
await self.process_raw_message_delete(msgid, int(msg["d"]["channel_id"])) await asyncio.sleep(1)
for msgid in message_ids:
msgid = int(msgid)
if not self.in_messages_cache(msgid):
await self.process_raw_message_delete(msgid, int(channel_id))
async def process_raw_message_delete(self, msg_id, channel_id): async def process_raw_message_delete(self, msg_id, channel_id):
if msg_id in self.delete_list: if msg_id in self.delete_list: