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
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),

Loading…
Cancel
Save