Populate msgs on server join and remove on leave

This commit is contained in:
Jeremy Zhang 2017-05-08 20:19:45 -07:00
parent 4bb559420f
commit a621fa8b0a
2 changed files with 9 additions and 0 deletions

View File

@ -84,6 +84,9 @@ class Titan(discord.Client):
async def on_server_join(self, guild): async def on_server_join(self, guild):
await self.database.update_guild(guild) await self.database.update_guild(guild)
for channel in guild.channels:
async for message in self.logs_from(channel, limit=50, reverse=True):
await self.database.push_message(message)
async def on_server_remove(self, guild): async def on_server_remove(self, guild):
await self.database.remove_guild(guild) await self.database.remove_guild(guild)

View File

@ -188,6 +188,9 @@ class DatabaseInterface(object):
disguild = discord.utils.get(guilds, id=guild.guild_id) disguild = discord.utils.get(guilds, id=guild.guild_id)
if not disguild: if not disguild:
changed = True changed = True
dbmsgs = session.query(Messages).filter(Messages.guild_id == guild.guild_id).all()
for msg in dbmsgs:
session.delete(msg)
session.delete(guild) session.delete(guild)
if changed: if changed:
session.commit() session.commit()
@ -197,6 +200,9 @@ class DatabaseInterface(object):
with self.get_session() as session: with self.get_session() as session:
gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first() gui = session.query(Guilds).filter(Guilds.guild_id == guild.id).first()
if gui: if gui:
dbmsgs = session.query(Messages).filter(Messages.guild_id == guild.id).all()
for msg in dbmsgs:
session.delete(msg)
session.delete(gui) session.delete(gui)
session.commit() session.commit()