Delete inactive channel messages and better print stmts

This commit is contained in:
Jeremy Zhang 2017-09-22 01:18:30 +00:00
parent 1ac5ac3eeb
commit 91f926bfe3

View File

@ -7,6 +7,7 @@ import asyncio
import sys import sys
import logging import logging
import json import json
import gc
from asyncio_extras import threadpool from asyncio_extras import threadpool
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')
logging.getLogger('TitanBot') logging.getLogger('TitanBot')
@ -72,17 +73,25 @@ class Titan(discord.Client):
async with threadpool(): async with threadpool():
with self.database.get_session() as session: with self.database.get_session() as session:
guilds = session.query(Guilds).all() guilds = session.query(Guilds).all()
count = 0
for guild in guilds: for guild in guilds:
print("id-{} snowflake-{} name-{}".format(guild.id, guild.guild_id, guild.name)) count += 1
print("[{}] id-{} snowflake-{} name-{}".format(count, guild.id, guild.guild_id, guild.name))
try: try:
channelsjson = json.loads(guild.channels) channelsjson = json.loads(guild.channels)
except: except:
continue continue
active_channels = []
for channel in channelsjson: for channel in channelsjson:
chanid = channel["id"] chanid = channel["id"]
active_channels.append(chanid)
keep_these = session.query(Messages.id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50) keep_these = session.query(Messages.id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50)
session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False) d = session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False)
session.commit() session.commit()
print(" --{} [{}]".format(channel["name"], d))
d = session.query(Messages).filter(~Messages.channel_id.in_(active_channels)).delete(synchronize_session=False)
session.commit()
print(" INACTIVE {}".format(d))
print("done!") print("done!")
await self.logout() await self.logout()