Making code more efficient

Using map/.join/filter/lambda
pull/8/head
sgoudham 4 years ago
parent 6d8b63fb46
commit b375e6a209

@ -25,13 +25,14 @@ Perms = frozenset(
"read message history", "read message history",
"external emojis", "external emojis",
"view guild insights", "view guild insights",
"connect": "", "connect",
"speak": "", "speak",
"use voice activation", "use voice activation",
"change nickname" "change nickname"
} }
) )
# Method to detect which permissions to filter out # Method to detect which permissions to filter out
def DetectPermissions(message, fset): def DetectPermissions(message, fset):
# Split the message individual permissions # Split the message individual permissions
@ -39,7 +40,8 @@ def DetectPermissions(message, fset):
# Filter the permission out if it's in the frozenset # Filter the permission out if it's in the frozenset
filtered = filter(lambda perm: perm not in fset, message) filtered = filter(lambda perm: perm not in fset, message)
return ",".join(filtered) return ", ".join(filtered)
class GetInfo(commands.Cog): class GetInfo(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
@ -64,7 +66,7 @@ class GetInfo(commands.Cog):
mentions = [role.mention for role in target.roles] mentions = [role.mention for role in target.roles]
# Store the roles in a string called "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 # Store all the permissions that the user has in a string
roles = " ".join(mentions[1:]) roles = " ".join(mentions[1:])
@ -74,7 +76,7 @@ class GetInfo(commands.Cog):
permission = ",".join(map(lambda x: x[0].replace("_", " "), filtered)) 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 # 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 # Set up the embed to display everything about the user
embed = Embed( embed = Embed(
@ -135,7 +137,7 @@ class GetInfo(commands.Cog):
("\u200b", "\u200b", False), ("\u200b", "\u200b", False),
("Members", len(ctx.guild.members), True), ("Members", len(ctx.guild.members), True),
("Humans", len(list(filter(lambda m: not m.bot, 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), ("Banned Members", len(await ctx.guild.bans()), True),
("Text Channels", len(ctx.guild.text_channels), True), ("Text Channels", len(ctx.guild.text_channels), True),
("Voice Channels", len(ctx.guild.voice_channels), True), ("Voice Channels", len(ctx.guild.voice_channels), True),

Loading…
Cancel
Save