From f7b3e4e65e01c1eff09d9b8c11a85a5e903de257 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 22 Mar 2018 22:55:09 +0000 Subject: [PATCH] AutoShard Titan bot with dpy rewrite, Closes #71 --- discordbot/fetch_last_messages.py | 4 +- discordbot/titanembeds/bot.py | 124 +++++++++--------- discordbot/titanembeds/database/__init__.py | 41 +++--- discordbot/titanembeds/poststats.py | 8 +- .../titanembeds/socketio/socketiointerface.py | 89 +++++++------ discordbot/titanembeds/utils.py | 60 ++++++--- requirements.txt | 2 +- 7 files changed, 178 insertions(+), 150 deletions(-) diff --git a/discordbot/fetch_last_messages.py b/discordbot/fetch_last_messages.py index 5199163..28d7e75 100644 --- a/discordbot/fetch_last_messages.py +++ b/discordbot/fetch_last_messages.py @@ -76,7 +76,7 @@ class Titan(discord.Client): return if "server" == sys.argv[1]: server_id = sys.argv[2] - server = self.get_server(server_id) + server = self.get_guild(server_id) if not server: print("Server not found") await self.logout() @@ -93,7 +93,7 @@ class Titan(discord.Client): for channel in all_channels: try: if str(channel.type) == "text": - print("Processing channel: ID-{} Name-'{}' ServerID-{} Server-'{}'".format(channel.id, channel.name, channel.server.id, channel.server.name)) + print("Processing channel: ID-{} Name-'{}' ServerID-{} Server-'{}'".format(channel.id, channel.name, channel.guild.id, channel.guild.name)) await self.database.delete_all_messages_from_channel(channel.id) async for message in self.logs_from(channel, limit=50, reverse=True): await self.database.push_message(message) diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index 3448b6f..6e00570 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -10,21 +10,25 @@ import asyncio import sys import logging 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') handler = logging.FileHandler(config.get("logging-location", "titanbot.log")) logging.getLogger('TitanBot') logging.getLogger('sqlalchemy') -class Titan(discord.Client): +class Titan(discord.AutoShardedClient): def __init__(self): - super().__init__(max_messages=20000) + game = discord.Game(name="Embed your Discord server! Visit https://TitanEmbeds.com/") + super().__init__(max_messages=20000, game=game) self.aiosession = aiohttp.ClientSession(loop=self.loop) self.http.user_agent += ' TitanEmbeds-Bot' self.database = DatabaseInterface(self) self.command = Commands(self, self.database) self.socketio = SocketIOInterface(self, config["redis-uri"]) - self.delete_list = [] # 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.botsDiscordPw = None @@ -61,10 +65,8 @@ class Titan(discord.Client): print(self.user.name) print(self.user.id) print('------') - - await self.change_presence( - game=discord.Game(name="Embed your Discord server! Visit https://TitanEmbeds.com/"), status=discord.Status.online - ) + print("Shard count: " + str(self.shard_count)) + print("------") try: await self.database.connect(config["database-uri"]) @@ -78,34 +80,12 @@ class Titan(discord.Client): self.botsDiscordPw = BotsDiscordPw(self.user.id, config.get("bots-discord-pw-token", None)) await self.postStats() - # if "no-init" not in sys.argv: - # for server in self.servers: - # await self.database.update_guild(server) - # if server.large: - # await self.request_offline_members(server) - # if server.me.server_permissions.ban_members: - # server_bans = await self.get_bans(server) - # else: - # server_bans = [] - # for member in server.members: - # banned = member.id in [u.id for u in server_bans] - # await self.database.update_guild_member( - # member, - # True, - # banned - # ) - # await self.database.flag_unactive_guild_members(server.id, server.members) - # await self.database.flag_unactive_bans(server.id, server_bans) - # await self.database.remove_unused_guilds(self.servers) - # else: - # print("Skipping indexing server due to no-init flag") - async def on_message(self, message): await self.socketio.on_message(message) await self.database.push_message(message) msg_arr = message.content.split() # split the message - if len(message.content.split()) > 1 and message.server: #making sure there is actually stuff in the message and have arguments and check if it is sent in server (not PM) + if len(message.content.split()) > 1 and message.guild: #making sure there is actually stuff in the message and have arguments and check if it is sent in server (not PM) if msg_arr[0] == "<@{}>".format(self.user.id) or msg_arr[0] == "<@!{}>".format(self.user.id): #make sure it is mention msg_cmd = msg_arr[1].lower() # get command cmd = getattr(self.command, msg_cmd, None) #check if cmd exist, if not its none @@ -122,12 +102,12 @@ class Titan(discord.Client): await self.database.delete_message(message) await self.socketio.on_message_delete(message) - async def on_server_join(self, guild): + async def on_guild_join(self, guild): await self.database.update_guild(guild) for member in guild.members: await self.database.update_guild_member(member, True, False) - if guild.me.server_permissions.ban_members: - banned = await self.get_bans(guild) + if guild.me.guild_permissions.ban_members: + banned = await guild.bans() for ban in banned: member = discord.Member(user={ "username": ban.name, @@ -138,52 +118,52 @@ class Titan(discord.Client): }) await self.database.update_guild_member(member, False, True) for channel in list(guild.channels): - chanperm = channel.permissions_for(channel.server.me) - if not chanperm.read_messages or not chanperm.read_message_history: + chanperm = channel.permissions_for(channel.guild.me) + if not chanperm.read_messages or not chanperm.read_message_history or not isinstance(channel, discord.channel.TextChannel): continue - async for message in self.logs_from(channel, limit=50, reverse=True): + async for message in channel.history(limit=50, reverse=True): try: await self.database.push_message(message) except: pass await self.postStats() - async def on_server_remove(self, guild): + async def on_guild_remove(self, guild): await self.database.remove_guild(guild) await self.postStats() - async def on_server_update(self, guildbefore, guildafter): + async def on_guild_update(self, guildbefore, guildafter): await self.database.update_guild(guildafter) await self.socketio.on_guild_update(guildafter) - async def on_server_role_create(self, role): + async def on_guild_role_create(self, role): if role.name == self.user.name and role.managed: await asyncio.sleep(2) - await self.database.update_guild(role.server) + await self.database.update_guild(role.guild) await self.socketio.on_guild_role_create(role) - async def on_server_role_delete(self, role): - if role.server.me not in role.server.members: + async def on_guild_role_delete(self, role): + if role.guild.me not in role.guild.members: return - await self.database.update_guild(role.server) + await self.database.update_guild(role.guild) await self.socketio.on_guild_role_delete(role) - async def on_server_role_update(self, rolebefore, roleafter): - await self.database.update_guild(roleafter.server) + async def on_guild_role_update(self, rolebefore, roleafter): + await self.database.update_guild(roleafter.guild) await self.socketio.on_guild_role_update(roleafter) async def on_channel_delete(self, channel): - if channel.server: - await self.database.update_guild(channel.server) + if channel.guild: + await self.database.update_guild(channel.guild) await self.socketio.on_channel_delete(channel) async def on_channel_create(self, channel): - if channel.server: - await self.database.update_guild(channel.server) + if channel.guild: + await self.database.update_guild(channel.guild) await self.socketio.on_channel_create(channel) async def on_channel_update(self, channelbefore, channelafter): - await self.database.update_guild(channelafter.server) + await self.database.update_guild(channelafter.guild) await self.socketio.on_channel_update(channelafter) async def on_member_join(self, member): @@ -205,23 +185,34 @@ class Titan(discord.Client): return await self.database.update_guild_member(member, active=False, banned=True) - async def on_member_unban(self, server, user): - await self.database.unban_server_user(user, server) + async def on_member_unban(self, guild, user): + await self.database.unban_server_user(user, guild) - async def on_server_emojis_update(self, before, after): + async def on_guild_emojis_update(self, guild, before, after): if len(after) == 0: - await self.database.update_guild(before[0].server) + await self.database.update_guild(guild) await self.socketio.on_guild_emojis_update(before) else: - await self.database.update_guild(after[0].server) + await self.database.update_guild(guild) await self.socketio.on_guild_emojis_update(after) - async def on_webhooks_update(self, server): - await self.database.update_guild(server) + async def on_webhooks_update(self, guild, channel): + await self.database.update_guild(guild) async def on_socket_raw_receive(self, msg): - if type(msg) is not str: - return + if isinstance(msg, bytes): + self.buffer_arr.extend(msg) + + if len(msg) >= 4: + if msg[-4:] == b'\x00\x00\xff\xff': + msg = self.zlib_obj.decompress(self.buffer_arr) + msg = msg.decode('utf-8') + self.buffer_arr = bytearray() + else: + return + else: + return + msg = json.loads(msg) if msg["op"] != 0: return @@ -245,17 +236,20 @@ class Titan(discord.Client): if msg_id in self.delete_list: self.delete_list.remove(msg_id) return - channel = self.get_channel(channel_id) - msg = discord.Message(channel=channel, reactions=[], id=msg_id, type=0, timestamp="2017-01-15T02:59:58", content="What fun is there in making sense?") # Procreate a fake message object + channel = self.get_channel(int(channel_id)) + data = {'content': "What fun is there in making sense?", 'type': 0, 'edited_timestamp': None, 'id': msg_id, 'channel_id': channel_id, 'timestamp': '2017-01-15T02:59:58+00:00'} + msg = discord.Message(channel=channel, state=self._connection, data=data) # Procreate a fake message object await self.on_message_delete(msg) def in_messages_cache(self, msg_id): - for msg in self.messages: + for msg in self._connection._messages: if msg.id == msg_id: return True return False async def postStats(self): - count = len(self.servers) - await self.discordBotsOrg.post(count) - await self.botsDiscordPw.post(count) + count = len(self.guilds) + shard_count = self.shard_count + shard_id = self.shard_id + await self.discordBotsOrg.post(count, shard_count, shard_id) + await self.botsDiscordPw.post(count, shard_count, shard_id) diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index ca30867..659e127 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -16,7 +16,7 @@ from titanembeds.database.guild_members import GuildMembers from titanembeds.database.unauthenticated_users import UnauthenticatedUsers from titanembeds.database.unauthenticated_bans import UnauthenticatedBans -from titanembeds.utils import get_message_author, get_message_mentions, get_webhooks_list, get_emojis_list, get_roles_list, get_channels_list, list_role_ids +from titanembeds.utils import get_message_author, get_message_mentions, get_webhooks_list, get_emojis_list, get_roles_list, get_channels_list, list_role_ids, get_attachments_list, get_embeds_list class DatabaseInterface(object): # Courtesy of https://github.com/SunDwarf/Jokusoramame @@ -44,54 +44,54 @@ class DatabaseInterface(object): session.close() async def push_message(self, message): - if message.server: + if message.guild: async with threadpool(): with self.get_session() as session: - edit_ts = message.edited_timestamp + edit_ts = message.edited_at if not edit_ts: edit_ts = None else: edit_ts = str(edit_ts) msg = Messages( - int(message.server.id), + int(message.guild.id), int(message.channel.id), int(message.id), message.content, json.dumps(get_message_author(message)), - str(message.timestamp), + str(message.created_at), edit_ts, json.dumps(get_message_mentions(message.mentions)), - json.dumps(message.attachments), - json.dumps(message.embeds) + json.dumps(get_attachments_list(message.attachments)), + json.dumps(get_embeds_list(message.embeds)) ) session.add(msg) session.commit() async def update_message(self, message): - if message.server: + if message.guild: async with threadpool(): with self.get_session() as session: msg = session.query(Messages) \ - .filter(Messages.guild_id == message.server.id) \ + .filter(Messages.guild_id == message.guild.id) \ .filter(Messages.channel_id == message.channel.id) \ .filter(Messages.message_id == message.id).first() if msg: msg.content = message.content - msg.timestamp = message.timestamp - msg.edited_timestamp = message.edited_timestamp + msg.timestamp = message.created_at + msg.edited_timestamp = message.edited_at msg.mentions = json.dumps(get_message_mentions(message.mentions)) - msg.attachments = json.dumps(message.attachments) - msg.embeds = json.dumps(message.embeds) + msg.attachments = json.dumps(get_attachments_list(message.attachments)) + msg.embeds = json.dumps(get_embeds_list(message.embeds)) msg.author = json.dumps(get_message_author(message)) session.commit() async def delete_message(self, message): - if message.server: + if message.guild: async with threadpool(): with self.get_session() as session: msg = session.query(Messages) \ - .filter(Messages.guild_id == int(message.server.id)) \ + .filter(Messages.guild_id == int(message.guild.id)) \ .filter(Messages.channel_id == int(message.channel.id)) \ .filter(Messages.message_id == int(message.id)).first() if msg: @@ -99,8 +99,11 @@ class DatabaseInterface(object): session.commit() async def update_guild(self, guild): - if guild.me.server_permissions.manage_webhooks: - server_webhooks = await self.bot.get_server_webhooks(guild) + if guild.me.guild_permissions.manage_webhooks: + try: + server_webhooks = await guild.webhooks() + except: + server_webhooks = [] else: server_webhooks = [] async with threadpool(): @@ -159,12 +162,12 @@ class DatabaseInterface(object): async with threadpool(): with self.get_session() as session: dbmember = session.query(GuildMembers) \ - .filter(GuildMembers.guild_id == int(member.server.id)) \ + .filter(GuildMembers.guild_id == int(member.guild.id)) \ .filter(GuildMembers.user_id == int(member.id)) \ .order_by(GuildMembers.id).all() if not dbmember: dbmember = GuildMembers( - int(member.server.id), + int(member.guild.id), int(member.id), member.name, member.discriminator, diff --git a/discordbot/titanembeds/poststats.py b/discordbot/titanembeds/poststats.py index 00fe44b..fc263f4 100644 --- a/discordbot/titanembeds/poststats.py +++ b/discordbot/titanembeds/poststats.py @@ -5,9 +5,9 @@ class DiscordBotsOrg(): # https://discordbots.org self.url = "https://discordbots.org/api/bots/{}/stats".format(client_id) self.token = token - async def post(self, count): + async def post(self, count, shard_count, shard_id): headers = {"Authorization": self.token} - payload = {"server_count": count} + payload = {"server_count": count, "shard_count": shard_count, "shard_no": shard_id} async with aiohttp.ClientSession() as aioclient: await aioclient.post(self.url, data=payload, headers=headers) @@ -16,8 +16,8 @@ class BotsDiscordPw(): # https://bots.discord.pw/ self.url = "https://bots.discord.pw/api/bots/{}/stats".format(client_id) self.token = token - async def post(self, count): + async def post(self, count, shard_count, shard_id): headers = {"Authorization": self.token} - payload = {"server_count": count} + payload = {"server_count": count, "shard_count": shard_count, "shard_id": shard_id} async with aiohttp.ClientSession() as aioclient: await aioclient.post(self.url, json=payload, headers=headers) \ No newline at end of file diff --git a/discordbot/titanembeds/socketio/socketiointerface.py b/discordbot/titanembeds/socketio/socketiointerface.py index 527c3e7..bf7ace7 100644 --- a/discordbot/titanembeds/socketio/socketiointerface.py +++ b/discordbot/titanembeds/socketio/socketiointerface.py @@ -1,5 +1,5 @@ import socketio -from titanembeds.utils import get_message_author, get_message_mentions, get_roles_list +from titanembeds.utils import get_message_author, get_message_mentions, get_roles_list, get_attachments_list, get_embeds_list import time from email import utils as emailutils @@ -12,47 +12,52 @@ class SocketIOInterface: return emailutils.formatdate(time.mktime(datetimeobj.timetuple())) # https://stackoverflow.com/questions/3453177/convert-python-datetime-to-rfc-2822 def get_formatted_message(self, message): - edit_ts = message.edited_timestamp + edit_ts = message.edited_at if not edit_ts: edit_ts = None else: edit_ts = self.format_datetime(edit_ts) msg = { - "id": message.id, - "channel_id": message.channel.id, + "id": str(message.id), + "channel_id": str(message.channel.id), "content": message.content, "author": get_message_author(message), - "timestamp": self.format_datetime(message.timestamp), + "timestamp": self.format_datetime(message.created_at), "edited_timestamp": edit_ts, - "mentions": get_message_mentions(message.mentions), - "attachments": message.attachments, - "embeds": message.embeds, } - nickname = None - if hasattr(message.author, 'nick') and message.author.nick: - nickname = message.author.nick - msg["author"]["nickname"] = nickname - for mention in msg["mentions"]: - mention["nickname"] = None - member = message.server.get_member(mention["id"]) - if member: - mention["nickname"] = member.nick + if hasattr(message, "mentions"): + msg["mentions"] = get_message_mentions(message.mentions) + if hasattr(message, "attachments"): + msg["attachments"] = get_attachments_list(message.attachments) + if hasattr(message, "embeds"): + msg["embeds"] = get_embeds_list(message.embeds) + if hasattr(message, "author"): + nickname = None + if hasattr(message.author, 'nick') and message.author.nick: + nickname = message.author.nick + msg["author"]["nickname"] = nickname + if hasattr(message, "mentions"): + for mention in msg["mentions"]: + mention["nickname"] = None + member = message.guild.get_member(mention["id"]) + if member: + mention["nickname"] = member.nick return msg async def on_message(self, message): - if message.server: + if message.guild: msg = self.get_formatted_message(message) - await self.io.emit('MESSAGE_CREATE', data=msg, room=str("CHANNEL_"+message.channel.id), namespace='/gateway') + await self.io.emit('MESSAGE_CREATE', data=msg, room=str("CHANNEL_"+str(message.channel.id)), namespace='/gateway') async def on_message_delete(self, message): - if message.server: + if message.guild: msg = self.get_formatted_message(message) - await self.io.emit('MESSAGE_DELETE', data=msg, room=str("CHANNEL_"+message.channel.id), namespace='/gateway') + await self.io.emit('MESSAGE_DELETE', data=msg, room=str("CHANNEL_"+str(message.channel.id)), namespace='/gateway') async def on_message_update(self, message): - if message.server: + if message.guild: msg = self.get_formatted_message(message) - await self.io.emit('MESSAGE_UPDATE', data=msg, room=str("CHANNEL_"+message.channel.id), namespace='/gateway') + await self.io.emit('MESSAGE_UPDATE', data=msg, room=str("CHANNEL_"+str(message.channel.id)), namespace='/gateway') def get_formatted_user(self, user): userobj = { @@ -73,7 +78,7 @@ class SocketIOInterface: # userobj["avatar_url"] = userobj["avatar_url"][:len(userobj["avatar_url"])-14] + ".jpg" if user.nick: userobj["nick"] = user.nick - if user.game: + if hasattr(user, "game"): userobj["game"] = { "name": user.game.name } @@ -81,7 +86,7 @@ class SocketIOInterface: for role in roles: if role.hoist: userobj["hoist-role"] = { - "id": role.id, + "id": str(role.id), "name": role.name, "position": role.position, } @@ -90,21 +95,21 @@ class SocketIOInterface: async def on_guild_member_add(self, member): user = self.get_formatted_user(member) - await self.io.emit('GUILD_MEMBER_ADD', data=user, room=str("GUILD_"+member.server.id), namespace='/gateway') + await self.io.emit('GUILD_MEMBER_ADD', data=user, room=str("GUILD_"+str(member.guild.id)), namespace='/gateway') async def on_guild_member_remove(self, member): user = self.get_formatted_user(member) - await self.io.emit('GUILD_MEMBER_REMOVE', data=user, room=str("GUILD_"+member.server.id), namespace='/gateway') + await self.io.emit('GUILD_MEMBER_REMOVE', data=user, room=str("GUILD_"+str(member.guild.id)), namespace='/gateway') async def on_guild_member_update(self, member): user = self.get_formatted_user(member) - await self.io.emit('GUILD_MEMBER_UPDATE', data=user, room=str("GUILD_"+member.server.id), namespace='/gateway') + await self.io.emit('GUILD_MEMBER_UPDATE', data=user, room=str("GUILD_"+str(member.guild.id)), namespace='/gateway') def get_formatted_emojis(self, emojis): emotes = [] for emo in emojis: emotes.append({ - "id": emo.id, + "id": str(emo.id), "managed": emo.managed, "name": emo.name, "require_colons": emo.require_colons, @@ -115,11 +120,11 @@ class SocketIOInterface: async def on_guild_emojis_update(self, emojis): emotes = self.get_formatted_emojis(emojis) - await self.io.emit('GUILD_EMOJIS_UPDATE', data=emotes, room=str("GUILD_"+emojis[0].server.id), namespace='/gateway') + await self.io.emit('GUILD_EMOJIS_UPDATE', data=emotes, room=str("GUILD_"+str(emojis[0].guild.id)), namespace='/gateway') def get_formatted_guild(self, guild): guil = { - "id": guild.id, + "id": str(guild.id), "name": guild.name, "icon": guild.icon, "icon_url": guild.icon_url, @@ -128,12 +133,12 @@ class SocketIOInterface: async def on_guild_update(self, guild): guildobj = self.get_formatted_guild(guild) - await self.io.emit('GUILD_UPDATE', data=guildobj, room=str("GUILD_"+guild.id), namespace='/gateway') + await self.io.emit('GUILD_UPDATE', data=guildobj, room=str("GUILD_"+str(guild.id)), namespace='/gateway') def get_formatted_channel(self, channel): chan = { - "id": channel.id, - "guild_id": channel.server.id, + "id": str(channel.id), + "guild_id": str(channel.guild.id), } return chan @@ -141,24 +146,24 @@ class SocketIOInterface: if str(channel.type) != "text": return chan = self.get_formatted_channel(channel) - await self.io.emit('CHANNEL_DELETE', data=chan, room=str("GUILD_"+channel.server.id), namespace='/gateway') + await self.io.emit('CHANNEL_DELETE', data=chan, room=str("GUILD_"+str(channel.guild.id)), namespace='/gateway') async def on_channel_create(self, channel): if str(channel.type) != "text": return chan = self.get_formatted_channel(channel) - await self.io.emit('CHANNEL_CREATE', data=chan, room=str("GUILD_"+channel.server.id), namespace='/gateway') + await self.io.emit('CHANNEL_CREATE', data=chan, room=str("GUILD_"+str(channel.guild.id)), namespace='/gateway') async def on_channel_update(self, channel): if str(channel.type) not in ["text", "category"]: return chan = self.get_formatted_channel(channel) - await self.io.emit('CHANNEL_UPDATE', data=chan, room=str("GUILD_"+channel.server.id), namespace='/gateway') + await self.io.emit('CHANNEL_UPDATE', data=chan, room=str("GUILD_"+str(channel.guild.id)), namespace='/gateway') def get_formatted_role(self, role): rol = { - "id": role.id, - "guild_id": role.server.id, + "id": str(role.id), + "guild_id": str(role.guild.id), "name": role.name, "color": role.color.value, "hoist": role.hoist, @@ -169,12 +174,12 @@ class SocketIOInterface: async def on_guild_role_create(self, role): rol = self.get_formatted_role(role) - await self.io.emit('GUILD_ROLE_CREATE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') + await self.io.emit('GUILD_ROLE_CREATE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway') async def on_guild_role_update(self, role): rol = self.get_formatted_role(role) - await self.io.emit('GUILD_ROLE_UPDATE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') + await self.io.emit('GUILD_ROLE_UPDATE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway') async def on_guild_role_delete(self, role): rol = self.get_formatted_role(role) - await self.io.emit('GUILD_ROLE_DELETE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') \ No newline at end of file + await self.io.emit('GUILD_ROLE_DELETE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway') \ No newline at end of file diff --git a/discordbot/titanembeds/utils.py b/discordbot/titanembeds/utils.py index 23dabf8..b5b89e8 100644 --- a/discordbot/titanembeds/utils.py +++ b/discordbot/titanembeds/utils.py @@ -1,12 +1,14 @@ import discord def get_message_author(message): + if not hasattr(message, "author"): + return {} author = message.author obj = { "username": author.name, "discriminator": author.discriminator, "bot": author.bot, - "id": author.id, + "id": str(author.id), "avatar": author.avatar } return obj @@ -18,7 +20,7 @@ def get_message_mentions(mentions): "username": author.name, "discriminator": author.discriminator, "bot": author.bot, - "id": author.id, + "id": str(author.id), "avatar": author.avatar }) return ments @@ -27,9 +29,9 @@ def get_webhooks_list(guild_webhooks): webhooks = [] for webhook in guild_webhooks: webhooks.append({ - "id": webhook.id, - "guild_id": webhook.server.id, - "channel_id": webhook.channel.id, + "id": str(webhook.id), + "guild_id": str(webhook.guild.id), + "channel_id": str(webhook.channel.id), "name": webhook.name, "token": webhook.token, }) @@ -39,7 +41,7 @@ def get_emojis_list(guildemojis): emojis = [] for emote in guildemojis: emojis.append({ - "id": emote.id, + "id": str(emote.id), "name": emote.name, "require_colons": emote.require_colons, "managed": emote.managed, @@ -53,7 +55,7 @@ def get_roles_list(guildroles): roles = [] for role in guildroles: roles.append({ - "id": role.id, + "id": str(role.id), "name": role.name, "color": role.color.value, "hoist": role.hoist, @@ -65,8 +67,9 @@ def get_roles_list(guildroles): def get_channels_list(guildchannels): channels = [] for channel in guildchannels: - if str(channel.type) in ["text", "category"]: + if isinstance(channel, discord.channel.TextChannel) or isinstance(channel, discord.channel.CategoryChannel): overwrites = [] + isTextChannel = isinstance(channel, discord.channel.TextChannel) for target, overwrite in channel.overwrites: if isinstance(target, discord.Role): type = "role" @@ -76,28 +79,51 @@ def get_channels_list(guildchannels): allow = allow.value deny = deny.value overwrites.append({ - "id": target.id, + "id": str(target.id), "type": type, "allow": allow, "deny": deny, }) - parent = channel.parent + parent = channel.category if parent: - parent = parent.id + parent = str(parent.id) channels.append({ - "id": channel.id, + "id": str(channel.id), "name": channel.name, - "topic": channel.topic, + "topic": channel.topic if isTextChannel else None, "position": channel.position, - "type": str(channel.type), + "type": "text" if isTextChannel else "category", "permission_overwrites": overwrites, "parent_id": parent, - "nsfw": channel.is_nsfw, + "nsfw": channel.is_nsfw(), }) return channels def list_role_ids(usr_roles): ids = [] for role in usr_roles: - ids.append(role.id) - return ids \ No newline at end of file + ids.append(str(role.id)) + return ids + +def get_attachments_list(attachments): + attr = [] + for attach in attachments: + a = { + "id": str(attach.id), + "size": attach.size, + "filename": attach.filename, + "url": attach.url, + "proxy_url": attach.proxy_url, + } + if attach.height: + a["height"] = attach.height + if attach.width: + a["width"] = attach.width + attr.append(a) + return attr + +def get_embeds_list(embeds): + em = [] + for e in embeds: + em.append(e.to_dict()) + return em \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2d6e3cd..360d24d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ requests_oauthlib Flask-SSLify flask_socketio paypalrestsdk -git+https://github.com/TitanEmbeds/discord.py +git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py asyncio_extras kombu redis