From b375e6a209c461f91e59ebec493bb3015f20f1af Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sun, 12 Jul 2020 04:44:57 +0100 Subject: [PATCH] Making code more efficient Using map/.join/filter/lambda --- cogs/help/info.py | 52 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/cogs/help/info.py b/cogs/help/info.py index 183068da..eea63df7 100644 --- a/cogs/help/info.py +++ b/cogs/help/info.py @@ -11,27 +11,28 @@ import settings # Permissions to filter through Perms = frozenset( - { - "create instant invite", - "add reactions", - "view audit log", - "priority speaker", - "stream", - "read messages", - "send messages", - "send tts messages", - "embed links", - "attach links", - "read message history", - "external emojis", - "view guild insights", - "connect": "", - "speak": "", - "use voice activation", - "change nickname" - } + { + "create instant invite", + "add reactions", + "view audit log", + "priority speaker", + "stream", + "read messages", + "send messages", + "send tts messages", + "embed links", + "attach links", + "read message history", + "external emojis", + "view guild insights", + "connect", + "speak", + "use voice activation", + "change nickname" + } ) + # Method to detect which permissions to filter out def DetectPermissions(message, fset): # Split the message individual permissions @@ -39,7 +40,8 @@ def DetectPermissions(message, fset): # Filter the permission out if it's in the frozenset filtered = filter(lambda perm: perm not in fset, message) - return ",".join(filtered) + return ", ".join(filtered) + class GetInfo(commands.Cog): def __init__(self, bot): @@ -64,17 +66,17 @@ class GetInfo(commands.Cog): mentions = [role.mention for role in target.roles] # Store the roles in a string called "roles" - # For each role that the user has (Skipping the first element as it's always going to be @everyone + # For each role that the user has (Skipping the first element as it's always going to be @everyone) # Store all the permissions that the user has in a string roles = " ".join(mentions[1:]) - + # filter out the ones with permission set to False filtered = filter(lambda x: x[1], target.guild_permissions) # now replace all "_" with " " in each item and join them together permission = ",".join(map(lambda x: x[0].replace("_", " "), filtered)) - + # Capitalise every word in the array and get rid of the ", " at the end of the string - permissions = string.capwords("".join(map(str, DetectPermissions(permission, Perms)[0:-4]))) + permissions = string.capwords("".join(map(str, DetectPermissions(permission, Perms)))) # Set up the embed to display everything about the user embed = Embed( @@ -135,7 +137,7 @@ class GetInfo(commands.Cog): ("\u200b", "\u200b", False), ("Members", len(ctx.guild.members), True), ("Humans", len(list(filter(lambda m: not m.bot, ctx.guild.members))), True), - ("Bots", len(list(filter(lambda c: c.bot, ctx.guild.members))), True), + ("Bots", len(list(filter(lambda m: m.bot, ctx.guild.members))), True), ("Banned Members", len(await ctx.guild.bans()), True), ("Text Channels", len(ctx.guild.text_channels), True), ("Voice Channels", len(ctx.guild.voice_channels), True),