diff options
author | 2018-07-31 19:33:57 +0000 | |
---|---|---|
committer | 2018-07-31 19:33:57 +0000 | |
commit | 35c6db924ca55ccbb26bac525079735b0ff92aa5 (patch) | |
tree | 5448463a80292ff5a5cae44f97a504823f016285 | |
parent | Merge branch 'enhancement/increase-pil-png-image-plugin-logging-level' into '... (diff) | |
parent | Added user info command (diff) |
Merge branch 'feature/userinfo' into 'master'
User info command
See merge request python-discord/projects/bot!42
-rw-r--r-- | bot/cogs/information.py | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/bot/cogs/information.py b/bot/cogs/information.py index f61b130a6..ddc18fcf8 100644 --- a/bot/cogs/information.py +++ b/bot/cogs/information.py @@ -3,10 +3,10 @@ import textwrap from datetime import datetime from dateutil.relativedelta import relativedelta -from discord import CategoryChannel, Colour, Embed, TextChannel, VoiceChannel +from discord import CategoryChannel, Colour, Embed, Member, TextChannel, VoiceChannel from discord.ext.commands import Bot, Context, command -from bot.constants import Emojis +from bot.constants import Emojis, Keys, URLs from bot.utils.time import humanize log = logging.getLogger(__name__) @@ -21,6 +21,7 @@ class Information: def __init__(self, bot: Bot): self.bot = bot + self.headers = {"X-API-Key": Keys.site_api} @command(name="roles") async def roles_info(self, ctx: Context): @@ -120,6 +121,76 @@ class Information: await ctx.send(embed=embed) + @command(name="user", aliases=["user_info", "member", "member_info"]) + async def user_info(self, ctx: Context, user: Member = None): + """ + Returns info about a user. + """ + + if user is None: + user = ctx.author + + now = datetime.now() + + # User information + created_delta = relativedelta(now, user.created_at) + created = humanize(created_delta, accuracy="days") + + name = f"{user.name}#{user.discriminator}" + if user.nick: + name = f"{user.nick} ({name})" + + # Member information + joined_delta = relativedelta(now, user.joined_at) + joined = humanize(joined_delta, accuracy="days") + + # You're welcome, Volcyyyyyyyyyyyyyyyy + roles = ", ".join( + role.mention for role in user.roles if role.name != "@everyone" + ) + + # Infractions + api_response = await self.bot.http_session.get( + url=URLs.site_infractions_user.format(user_id=user.id), + headers=self.headers + ) + + infractions = await api_response.json() + + infr_total = 0 + infr_active = 0 + + # At least it's readable. + for infr in infractions: + if infr["active"]: + infr_active += 1 + + infr_total += 1 + + # Let's build the embed now + embed = Embed( + title=name, + description=textwrap.dedent(f""" + **User Information** + Created: {created} ago + Profile: {user.mention} + ID: {user.id} + + **Member Information** + Joined: {joined} ago + Roles: {roles or None} + + **Infractions** + Total: {infr_total} + Active: {infr_active} + """) + ) + + embed.set_thumbnail(url=user.avatar_url_as(format="png")) + embed.colour = user.top_role.colour if roles else Colour.blurple() + + await ctx.send(embed=embed) + def setup(bot): bot.add_cog(Information(bot)) |