|
| 1 | +"""MIT License |
| 2 | +
|
| 3 | +Copyright (c) 2021-Present PythonistaGuild |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +""" |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import discord |
| 26 | +from discord.ext import commands |
| 27 | + |
| 28 | +import core |
| 29 | + |
| 30 | + |
| 31 | +class InformationEmbed(discord.Embed): |
| 32 | + """A subclass of discord.Embed. |
| 33 | +
|
| 34 | + This class allows to automatically get information within the class instead of recreating the embed each time |
| 35 | +
|
| 36 | + :param author: The embed author. Expects discord.Member or discord.User |
| 37 | + :param entity: The Member, User, Role, TextChannel, or Guild to get information about. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + *, |
| 43 | + author: discord.Member | discord.User, |
| 44 | + entity: discord.Member | discord.User | discord.Role | discord.TextChannel | discord.Guild, |
| 45 | + ) -> None: |
| 46 | + super().__init__() |
| 47 | + created_at: str = f"{discord.utils.format_dt(entity.created_at)} ({discord.utils.format_dt(entity.created_at, 'R')})" |
| 48 | + if isinstance(entity, discord.Member) and entity.joined_at: |
| 49 | + joined_at = f"\n\nJoined At: {discord.utils.format_dt(entity.joined_at)} ({discord.utils.format_dt(entity.joined_at, 'R')})" |
| 50 | + else: |
| 51 | + joined_at = "" |
| 52 | + |
| 53 | + description = f"Name: {entity.name}\n\nID: {entity.id}\n\nCreated At: {created_at}{joined_at}" |
| 54 | + if isinstance(entity, discord.Member): |
| 55 | + self.set_thumbnail(url=entity.guild_avatar or entity.display_avatar or None) |
| 56 | + elif isinstance(entity, discord.User): |
| 57 | + self.set_thumbnail(url=entity.display_avatar or None) |
| 58 | + elif isinstance(entity, discord.Role): |
| 59 | + description += f"\n\nHoisted: {entity.hoist}\n\nMentionable: {entity.mentionable}\n\n" |
| 60 | + elif isinstance(entity, discord.TextChannel): |
| 61 | + description += f"\n\nCategory: {entity.category}\n\nNSFW: {entity.nsfw}" |
| 62 | + else: # Change to elif when other types are added |
| 63 | + description += f"\n\nOwner: {entity.owner}" |
| 64 | + self.set_thumbnail(url=entity.icon or None) |
| 65 | + |
| 66 | + self.description = description |
| 67 | + self.set_author(name=author.name, icon_url=author.display_avatar) |
| 68 | + self.color = 0x7289DA |
| 69 | + |
| 70 | + |
| 71 | +class Information(core.Cog): |
| 72 | + """Information commands which allows you to get information about users, the guild, roles, and channels.""" |
| 73 | + |
| 74 | + def __init__(self, bot: core.Bot) -> None: |
| 75 | + self.bot = bot |
| 76 | + |
| 77 | + @commands.group( |
| 78 | + name="information", |
| 79 | + brief="Get information on a user, role, or channel", |
| 80 | + aliases=["i", "info"], |
| 81 | + invoke_without_command=True, |
| 82 | + ) |
| 83 | + async def info( |
| 84 | + self, ctx: core.Context, entity: discord.Member | discord.User | discord.Role | discord.TextChannel = commands.Author |
| 85 | + ) -> None: |
| 86 | + """Get information about a object |
| 87 | + Args: |
| 88 | + entity: The user, role, or TextChannel to get information about""" |
| 89 | + embed = InformationEmbed(author=ctx.author, entity=entity) |
| 90 | + await ctx.send(embed=embed) |
| 91 | + |
| 92 | + @info.command(name="guild", brief="Get the current guild's information.") |
| 93 | + async def guild_info(self, ctx: core.GuildContext): |
| 94 | + embed = InformationEmbed(author=ctx.author, entity=ctx.guild) |
| 95 | + await ctx.reply(embed=embed) |
| 96 | + |
| 97 | + |
| 98 | +async def setup(bot: core.Bot) -> None: |
| 99 | + await bot.add_cog(Information(bot)) |
0 commit comments