diff --git a/cogs/fetch_user_cog.py b/cogs/fetch_user_cog.py new file mode 100644 index 0000000..e4f5092 --- /dev/null +++ b/cogs/fetch_user_cog.py @@ -0,0 +1,50 @@ +import discord +from discord.ext import commands +from discord import app_commands + +class FetchUserCog(commands.Cog, name="FetchUser"): + """Cog providing a command to fetch a user by ID.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + async def _create_user_embed(self, user: discord.User) -> discord.Embed: + """Create an embed showing basic information about a user.""" + username = ( + f"{user.name}#{user.discriminator}" + if user.discriminator != "0" + else user.name + ) + embed = discord.Embed(title="User Information", color=discord.Color.blue()) + embed.add_field(name="Username", value=username, inline=False) + embed.add_field(name="User ID", value=str(user.id), inline=False) + embed.add_field(name="Bot Account", value=str(user.bot), inline=False) + embed.add_field( + name="Created", + value=user.created_at.strftime("%Y-%m-%d %H:%M UTC"), + inline=False, + ) + if user.avatar: + embed.set_thumbnail(url=user.avatar.url) + return embed + + async def _fetch_user_and_send(self, sendable, user_id: int): + try: + user = await self.bot.fetch_user(user_id) + except discord.NotFound: + await sendable("❌ User not found.") + return + except discord.HTTPException as e: + await sendable(f"Failed to fetch user: {e}") + return + + embed = await self._create_user_embed(user) + await sendable(embed=embed) + + @commands.hybrid_command(name="fetchuser", description="Fetch a user by ID and show info.") + async def fetchuser(self, ctx: commands.Context, user_id: int): + """Fetch a Discord user by ID.""" + await self._fetch_user_and_send(ctx.send, user_id) + +async def setup(bot: commands.Bot): + await bot.add_cog(FetchUserCog(bot)) diff --git a/neru_bot.py b/neru_bot.py index 0d4527f..3ac5aec 100644 --- a/neru_bot.py +++ b/neru_bot.py @@ -107,6 +107,7 @@ class NeruBot(commands.Bot): "cogs.upload_cog", "cogs.dictionary_cog", "cogs.eval_cog", + "cogs.fetch_user_cog", ] # Load each cog individually