Quellcode durchsuchen

Readability, asynchio types fix, access_token progress

Double-Vee vor 3 Jahren
Ursprung
Commit
a2af721c13

+ 0 - 4
bot/commands/admin.py

@@ -20,11 +20,7 @@ class Admin(commands.Cog):
 		help="Sends a single-use time based token to the webportal"
 	)
 	async def chanset(self, ctx: commands.Context):
-		
-
-		#id, token, user_id, created = get_active_token(self.bot.pg, ctx.guild.id)
 		record = await get_active_token(self.bot.pg, ctx.guild.id)
-		print(record)
 
 		if record:	# Check for active token
 			await ctx.send(f"Token {record['id']} is in use by {record['user']} until {plus10min(record['created'])}.")

+ 22 - 10
bot/events/general.py

@@ -5,6 +5,7 @@ from query.channel import insert_channel
 from query.channel_settings import get_interact
 from query.channel_user import upsert_total_messages
 from query.user import create_user
+from query.settings import get_crew_channel
 
 def setup(bot: commands.Bot):
 	bot.add_cog(General(bot))
@@ -29,20 +30,31 @@ class General(commands.Cog):
 	async def on_message_delete(self, message: discord.Message):
 		self.last_msg = message
 
+	@commands.Cog.listener()
+	async def on_raw_member_remove(self, payload):
+		channel = bot.get_channel(get_crew_channel(self.pg))
+		if channel:
+			await channel.send(f"{payload.user} has left {payload.guild_id}")
+
 	@commands.Cog.listener()
 	async def on_message(self, message: discord.Message):
-		# ActiveRPG
-		await create_user(self.bot.pg, message.author.id)
-		try:
-			await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
-		except asyncpg.exceptions.ForeignKeyViolationError:
+		## ActiveRPG
+		
+		# Create user, if not exists
+		await create_user(self.bot.pg, message.author.id)	
+		
+		# Count messages
+		if message.guild:	# Ignore DM's
 			try:
-				print(message)
-				print(message.channel)
-				print(message.guild)
-				await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
+				await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
 			except asyncpg.exceptions.ForeignKeyViolationError:
-				await update_guild(self.bot.pg, message.guild.id)
+				try:
+					print(message)
+					print(message.channel)
+					print(message.guild)
+					await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
+				except asyncpg.exceptions.ForeignKeyViolationError:
+					await update_guild(self.bot.pg, message.guild)
 		
 		# Do not respond to one self.
 		if self.bot.user == message.author:

+ 2 - 0
bot/main.py

@@ -91,6 +91,7 @@ try:
 		case_insensitive = True,
 	)
 except AttributeError:
+	print("Attribute error on create.")
 	missing_config()
 
 # Create database pool
@@ -114,6 +115,7 @@ for ext in default_extensions:
 try:
 	bot.run(settings.DISCORD_TOKEN)
 except AttributeError:
+	print("Attribute error on run.")
 	missing_config()
 except discord.errors.LoginFailure:
 	logging.error("Invalid discord token.")

+ 4 - 2
bot/query/guild_access_token.py

@@ -6,10 +6,12 @@ async def get_active_token(pg, guild_id):
 	return await pg.fetchrow("SELECT * FROM guild_access_token WHERE guild=$1 AND created > $2", guild_id, min10min(datetime.datetime.now()))
 
 async def upsert_token(pg, guild_id, user_id, token):
-	print(type(token))
 	print(token)
+	print("INSERT INTO guild_access_token(guild, \"user\", token) VALUES($1, $2, $3)", guild_id, user_id, token)
+
 	try:
-		await pg.execute("INSERT INTO guild_access_token(guild, \"user\", token) VALUES($1, $2, $3)", guild_id, user_id, token).bindparams(bindparam("token", type_=String))
+		await pg.execute("INSERT INTO guild_access_token(guild, \"user\", token) VALUES($1, $2, $3)", guild_id, user_id, [token])
+		
 		# ON CONFLICT() DO UPDATE SET \"user\"=$1 AND token=$2", user_id, token)
 	except asyncpg.exceptions.ForeignKeyViolationError:
 		await update_guild(self.bot.pg, message.guild)

+ 51 - 6
bot/query/initialise_database.py

@@ -1,11 +1,56 @@
 async def init_db(pg):
     queries = [
-        "CREATE TABLE IF NOT EXISTS guild (id SERIAL PRIMARY KEY, guild_id BIGINT UNIQUE NOT NULL)",
-        "CREATE TABLE IF NOT EXISTS channel (id SERIAL PRIMARY KEY, channel_id BIGINT UNIQUE NOT NULL, guild BIGINT REFERENCES guild (guild_id))",
-        "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)",
-        "CREATE TABLE IF NOT EXISTS \"user\" (id SERIAL PRIMARY KEY, user_id BIGINT UNIQUE NOT NULL)",
-        "CREATE TABLE IF NOT EXISTS guild_access_token (id SERIAL PRIMARY KEY, guild BIGINT REFERENCES guild (guild_id), \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), token varchar[40] UNIQUE NOT NULL, created TIMESTAMP NOT NULL DEFAULT now())",
-        "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\"))",
+        "CREATE TABLE IF NOT EXISTS \
+            guild (\
+                id SERIAL PRIMARY KEY, \
+                guild_id BIGINT UNIQUE NOT NULL \
+            )\
+        ",
+        "CREATE TABLE IF NOT EXISTS \
+            channel (\
+                id SERIAL PRIMARY KEY, \
+                channel_id BIGINT UNIQUE NOT NULL, \
+                guild BIGINT REFERENCES guild (guild_id)\
+            )\
+        ",
+        "CREATE TABLE IF NOT EXISTS \
+            settings (\
+                id SERIAL PRIMARY KEY, \
+                crew_channel_id BIGINT UNIQUE NOT NULL\
+            )\
+        ",
+        "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\
+            )\
+        ",
+        "CREATE TABLE IF NOT EXISTS \
+            \"user\" (\
+                id SERIAL PRIMARY KEY, \
+                user_id BIGINT UNIQUE NOT NULL\
+            )\
+        ",
+        "CREATE TABLE IF NOT EXISTS \
+            guild_access_token (\
+                id SERIAL PRIMARY KEY, \
+                guild BIGINT REFERENCES guild (guild_id), \
+                \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), \
+                token varchar[40] UNIQUE NOT NULL, \
+                created TIMESTAMP NOT NULL DEFAULT now()\
+            )\
+        ",
+        "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)

+ 2 - 0
bot/query/settings.py

@@ -0,0 +1,2 @@
+async def get_crew_channel(pg):
+	await pg.fetchrow("SELECT crew_channel FROM settings WHERE id=1")