Making code more efficient

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

@ -11,27 +11,28 @@ import settings
# Permissions to filter through # Permissions to filter through
Perms = frozenset( Perms = frozenset(
{ {
"create instant invite", "create instant invite",
"add reactions", "add reactions",
"view audit log", "view audit log",
"priority speaker", "priority speaker",
"stream", "stream",
"read messages", "read messages",
"send messages", "send messages",
"send tts messages", "send tts messages",
"embed links", "embed links",
"attach links", "attach links",
"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,17 +66,17 @@ 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:])
# filter out the ones with permission set to False # filter out the ones with permission set to False
filtered = filter(lambda x: x[1], target.guild_permissions) filtered = filter(lambda x: x[1], target.guild_permissions)
# now replace all "_" with " " in each item and join them together # now replace all "_" with " " in each item and join them together
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