2017-06-25 00:02:34 +02:00
|
|
|
from config import config
|
|
|
|
from titanembeds.database import DatabaseInterface, Guilds, Messages
|
|
|
|
from titanembeds.commands import Commands
|
|
|
|
import discord
|
|
|
|
import aiohttp
|
|
|
|
import asyncio
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import json
|
2017-09-22 03:18:30 +02:00
|
|
|
import gc
|
2017-06-25 00:02:34 +02:00
|
|
|
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.getLogger('TitanBot')
|
|
|
|
logging.getLogger('sqlalchemy')
|
|
|
|
|
|
|
|
###########################
|
|
|
|
# Cleanup DB Messages #
|
|
|
|
# #
|
|
|
|
# Cleans the database #
|
|
|
|
# messages store #
|
|
|
|
###########################
|
|
|
|
|
|
|
|
class Titan(discord.Client):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.aiosession = aiohttp.ClientSession(loop=self.loop)
|
|
|
|
self.http.user_agent += ' TitanEmbeds-Bot'
|
|
|
|
self.database = DatabaseInterface(self)
|
|
|
|
self.command = Commands(self, self.database)
|
|
|
|
|
|
|
|
def _cleanup(self):
|
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(self.logout())
|
|
|
|
except: # Can be ignored
|
|
|
|
pass
|
|
|
|
pending = asyncio.Task.all_tasks()
|
|
|
|
gathered = asyncio.gather(*pending)
|
|
|
|
try:
|
|
|
|
gathered.cancel()
|
|
|
|
self.loop.run_until_complete(gathered)
|
|
|
|
gathered.exception()
|
|
|
|
except: # Can be ignored
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
self.loop.run_until_complete(self.start(config["bot-token"]))
|
|
|
|
except discord.errors.LoginFailure:
|
|
|
|
print("Invalid bot token in config!")
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self._cleanup()
|
|
|
|
except Exception as e:
|
|
|
|
print("Error in cleanup:", e)
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
async def on_ready(self):
|
|
|
|
print('Titan [DiscordBot] [UTILITY: Cleanup database messages]')
|
|
|
|
print('Logged in as the following user:')
|
|
|
|
print(self.user.name)
|
|
|
|
print(self.user.id)
|
|
|
|
print('------')
|
|
|
|
|
|
|
|
try:
|
2017-09-09 06:20:39 +02:00
|
|
|
await self.database.connect(config["database-uri"])
|
2017-06-25 00:02:34 +02:00
|
|
|
except Exception:
|
|
|
|
self.logger.error("Unable to connect to specified database!")
|
|
|
|
traceback.print_exc()
|
|
|
|
await self.logout()
|
|
|
|
return
|
|
|
|
|
|
|
|
print("working on this...")
|
|
|
|
async with threadpool():
|
|
|
|
with self.database.get_session() as session:
|
|
|
|
guilds = session.query(Guilds).all()
|
2017-09-22 03:18:30 +02:00
|
|
|
count = 0
|
2017-06-25 00:02:34 +02:00
|
|
|
for guild in guilds:
|
2017-09-22 03:18:30 +02:00
|
|
|
count += 1
|
|
|
|
print("[{}] id-{} snowflake-{} name-{}".format(count, guild.id, guild.guild_id, guild.name))
|
2017-06-25 00:02:34 +02:00
|
|
|
try:
|
|
|
|
channelsjson = json.loads(guild.channels)
|
|
|
|
except:
|
|
|
|
continue
|
2017-09-22 03:18:30 +02:00
|
|
|
active_channels = []
|
2017-06-25 00:02:34 +02:00
|
|
|
for channel in channelsjson:
|
|
|
|
chanid = channel["id"]
|
2017-09-22 03:18:30 +02:00
|
|
|
active_channels.append(chanid)
|
2017-09-22 02:53:59 +02:00
|
|
|
keep_these = session.query(Messages.id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50)
|
2017-09-22 03:18:30 +02:00
|
|
|
d = session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False)
|
2017-09-22 02:53:59 +02:00
|
|
|
session.commit()
|
2017-09-22 03:18:30 +02:00
|
|
|
print(" --{} [{}]".format(channel["name"], d))
|
2017-09-22 03:33:55 +02:00
|
|
|
d = session.query(Messages).filter(Messages.guild_id == guild.id, ~Messages.channel_id.in_(active_channels)).delete(synchronize_session=False)
|
2017-09-22 03:18:30 +02:00
|
|
|
session.commit()
|
|
|
|
print(" INACTIVE {}".format(d))
|
2017-06-25 00:02:34 +02:00
|
|
|
print("done!")
|
|
|
|
await self.logout()
|
|
|
|
|
|
|
|
def main():
|
|
|
|
print("Starting...")
|
|
|
|
te = Titan()
|
|
|
|
te.run()
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|