AutoShard Titan bot with dpy rewrite, Closes #71

This commit is contained in:
Jeremy Zhang 2018-03-22 22:55:09 +00:00
parent 9c2b0990a7
commit f7b3e4e65e
7 changed files with 178 additions and 150 deletions

View File

@ -76,7 +76,7 @@ class Titan(discord.Client):
return return
if "server" == sys.argv[1]: if "server" == sys.argv[1]:
server_id = sys.argv[2] server_id = sys.argv[2]
server = self.get_server(server_id) server = self.get_guild(server_id)
if not server: if not server:
print("Server not found") print("Server not found")
await self.logout() await self.logout()
@ -93,7 +93,7 @@ class Titan(discord.Client):
for channel in all_channels: for channel in all_channels:
try: try:
if str(channel.type) == "text": 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) await self.database.delete_all_messages_from_channel(channel.id)
async for message in self.logs_from(channel, limit=50, reverse=True): async for message in self.logs_from(channel, limit=50, reverse=True):
await self.database.push_message(message) await self.database.push_message(message)

View File

@ -10,21 +10,25 @@ 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')
logging.getLogger('sqlalchemy') logging.getLogger('sqlalchemy')
class Titan(discord.Client): class Titan(discord.AutoShardedClient):
def __init__(self): 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.aiosession = aiohttp.ClientSession(loop=self.loop)
self.http.user_agent += ' TitanEmbeds-Bot' self.http.user_agent += ' TitanEmbeds-Bot'
self.database = DatabaseInterface(self) self.database = DatabaseInterface(self)
self.command = Commands(self, self.database) self.command = Commands(self, self.database)
self.socketio = SocketIOInterface(self, config["redis-uri"]) 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.discordBotsOrg = None
self.botsDiscordPw = None self.botsDiscordPw = None
@ -61,10 +65,8 @@ class Titan(discord.Client):
print(self.user.name) print(self.user.name)
print(self.user.id) print(self.user.id)
print('------') print('------')
print("Shard count: " + str(self.shard_count))
await self.change_presence( print("------")
game=discord.Game(name="Embed your Discord server! Visit https://TitanEmbeds.com/"), status=discord.Status.online
)
try: try:
await self.database.connect(config["database-uri"]) 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)) self.botsDiscordPw = BotsDiscordPw(self.user.id, config.get("bots-discord-pw-token", None))
await self.postStats() 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): async def on_message(self, message):
await self.socketio.on_message(message) await self.socketio.on_message(message)
await self.database.push_message(message) await self.database.push_message(message)
msg_arr = message.content.split() # split the 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 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 msg_cmd = msg_arr[1].lower() # get command
cmd = getattr(self.command, msg_cmd, None) #check if cmd exist, if not its none 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.database.delete_message(message)
await self.socketio.on_message_delete(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) await self.database.update_guild(guild)
for member in guild.members: for member in guild.members:
await self.database.update_guild_member(member, True, False) await self.database.update_guild_member(member, True, False)
if guild.me.server_permissions.ban_members: if guild.me.guild_permissions.ban_members:
banned = await self.get_bans(guild) banned = await guild.bans()
for ban in banned: for ban in banned:
member = discord.Member(user={ member = discord.Member(user={
"username": ban.name, "username": ban.name,
@ -138,52 +118,52 @@ class Titan(discord.Client):
}) })
await self.database.update_guild_member(member, False, True) await self.database.update_guild_member(member, False, True)
for channel in list(guild.channels): for channel in list(guild.channels):
chanperm = channel.permissions_for(channel.server.me) chanperm = channel.permissions_for(channel.guild.me)
if not chanperm.read_messages or not chanperm.read_message_history: if not chanperm.read_messages or not chanperm.read_message_history or not isinstance(channel, discord.channel.TextChannel):
continue continue
async for message in self.logs_from(channel, limit=50, reverse=True): async for message in channel.history(limit=50, reverse=True):
try: try:
await self.database.push_message(message) await self.database.push_message(message)
except: except:
pass pass
await self.postStats() 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.database.remove_guild(guild)
await self.postStats() 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.database.update_guild(guildafter)
await self.socketio.on_guild_update(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: if role.name == self.user.name and role.managed:
await asyncio.sleep(2) 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) await self.socketio.on_guild_role_create(role)
async def on_server_role_delete(self, role): async def on_guild_role_delete(self, role):
if role.server.me not in role.server.members: if role.guild.me not in role.guild.members:
return return
await self.database.update_guild(role.server) await self.database.update_guild(role.guild)
await self.socketio.on_guild_role_delete(role) await self.socketio.on_guild_role_delete(role)
async def on_server_role_update(self, rolebefore, roleafter): async def on_guild_role_update(self, rolebefore, roleafter):
await self.database.update_guild(roleafter.server) await self.database.update_guild(roleafter.guild)
await self.socketio.on_guild_role_update(roleafter) await self.socketio.on_guild_role_update(roleafter)
async def on_channel_delete(self, channel): async def on_channel_delete(self, channel):
if channel.server: if channel.guild:
await self.database.update_guild(channel.server) await self.database.update_guild(channel.guild)
await self.socketio.on_channel_delete(channel) await self.socketio.on_channel_delete(channel)
async def on_channel_create(self, channel): async def on_channel_create(self, channel):
if channel.server: if channel.guild:
await self.database.update_guild(channel.server) await self.database.update_guild(channel.guild)
await self.socketio.on_channel_create(channel) await self.socketio.on_channel_create(channel)
async def on_channel_update(self, channelbefore, channelafter): 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) await self.socketio.on_channel_update(channelafter)
async def on_member_join(self, member): async def on_member_join(self, member):
@ -205,23 +185,34 @@ class Titan(discord.Client):
return return
await self.database.update_guild_member(member, active=False, banned=True) await self.database.update_guild_member(member, active=False, banned=True)
async def on_member_unban(self, server, user): async def on_member_unban(self, guild, user):
await self.database.unban_server_user(user, server) 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: 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) await self.socketio.on_guild_emojis_update(before)
else: else:
await self.database.update_guild(after[0].server) await self.database.update_guild(guild)
await self.socketio.on_guild_emojis_update(after) await self.socketio.on_guild_emojis_update(after)
async def on_webhooks_update(self, server): async def on_webhooks_update(self, guild, channel):
await self.database.update_guild(server) await self.database.update_guild(guild)
async def on_socket_raw_receive(self, msg): async def on_socket_raw_receive(self, msg):
if type(msg) is not str: if isinstance(msg, bytes):
return 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) msg = json.loads(msg)
if msg["op"] != 0: if msg["op"] != 0:
return return
@ -245,17 +236,20 @@ class Titan(discord.Client):
if msg_id in self.delete_list: if msg_id in self.delete_list:
self.delete_list.remove(msg_id) self.delete_list.remove(msg_id)
return return
channel = self.get_channel(channel_id) channel = self.get_channel(int(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 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) await self.on_message_delete(msg)
def in_messages_cache(self, msg_id): def in_messages_cache(self, msg_id):
for msg in self.messages: for msg in self._connection._messages:
if msg.id == msg_id: if msg.id == msg_id:
return True return True
return False return False
async def postStats(self): async def postStats(self):
count = len(self.servers) count = len(self.guilds)
await self.discordBotsOrg.post(count) shard_count = self.shard_count
await self.botsDiscordPw.post(count) shard_id = self.shard_id
await self.discordBotsOrg.post(count, shard_count, shard_id)
await self.botsDiscordPw.post(count, shard_count, shard_id)

View File

@ -16,7 +16,7 @@ from titanembeds.database.guild_members import GuildMembers
from titanembeds.database.unauthenticated_users import UnauthenticatedUsers from titanembeds.database.unauthenticated_users import UnauthenticatedUsers
from titanembeds.database.unauthenticated_bans import UnauthenticatedBans 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): class DatabaseInterface(object):
# Courtesy of https://github.com/SunDwarf/Jokusoramame # Courtesy of https://github.com/SunDwarf/Jokusoramame
@ -44,54 +44,54 @@ class DatabaseInterface(object):
session.close() session.close()
async def push_message(self, message): async def push_message(self, message):
if message.server: if message.guild:
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
edit_ts = message.edited_timestamp edit_ts = message.edited_at
if not edit_ts: if not edit_ts:
edit_ts = None edit_ts = None
else: else:
edit_ts = str(edit_ts) edit_ts = str(edit_ts)
msg = Messages( msg = Messages(
int(message.server.id), int(message.guild.id),
int(message.channel.id), int(message.channel.id),
int(message.id), int(message.id),
message.content, message.content,
json.dumps(get_message_author(message)), json.dumps(get_message_author(message)),
str(message.timestamp), str(message.created_at),
edit_ts, edit_ts,
json.dumps(get_message_mentions(message.mentions)), json.dumps(get_message_mentions(message.mentions)),
json.dumps(message.attachments), json.dumps(get_attachments_list(message.attachments)),
json.dumps(message.embeds) json.dumps(get_embeds_list(message.embeds))
) )
session.add(msg) session.add(msg)
session.commit() session.commit()
async def update_message(self, message): async def update_message(self, message):
if message.server: if message.guild:
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
msg = session.query(Messages) \ 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.channel_id == message.channel.id) \
.filter(Messages.message_id == message.id).first() .filter(Messages.message_id == message.id).first()
if msg: if msg:
msg.content = message.content msg.content = message.content
msg.timestamp = message.timestamp msg.timestamp = message.created_at
msg.edited_timestamp = message.edited_timestamp msg.edited_timestamp = message.edited_at
msg.mentions = json.dumps(get_message_mentions(message.mentions)) msg.mentions = json.dumps(get_message_mentions(message.mentions))
msg.attachments = json.dumps(message.attachments) msg.attachments = json.dumps(get_attachments_list(message.attachments))
msg.embeds = json.dumps(message.embeds) msg.embeds = json.dumps(get_embeds_list(message.embeds))
msg.author = json.dumps(get_message_author(message)) msg.author = json.dumps(get_message_author(message))
session.commit() session.commit()
async def delete_message(self, message): async def delete_message(self, message):
if message.server: if message.guild:
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
msg = session.query(Messages) \ 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.channel_id == int(message.channel.id)) \
.filter(Messages.message_id == int(message.id)).first() .filter(Messages.message_id == int(message.id)).first()
if msg: if msg:
@ -99,8 +99,11 @@ class DatabaseInterface(object):
session.commit() session.commit()
async def update_guild(self, guild): async def update_guild(self, guild):
if guild.me.server_permissions.manage_webhooks: if guild.me.guild_permissions.manage_webhooks:
server_webhooks = await self.bot.get_server_webhooks(guild) try:
server_webhooks = await guild.webhooks()
except:
server_webhooks = []
else: else:
server_webhooks = [] server_webhooks = []
async with threadpool(): async with threadpool():
@ -159,12 +162,12 @@ class DatabaseInterface(object):
async with threadpool(): async with threadpool():
with self.get_session() as session: with self.get_session() as session:
dbmember = session.query(GuildMembers) \ 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)) \ .filter(GuildMembers.user_id == int(member.id)) \
.order_by(GuildMembers.id).all() .order_by(GuildMembers.id).all()
if not dbmember: if not dbmember:
dbmember = GuildMembers( dbmember = GuildMembers(
int(member.server.id), int(member.guild.id),
int(member.id), int(member.id),
member.name, member.name,
member.discriminator, member.discriminator,

View File

@ -5,9 +5,9 @@ class DiscordBotsOrg(): # https://discordbots.org
self.url = "https://discordbots.org/api/bots/{}/stats".format(client_id) self.url = "https://discordbots.org/api/bots/{}/stats".format(client_id)
self.token = token self.token = token
async def post(self, count): async def post(self, count, shard_count, shard_id):
headers = {"Authorization": self.token} 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: async with aiohttp.ClientSession() as aioclient:
await aioclient.post(self.url, data=payload, headers=headers) 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.url = "https://bots.discord.pw/api/bots/{}/stats".format(client_id)
self.token = token self.token = token
async def post(self, count): async def post(self, count, shard_count, shard_id):
headers = {"Authorization": self.token} 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: async with aiohttp.ClientSession() as aioclient:
await aioclient.post(self.url, json=payload, headers=headers) await aioclient.post(self.url, json=payload, headers=headers)

View File

@ -1,5 +1,5 @@
import socketio 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 import time
from email import utils as emailutils 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 return emailutils.formatdate(time.mktime(datetimeobj.timetuple())) # https://stackoverflow.com/questions/3453177/convert-python-datetime-to-rfc-2822
def get_formatted_message(self, message): def get_formatted_message(self, message):
edit_ts = message.edited_timestamp edit_ts = message.edited_at
if not edit_ts: if not edit_ts:
edit_ts = None edit_ts = None
else: else:
edit_ts = self.format_datetime(edit_ts) edit_ts = self.format_datetime(edit_ts)
msg = { msg = {
"id": message.id, "id": str(message.id),
"channel_id": message.channel.id, "channel_id": str(message.channel.id),
"content": message.content, "content": message.content,
"author": get_message_author(message), "author": get_message_author(message),
"timestamp": self.format_datetime(message.timestamp), "timestamp": self.format_datetime(message.created_at),
"edited_timestamp": edit_ts, "edited_timestamp": edit_ts,
"mentions": get_message_mentions(message.mentions),
"attachments": message.attachments,
"embeds": message.embeds,
} }
nickname = None if hasattr(message, "mentions"):
if hasattr(message.author, 'nick') and message.author.nick: msg["mentions"] = get_message_mentions(message.mentions)
nickname = message.author.nick if hasattr(message, "attachments"):
msg["author"]["nickname"] = nickname msg["attachments"] = get_attachments_list(message.attachments)
for mention in msg["mentions"]: if hasattr(message, "embeds"):
mention["nickname"] = None msg["embeds"] = get_embeds_list(message.embeds)
member = message.server.get_member(mention["id"]) if hasattr(message, "author"):
if member: nickname = None
mention["nickname"] = member.nick 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 return msg
async def on_message(self, message): async def on_message(self, message):
if message.server: if message.guild:
msg = self.get_formatted_message(message) 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): async def on_message_delete(self, message):
if message.server: if message.guild:
msg = self.get_formatted_message(message) 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): async def on_message_update(self, message):
if message.server: if message.guild:
msg = self.get_formatted_message(message) 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): def get_formatted_user(self, user):
userobj = { userobj = {
@ -73,7 +78,7 @@ class SocketIOInterface:
# userobj["avatar_url"] = userobj["avatar_url"][:len(userobj["avatar_url"])-14] + ".jpg" # userobj["avatar_url"] = userobj["avatar_url"][:len(userobj["avatar_url"])-14] + ".jpg"
if user.nick: if user.nick:
userobj["nick"] = user.nick userobj["nick"] = user.nick
if user.game: if hasattr(user, "game"):
userobj["game"] = { userobj["game"] = {
"name": user.game.name "name": user.game.name
} }
@ -81,7 +86,7 @@ class SocketIOInterface:
for role in roles: for role in roles:
if role.hoist: if role.hoist:
userobj["hoist-role"] = { userobj["hoist-role"] = {
"id": role.id, "id": str(role.id),
"name": role.name, "name": role.name,
"position": role.position, "position": role.position,
} }
@ -90,21 +95,21 @@ class SocketIOInterface:
async def on_guild_member_add(self, member): async def on_guild_member_add(self, member):
user = self.get_formatted_user(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): async def on_guild_member_remove(self, member):
user = self.get_formatted_user(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): async def on_guild_member_update(self, member):
user = self.get_formatted_user(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): def get_formatted_emojis(self, emojis):
emotes = [] emotes = []
for emo in emojis: for emo in emojis:
emotes.append({ emotes.append({
"id": emo.id, "id": str(emo.id),
"managed": emo.managed, "managed": emo.managed,
"name": emo.name, "name": emo.name,
"require_colons": emo.require_colons, "require_colons": emo.require_colons,
@ -115,11 +120,11 @@ class SocketIOInterface:
async def on_guild_emojis_update(self, emojis): async def on_guild_emojis_update(self, emojis):
emotes = self.get_formatted_emojis(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): def get_formatted_guild(self, guild):
guil = { guil = {
"id": guild.id, "id": str(guild.id),
"name": guild.name, "name": guild.name,
"icon": guild.icon, "icon": guild.icon,
"icon_url": guild.icon_url, "icon_url": guild.icon_url,
@ -128,12 +133,12 @@ class SocketIOInterface:
async def on_guild_update(self, guild): async def on_guild_update(self, guild):
guildobj = self.get_formatted_guild(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): def get_formatted_channel(self, channel):
chan = { chan = {
"id": channel.id, "id": str(channel.id),
"guild_id": channel.server.id, "guild_id": str(channel.guild.id),
} }
return chan return chan
@ -141,24 +146,24 @@ class SocketIOInterface:
if str(channel.type) != "text": if str(channel.type) != "text":
return return
chan = self.get_formatted_channel(channel) 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): async def on_channel_create(self, channel):
if str(channel.type) != "text": if str(channel.type) != "text":
return return
chan = self.get_formatted_channel(channel) 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): async def on_channel_update(self, channel):
if str(channel.type) not in ["text", "category"]: if str(channel.type) not in ["text", "category"]:
return return
chan = self.get_formatted_channel(channel) 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): def get_formatted_role(self, role):
rol = { rol = {
"id": role.id, "id": str(role.id),
"guild_id": role.server.id, "guild_id": str(role.guild.id),
"name": role.name, "name": role.name,
"color": role.color.value, "color": role.color.value,
"hoist": role.hoist, "hoist": role.hoist,
@ -169,12 +174,12 @@ class SocketIOInterface:
async def on_guild_role_create(self, role): async def on_guild_role_create(self, role):
rol = self.get_formatted_role(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): async def on_guild_role_update(self, role):
rol = self.get_formatted_role(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): async def on_guild_role_delete(self, role):
rol = self.get_formatted_role(role) rol = self.get_formatted_role(role)
await self.io.emit('GUILD_ROLE_DELETE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') await self.io.emit('GUILD_ROLE_DELETE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway')

View File

@ -1,12 +1,14 @@
import discord import discord
def get_message_author(message): def get_message_author(message):
if not hasattr(message, "author"):
return {}
author = message.author author = message.author
obj = { obj = {
"username": author.name, "username": author.name,
"discriminator": author.discriminator, "discriminator": author.discriminator,
"bot": author.bot, "bot": author.bot,
"id": author.id, "id": str(author.id),
"avatar": author.avatar "avatar": author.avatar
} }
return obj return obj
@ -18,7 +20,7 @@ def get_message_mentions(mentions):
"username": author.name, "username": author.name,
"discriminator": author.discriminator, "discriminator": author.discriminator,
"bot": author.bot, "bot": author.bot,
"id": author.id, "id": str(author.id),
"avatar": author.avatar "avatar": author.avatar
}) })
return ments return ments
@ -27,9 +29,9 @@ def get_webhooks_list(guild_webhooks):
webhooks = [] webhooks = []
for webhook in guild_webhooks: for webhook in guild_webhooks:
webhooks.append({ webhooks.append({
"id": webhook.id, "id": str(webhook.id),
"guild_id": webhook.server.id, "guild_id": str(webhook.guild.id),
"channel_id": webhook.channel.id, "channel_id": str(webhook.channel.id),
"name": webhook.name, "name": webhook.name,
"token": webhook.token, "token": webhook.token,
}) })
@ -39,7 +41,7 @@ def get_emojis_list(guildemojis):
emojis = [] emojis = []
for emote in guildemojis: for emote in guildemojis:
emojis.append({ emojis.append({
"id": emote.id, "id": str(emote.id),
"name": emote.name, "name": emote.name,
"require_colons": emote.require_colons, "require_colons": emote.require_colons,
"managed": emote.managed, "managed": emote.managed,
@ -53,7 +55,7 @@ def get_roles_list(guildroles):
roles = [] roles = []
for role in guildroles: for role in guildroles:
roles.append({ roles.append({
"id": role.id, "id": str(role.id),
"name": role.name, "name": role.name,
"color": role.color.value, "color": role.color.value,
"hoist": role.hoist, "hoist": role.hoist,
@ -65,8 +67,9 @@ def get_roles_list(guildroles):
def get_channels_list(guildchannels): def get_channels_list(guildchannels):
channels = [] channels = []
for channel in guildchannels: for channel in guildchannels:
if str(channel.type) in ["text", "category"]: if isinstance(channel, discord.channel.TextChannel) or isinstance(channel, discord.channel.CategoryChannel):
overwrites = [] overwrites = []
isTextChannel = isinstance(channel, discord.channel.TextChannel)
for target, overwrite in channel.overwrites: for target, overwrite in channel.overwrites:
if isinstance(target, discord.Role): if isinstance(target, discord.Role):
type = "role" type = "role"
@ -76,28 +79,51 @@ def get_channels_list(guildchannels):
allow = allow.value allow = allow.value
deny = deny.value deny = deny.value
overwrites.append({ overwrites.append({
"id": target.id, "id": str(target.id),
"type": type, "type": type,
"allow": allow, "allow": allow,
"deny": deny, "deny": deny,
}) })
parent = channel.parent parent = channel.category
if parent: if parent:
parent = parent.id parent = str(parent.id)
channels.append({ channels.append({
"id": channel.id, "id": str(channel.id),
"name": channel.name, "name": channel.name,
"topic": channel.topic, "topic": channel.topic if isTextChannel else None,
"position": channel.position, "position": channel.position,
"type": str(channel.type), "type": "text" if isTextChannel else "category",
"permission_overwrites": overwrites, "permission_overwrites": overwrites,
"parent_id": parent, "parent_id": parent,
"nsfw": channel.is_nsfw, "nsfw": channel.is_nsfw(),
}) })
return channels return channels
def list_role_ids(usr_roles): def list_role_ids(usr_roles):
ids = [] ids = []
for role in usr_roles: for role in usr_roles:
ids.append(role.id) ids.append(str(role.id))
return ids 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

View File

@ -5,7 +5,7 @@ requests_oauthlib
Flask-SSLify Flask-SSLify
flask_socketio flask_socketio
paypalrestsdk paypalrestsdk
git+https://github.com/TitanEmbeds/discord.py git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py
asyncio_extras asyncio_extras
kombu kombu
redis redis