Double-Vee %!s(int64=3) %!d(string=hai) anos
pai
achega
9cee279d49

+ 0 - 8
bot/commands/general.py

@@ -67,11 +67,3 @@ class General(commands.Cog):
 		await self.bot.change_presence(activity=discord.Game(name=text))
 
 
-	@commands.command(
-		description="Modify channel settings",
-		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):
-		pass
-		#await self.bot.change_presence(activity=discord.Game(name=text))

+ 17 - 8
bot/events/general.py

@@ -1,6 +1,10 @@
 import logging, discord, asyncpg, random
 from discord.ext import commands
 from query.guild import update_guild
+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
 
 def setup(bot: commands.Bot):
 	bot.add_cog(General(bot))
@@ -12,13 +16,14 @@ class General(commands.Cog):
 		self.bot = bot
 		self.last_msg = None
 
+
 	@commands.Cog.listener()
 	async def on_ready(self):
 		logging.info("Logged in as %s - %i", self.bot.user.name, self.bot.user.id)
 
 	@commands.Cog.listener()
 	async def on_guild_join(self, guild: discord.Guild):
-		update_guild(guild)
+		await update_guild(self.bot.pg, guild.id)
 
 	@commands.Cog.listener()
 	async def on_message_delete(self, message: discord.Message):
@@ -27,14 +32,17 @@ 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 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 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 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:
-				update_guild(message.guild.id)
+				await update_guild(self.bot.pg, message.guild)
 		
 		# Do not respond to one self.
 		if self.bot.user == message.author:
@@ -44,9 +52,10 @@ class General(commands.Cog):
 		if self.bot.user.mentioned_in(message):
 			print("mentioned in ")
 			print(message.channel)
-			interact = await self.bot.pg.fetchrow("SELECT interact FROM channel_settings WHERE channel_id=$1::bigint", message.channel.id)
+			#interact = get_interact
 			print(interact)
-			if interact:
+			#if interact:
+			if get_interact(self.bot.pg, message.channel.id):
 				messages = [
 					f"Hello {message.author.mention}. <3",
 					f"How are you today {message.author.mention}?",
@@ -83,4 +92,4 @@ class General(commands.Cog):
 		content = self.last_msg.content
 
 		embed = discord.Embed(title=f"Message from {author}", description=content)
-		await ctx.send(embed=embed)
+		await ctx.send(embed=embed)

+ 1 - 1
bot/main.py

@@ -81,7 +81,7 @@ bot.loop.run_until_complete(init_db(bot.pg))
 default_extensions = [
   "commands.general",
   "commands.games",
-  "events.general"
+  "events.general",
 ]
 for ext in default_extensions:
   bot.load_extension(ext)

+ 2 - 0
bot/query/channel.py

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

+ 2 - 0
bot/query/channel_settings.py

@@ -0,0 +1,2 @@
+async def get_interact(pg, channel_id):
+	await pg.fetchrow("SELECT interact FROM channel_settings WHERE channel_id=$1::bigint", channel_id)

+ 2 - 0
bot/query/channel_user.py

@@ -0,0 +1,2 @@
+async def upsert_total_messages(pg, channel_id, user_id):
+	await 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", channel_id, user_id)

+ 3 - 3
bot/query/guild.py

@@ -1,4 +1,4 @@
-async def update_guild(guild):
-	await bot.pg.execute("INSERT INTO guild(guild_id) VALUES($1) ON CONFLICT DO NOTHING", guild.id)
+async def update_guild(pg, guild):
+	await pg.execute("INSERT INTO guild(guild_id) VALUES($1) ON CONFLICT DO NOTHING", guild.id)
 	for chan in guild.text_channels:
-		await self.bot.pg.execute("INSERT INTO channel(channel_id, guild) ON CONFLICT DO NOTHING", chan.id, guild.id)
+		await pg.execute("INSERT INTO channel(channel_id, guild) VALUES($1, $2) ON CONFLICT DO NOTHING", chan.id, guild.id)

+ 26 - 15
bot/query/initialise_database.py

@@ -1,16 +1,27 @@
 async def init_db(pg):
-    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\"))"
-    )
+    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 access_token (id SERIAL PRIMARY KEY, guild BIGINT REFERENCES guild (guild_id), \"user\" BIGINT NOT NULL REFERENCES \"user\" (user_id), token char[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)
+
+    # 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\"))"
+    # )

+ 2 - 0
bot/query/user.py

@@ -0,0 +1,2 @@
+async def create_user(pg, user_id):
+	await pg.execute("INSERT INTO \"user\"(user_id) VALUES($1) ON CONFLICT DO NOTHING", user_id)