Cleanup db script to not depend on Discord.py so it could be ran in the background

This commit is contained in:
Jeremy Zhang 2018-06-28 05:29:52 +00:00
parent 5cf32ffb0c
commit 4cd48e75d7

View File

@ -1,8 +1,5 @@
from config import config from config import config
from titanembeds.database import DatabaseInterface, Guilds, Messages from titanembeds.database import DatabaseInterface, Guilds, Messages
from titanembeds.commands import Commands
import discord
import aiohttp
import asyncio import asyncio
import sys import sys
import logging import logging
@ -21,13 +18,11 @@ logging.getLogger('sqlalchemy')
# messages store # # messages store #
########################### ###########################
class Titan(discord.AutoShardedClient): class TitanCleanupDB:
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.aiosession = aiohttp.ClientSession(loop=self.loop) self.loop = asyncio.get_event_loop()
self.http.user_agent += ' TitanEmbeds-Bot'
self.database = DatabaseInterface(self) self.database = DatabaseInterface(self)
self.command = Commands(self, self.database)
self.logger = logging.getLogger("titan_cleanupdb") self.logger = logging.getLogger("titan_cleanupdb")
self.logger.setLevel(logging.DEBUG) self.logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("titan_cleanupdb.log") fh = logging.FileHandler("titan_cleanupdb.log")
@ -57,9 +52,9 @@ class Titan(discord.AutoShardedClient):
def run(self): def run(self):
try: try:
self.loop.run_until_complete(self.start(config["bot-token"])) self.loop.run_until_complete(self.on_ready())
except discord.errors.LoginFailure: except Exception as e:
print("Invalid bot token in config!") print("Error!", e)
finally: finally:
try: try:
self._cleanup() self._cleanup()
@ -69,20 +64,13 @@ class Titan(discord.AutoShardedClient):
async def on_ready(self): async def on_ready(self):
print('Titan [DiscordBot] [UTILITY: Cleanup database messages]') print('Titan [DiscordBot] [UTILITY: Cleanup database messages]')
print('Logged in as the following user:')
print(self.user.name)
print(self.user.id)
print('------') print('------')
game = discord.Game(name="Titan is currently down for database maintenances. Bookmark https://TitanEmbeds.com/ for later access to our services!")
await self.change_presence(status=discord.Status.do_not_disturb, activity=game)
try: try:
self.database.connect(config["database-uri"]) self.database.connect(config["database-uri"])
except Exception: except Exception:
self.logger.error("Unable to connect to specified database!") self.logger.error("Unable to connect to specified database!")
traceback.print_exc() traceback.print_exc()
await self.logout()
return return
print("working on this...") print("working on this...")
@ -117,7 +105,7 @@ class Titan(discord.AutoShardedClient):
def main(): def main():
print("Starting...") print("Starting...")
te = Titan() te = TitanCleanupDB()
te.run() te.run()
gc.collect() gc.collect()