Added new commands (roleinfo and rolelist)

pull/8/head
sgoudham 4 years ago
parent f2f20cd1e8
commit 477160ece2

@ -23,7 +23,7 @@ from platform import python_version
from time import time
from typing import Optional
from discord import Embed
from discord import Embed, Role
from discord import Member, TextChannel
from discord import __version__ as discord_version
from discord.ext.commands import BucketType, cooldown, bot_has_permissions, guild_only, Cog
@ -149,6 +149,107 @@ class Info(Cog):
await self.bot.generate_embed(ctx, desc=f"Pong! **{round(self.bot.latency * 1000)}ms**")
@command(name="roleinfo", aliases=["ri"])
@cooldown(1, 5, BucketType.user)
@guild_only()
async def role_info(self, ctx, role: Role):
"""Retrieve information about any role!"""
# Returns the permissions that the user has within the guild
filtered = filter(lambda x: x[1], role.permissions)
# Replace all "_" with " " in each item and join them together
perms = ",".join(map(lambda x: x[0].replace("_", " "), filtered))
# Capitalise every word in the array and filter out the permissions that are defined within the frozenset
permission = string.capwords("".join(map(str, detectPermissions(perms, Perms))))
# Accounting for the edge case where the user has no key permissions to be displayed
permissions = "No Key Permissions" if permission == "" else permission
if len(role.members) > 30:
# Retrieve the length of the members within the role
length = len(role.members) - 30
# Store the first 30 members in a string called "roles" (highest to lowest)
member = f"{' **|** '.join(map(str, (member.mention for member in list(reversed(role.members))[:30])))} and **{length}** more"
else:
# Display all members within the roles as it is lower than 30
member = f"{' **|** '.join(map(str, (member.mention for member in list(reversed(role.members)))))}"
# Accounting for the edge case the role has no members
members = "No Members In Role" if member == "" else member
# Using emotes to represent bools
mentionable = "<:greenTick:746834932936212570>" if role.mention else "<:xMark:746834944629932032>"
hoisted = "<:greenTick:746834932936212570>" if role.hoist else "<:xMark:746834944629932032>"
managed = "<:greenTick:746834932936212570>" if role.managed else "<:xMark:746834944629932032>"
# Description of the embed
desc = f"{role.mention} " \
f"**<-- Colour:** {str(role.colour)} | **Position:** {role.position}"
# Set up Embed
embed = Embed(title=f"{role.name}",
description=desc,
colour=role.colour,
timestamp=datetime.datetime.utcnow())
embed.set_thumbnail(url=ctx.guild.icon_url)
embed.set_footer(text=f"ID: {role.id}")
# Setting up fields
fields = [
("Creation At", role.created_at.strftime("%a, %b %d, %Y\n%I:%M:%S %p"), True),
(f"Misc",
f"\n Mentionable?: {mentionable}" +
f"\n Hoisted?: {hoisted}" +
f"\n Managed?: {managed}", True),
(f"Members ({len(role.members)})",
f"\nHumans: {len(list(filter(lambda m: not m.bot, role.members)))}" +
f"\nBots: {len(list(filter(lambda m: m.bot, role.members)))}", True),
("List of Members", members, False),
("Key Permissions", permissions, False)
]
# Add fields to the embed
for name, value, inline in fields:
embed.add_field(name=name, value=value, inline=inline)
await ctx.send(embed=embed)
@command(name="rolelist", aliases=["rl"])
@guild_only()
async def role_list(self, ctx):
"""Retrieve list of all roles in the server!"""
# More readable name
guild_roles = ctx.guild.roles
if len(guild_roles) > 50:
# Retrieve the length of the remaining roles
length = len(guild_roles) - 50
# Store the first 50 roles in a string called "roles" (highest to lowest)
role = f"{' **|** '.join(map(str, (role.mention for role in list(reversed(guild_roles))[:50])))} and **{length}** more"
else:
# Display all roles as it is lower than 20
role = f"{' **|** '.join(map(str, (role.mention for role in list(reversed(guild_roles[1:])))))}"
# Accounting for the edge case where the user has no roles to be displayed
roles = "Guild Has No Roles" if role == "" else role
embed = Embed(title=f"{ctx.guild}'s Roles({len(ctx.guild.roles)})",
description=roles,
color=self.bot.random_colour(),
timestamp=datetime.datetime.utcnow())
embed.set_footer(text=f"Guild ID: {ctx.guild.id}", icon_url=ctx.guild.icon_url)
await ctx.send(embed=embed)
@command(name="userinfo")
@cooldown(1, 5, BucketType.user)
@guild_only()
@ -169,11 +270,11 @@ class Info(Cog):
length = len(member.roles) - 20
# Store the first 20 roles in a string called "roles" (highest to lowest)
role = f"{' '.join(map(str, (role.mention for role in list(reversed(member.roles))[:20])))} and **{length}** more"
role = f"{' **|** '.join(map(str, (role.mention for role in list(reversed(member.roles))[:20])))} and **{length}** more"
else:
# Display all roles as it is lower than 20
role = f"{' '.join(map(str, (role.mention for role in list(reversed(member.roles[1:])))))}"
role = f"{' **|** '.join(map(str, (role.mention for role in list(reversed(member.roles[1:])))))}"
# Accounting for the edge case where the user has no roles to be displayed
roles = "No Roles" if role == "" else role

Loading…
Cancel
Save