Ver Fonte

DRY-yfied code, wan wsgi settings

Double-Vee há 3 anos atrás
pai
commit
1fbbfe1601

+ 6 - 3
bot/commands/admin.py

@@ -1,6 +1,7 @@
 #import discord
 import secrets
 from discord.ext import commands
+import local_settings as settings
 
 
 class Admin(commands.Cog):
@@ -14,7 +15,9 @@ class Admin(commands.Cog):
 		brief="Set channel specific settings via the webgui",
 		help="Sends a single-use time based token to the webportal"
 	)
-	async def channelsettings(self, ctx: commands.Context, *, text: str):
+	async def chanset(self, ctx: commands.Context, *, text: str):
 		pass
-		temp_key = secrets.token_urlsafe(40)[:40]
-		#await self.bot.change_presence(activity=discord.Game(name=text))
+		token = secrets.token_urlsafe(40)[:40]
+		# Check for active token
+		# Upsert token 
+		#await "%s://%s/foo/%s", setting.WEB_SCHEME, setting.WEB_HOST, token

+ 0 - 3
bot/events/general.py

@@ -32,14 +32,11 @@ class General(commands.Cog):
 	@commands.Cog.listener()
 	async def on_message(self, message: discord.Message):
 		# ActiveRPG
-		#await self.bot.pg.execute("INSERT INTO \"user\"(user_id) VALUES($1) ON CONFLICT DO NOTHING", message.author.id)
 		await create_user(self.bot.pg, message.author.id)
 		try:
-			#await self.bot.pg.execute("INSERT INTO channel_user(channel, \"user\") VALUES($1, $2) ON CONFLICT ON CONSTRAINT channel_user_channel_user_key DO UPDATE SET total_messages=channel_user.total_messages+1", message.channel.id, message.author.id)
 			await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
 		except asyncpg.exceptions.ForeignKeyViolationError:
 			try:
-				#await self.bot.pg.execute("INSERT INTO channel(channel_id, guild) VALUES($1, $2)", message.channel.id, message.guild.id)
 				await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
 			except asyncpg.exceptions.ForeignKeyViolationError:
 				await update_guild(self.bot.pg, message.guild)

+ 3 - 0
bot/local_settings_example.py

@@ -8,5 +8,8 @@ DATABASE_USER = ""
 DATABASE_HOST = "127.0.0.1"
 DATABASE_PASSWORD = ""
 
+WEB_HOST = ""
+WEB_SCHEME = "https"
+
 DISCORD_TOKEN = ""
 COMMAND_PREFIX = ""

+ 28 - 9
bot/main.py

@@ -1,6 +1,15 @@
 import logging, os
 
 from os.path import exists
+
+# Hint how to edit the settings and quit
+def hint_quit():
+    logging.info("")
+    logging.info("    edit local_settings.py")
+    logging.info("")
+    quit()
+
+# Copy or create settings file if missing
 def missing_config():
     logging.basicConfig(level=logging.DEBUG)
     if not exists("local_settings.py"):
@@ -21,16 +30,21 @@ def missing_config():
                         "DATABASE_HOST = \"\"",
                         "DATABASE_PASSWORD = \"\"",
                         "",
+                        "WEB_HOST = \"\"",
+                        "WEB_SCHEME = \"\"",
+                        "",
                         "DISCORD_TOKEN = \"\"",
                         "COMMAND_PREFIX = \"\"",
                     ]
                 )
     logging.error("Settings undefined.")
     logging.info("Configure the settings:")
-    logging.info("")
-    logging.info("    edit local_settings.py")
-    logging.info("")
-    quit()
+    hint_quit()
+
+# Hint to correct specific setting and quit
+def correct_setting(setting):
+    logging.info("Correct the %s in local_settings.py", setting)
+    hint_quit()
 
 # Import settings
 try:
@@ -38,6 +52,15 @@ try:
 except ModuleNotFoundError:
     missing_config()
 
+# Check additional settings
+if not settings.WEB_HOST:
+    logging.error("Web host undefinded.")
+    correct_setting("WEB_HOST")
+    
+if not settings.WEB_SCHEME:
+    logging.error("Web scheme undefinded.")
+    correct_setting("WEB_SCHEME")
+
 # Set loglevel
 try:
     logging.basicConfig(level=settings.LOG_LEVEL)
@@ -93,8 +116,4 @@ except AttributeError:
     missing_config()
 except discord.errors.LoginFailure:
     logging.error("Invalid discord token.")
-    logging.info("Correct the DISCORD_TOKEN in local_settings.py")
-    logging.info("")
-    logging.info("    edit local_settings.py")
-    logging.info("")
-    quit()
+    correct_setting("DISCORD_TOKEN")

+ 2 - 0
bot/query/access_token.py

@@ -0,0 +1,2 @@
+#async def insert_channel(pg,):
+#	await pg.execute("INSERT INTO access_token(channel_id, guild) VALUES($1, $2)", channel_id, guild_id)

+ 1 - 17
bot/query/initialise_database.py

@@ -8,20 +8,4 @@ async def init_db(pg):
         "CREATE TABLE IF NOT EXISTS channel_user (id SERIAL PRIMARY KEY, channel BIGINT NOT NULL REFERENCES channel (channel_id), \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), total_messages BIGINT DEFAULT 1, UNIQUE (channel, \"user\"))",
     ]
     for query in queries:
-        await pg.execute(query)
-
-    # await pg.execute(
-    #     "CREATE TABLE IF NOT EXISTS guild (id SERIAL PRIMARY KEY, guild_id BIGINT UNIQUE NOT NULL)"
-    # )
-    # await pg.execute(
-    #     "CREATE TABLE IF NOT EXISTS channel (id SERIAL PRIMARY KEY, channel_id BIGINT UNIQUE NOT NULL, guild BIGINT REFERENCES guild (guild_id))"
-    # )
-    # await pg.execute(
-    #     "CREATE TABLE IF NOT EXISTS channel_settings (id SERIAL PRIMARY KEY, channel BIGINT UNIQUE NOT NULL REFERENCES channel (channel_id), guild BIGINT REFERENCES guild (guild_id), interact BOOL DEFAULT FALSE)"
-    # )
-    # await pg.execute(
-    #     "CREATE TABLE IF NOT EXISTS \"user\" (id SERIAL PRIMARY KEY, user_id BIGINT UNIQUE NOT NULL)"
-    # )
-    # await pg.execute(
-    #     "CREATE TABLE IF NOT EXISTS channel_user (id SERIAL PRIMARY KEY, channel BIGINT NOT NULL REFERENCES channel (channel_id), \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), total_messages BIGINT DEFAULT 1, UNIQUE (channel, \"user\"))"
-    # )
+        await pg.execute(query)