Przeglądaj źródła

Added role permissions

Penta 5 lat temu
rodzic
commit
2abc7bc0d8
3 zmienionych plików z 61 dodań i 12 usunięć
  1. 33 12
      myanimebot.py
  2. 20 0
      myanimebot/discord.py
  3. 8 0
      myanimebot/utils.py

+ 33 - 12
myanimebot.py

@@ -24,7 +24,7 @@ import myanimebot.anilist as anilist
 import myanimebot.globals as globals
 import myanimebot.utils as utils
 import myanimebot.myanimelist as myanimelist
-from myanimebot.discord import send_embed_wrapper, build_embed
+from myanimebot.discord import send_embed_wrapper, build_embed, in_allowed_role
 
 
 if not sys.version_info[:2] >= (3, 7):
@@ -353,7 +353,7 @@ async def on_message(message):
 				await message.channel.send("pong")
 			
 			elif words[1] == "here":
-				if message.author.guild_permissions.administrator:
+				if in_allowed_role(message.author, message.guild):
 					cursor = globals.conn.cursor(buffered=True)
 					cursor.execute("SELECT server, channel FROM t_servers WHERE server=%s", [str(message.guild.id)])
 					data = cursor.fetchone()
@@ -372,22 +372,26 @@ async def on_message(message):
 							await message.channel.send("Channel updated to: **" + str(message.channel) + "**.")
 							
 					cursor.close()
-				else: await message.channel.send("Only server's admins can use this command!")
+				else: await message.channel.send("Only allowed users can use this command!")
 				
 			elif words[1] == "add":
-				await add_user_cmd(words, message)
+				if in_allowed_role(message.author, message.guild):
+					await add_user_cmd(words, message)
+				else: await message.channel.send("Only allowed users can use this command!")
 				
 			elif words[1] == "delete":
-				await delete_user_cmd(words, message)
+				if in_allowed_role(message.author, message.guild):
+					await delete_user_cmd(words, message)
+				else: await message.channel.send("Only allowed users can use this command!")
 				
 			elif words[1] == "stop":
-				if message.author.guild_permissions.administrator:
+				if in_allowed_role(message.author, message.guild):
 					if (len(words) == 2):
 						cursor = globals.conn.cursor(buffered=True)
 						cursor.execute("SELECT server FROM t_servers WHERE server=%s", [str(message.guild.id)])
 						data = cursor.fetchone()
 					
-						if data is None: await globals.client.send_message(message.channel, "The server **" + str(message.guild) + "** is not in our database.")
+						if data is None: await message.channel.send("The server **" + str(message.guild) + "** is not in our database.")
 						else:
 							cursor.execute("DELETE FROM t_servers WHERE server = %s", [message.guild.id])
 							globals.conn.commit()
@@ -395,7 +399,7 @@ async def on_message(message):
 						
 						cursor.close()
 					else: await message.channel.send("Too many arguments! Only type *stop* if you want to stop this bot on **" + message.guild + "**")
-				else: await message.channel.send("Only server's admins can use this command!")
+				else: await message.channel.send("Only allowed users can use this command!")
 				
 			elif words[1] == "info":
 				await info_cmd(message, words)
@@ -464,14 +468,31 @@ async def on_message(message):
 						globals.logger.warning("An error occured while displaying the global top for keyword '" + keyword + "': " + str(e))
 						await message.channel.send("Unable to reply to your request at the moment...")
 			
-			elif words[1] == "group":
+			elif words[1] == "role":
 				if len(words) > 2:
 					if message.author.guild_permissions.administrator:
-						group = words[2]
-						await message.channel.send("admin OK")
+						cursor = globals.conn.cursor(buffered=True)
+
+						if (words[2] == "everyone") | (words[2] == "@everyone"):
+							cursor.execute("UPDATE t_servers SET admin_group = NULL WHERE server = %s", [str(message.guild.id)])
+							globals.conn.commit()
+
+							await message.channel.send("Everybody is now allowed to use the bot!")
+						else:
+							rolesFound = message.role_mentions
+
+							if (len(rolesFound) > 1): await message.channel.send("Please specify only 1 group!")
+							elif (len(rolesFound) == 0): await message.channel.send("Please specify a correct group.")
+							else: 
+								cursor.execute("UPDATE t_servers SET admin_group = %s WHERE server = %s", [str(rolesFound[0].id), str(message.guild.id)])
+								globals.conn.commit()
+
+								await message.channel.send("The role **" + str(rolesFound[0].name) + "** is now allowed to use this bot!")
+
+						cursor.close()
 					else: await message.channel.send("Only server's admins can use this command!")
 				else:
-					await message.channel.send("You have to specify a group!")
+					await message.channel.send("You have to specify a role!")
 
 			elif words[1] == "fetch-debug":
 				await fetch_activities_anilist()

+ 20 - 0
myanimebot/discord.py

@@ -46,3 +46,23 @@ async def send_embed_wrapper(asyncioloop, channelid, client, embed):
 	except Exception as e:
 		globals.logger.debug("Impossible to send a message on '{}': {}".format(channelid, e)) 
 		return
+
+def in_allowed_role(user : discord.Member, server : int) -> bool :
+	'''Check if a user has the permissions to configure the bot on a specific server '''
+
+	targetRole = utils.get_allowed_role(server.id)
+	globals.logger.debug ("Role target: " + str(targetRole))
+
+	if user.guild_permissions.administrator:
+		globals.logger.debug (str(user) + " is server admin on " + str(server) + "!")
+		return True
+	elif (targetRole is None):
+		globals.logger.debug ("No group specified for " + str(server))
+		return True
+	else:
+		for role in user.roles: 
+			if str(role.id) == str(targetRole):
+				globals.logger.debug ("Permissions validated for " + str(user))
+				return True
+
+	return False

+ 8 - 0
myanimebot/utils.py

@@ -340,3 +340,11 @@ def insert_user_into_db(user_name : str, service : Service, servers : str) -> bo
     globals.conn.commit()
     cursor.close()
     return True
+
+def get_allowed_role(server : int) -> int:
+    cursor = globals.conn.cursor(buffered=True)
+    cursor.execute("SELECT admin_group FROM t_servers WHERE server=%s LIMIT 1", [str(server)])
+    allowedRole = cursor.fetchone()
+    cursor.close()
+
+    return allowedRole[0]