소스 검색

-webgui | +database checks

root 2 년 전
부모
커밋
aba483a584
3개의 변경된 파일29개의 추가작업 그리고 5개의 파일을 삭제
  1. 1 1
      bot/events/angels.py
  2. 22 3
      bot/main.py
  3. 6 1
      bot/query/initialise_database.py

+ 1 - 1
bot/events/angels.py

@@ -5,7 +5,7 @@ async def setup(bot: commands.Bot):
 	await bot.add_cog(AngelEvents(bot))
 
 class AngelEvents(commands.Cog):
-	"""A couple of simple commands."""
+	"""A couple Angel specific commands."""
 
 	def __init__(self, bot: commands.Bot):
 		self.bot = bot

+ 22 - 3
bot/main.py

@@ -10,6 +10,13 @@ def hint_quit():  # Hint how to edit the settings and quit
 	logging.info("")
 	quit()
 
+def sql_db_does_not_exis():
+	logging.error("Database does not exist. Doublecheck if it has been created, and the user has access.")
+	quit()
+
+def sql_authentication_error():
+	logging.error("Database autentication failed. Doublecheck username & password, and if the user has been created.")
+	quit()
 
 def missing_config():  # Copy or create settings file if missing
 	logging.basicConfig(level=logging.DEBUG)
@@ -105,13 +112,22 @@ async def main():
 			)
 		except AttributeError:
 			missing_config()
+		except asyncpg.exceptions.InvalidPasswordError:
+			sql_authentication_error()
+		except asyncpg.exceptions.InvalidCatalogNameError:
+			sql_db_does_not_exis()
 
 	# Create database pool
 	await create_db_pool()
 
 	# Create database tables if they do not exist
-	from query.initialise_database import init_db
-	await init_db(bot.pg)
+	from query.initialise_database import init_db, check_db
+	try:
+		await check_db(bot.pg)
+	except asyncpg.exceptions.UndefinedTableError:
+		logging.info("Guild table does not exists, assuming empty database. Populating database...")
+		await init_db(bot.pg)
+
 	await bot.start(settings.DISCORD_TOKEN)
 
 
@@ -122,4 +138,7 @@ except AttributeError:
 	missing_config()
 except discord.errors.LoginFailure:
 	logging.error("Invalid discord token.")
-	correct_setting("DISCORD_TOKEN")
+	correct_setting("DISCORD_TOKEN")
+except KeyboardInterrupt:
+	logging.info("Received keyboard interrupt, exiting...")
+	quit()

+ 6 - 1
bot/query/initialise_database.py

@@ -1,10 +1,12 @@
+async def check_db(pg):
+    await pg.fetchval("SELECT * FROM guild")
+
 async def init_db(pg):
     queries = [
         "CREATE TABLE IF NOT EXISTS \
             guild (\
                 id SERIAL PRIMARY KEY, \
                 guild_id BIGINT UNIQUE NOT NULL, \
-                output_channel BIGINT REFERENCES channel (channel_id), \
                 report_deleted BOOL DEFAULT FALSE, \
                 report_edited BOOL DEFAULT FALSE\
             )\
@@ -18,6 +20,9 @@ async def init_db(pg):
                 games BOOL DEFAULT FALSE\
             )\
         ",
+        "ALTER TABLE guild \
+            ADD COLUMN output_channel BIGINT REFERENCES channel (channel_id) \
+        ",
         "CREATE TABLE IF NOT EXISTS \
             settings (\
                 id SERIAL PRIMARY KEY, \