1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-03 05:23:11 +00:00
Lumi/modules/misc/avatar.py

59 lines
1.4 KiB
Python
Raw Normal View History

2024-06-21 19:36:59 +00:00
from io import BytesIO
from typing import Optional
2024-06-21 19:36:59 +00:00
from discord import File, Member
from discord.ext import bridge
2024-06-21 19:36:59 +00:00
import httpx
client: httpx.AsyncClient = httpx.AsyncClient()
2024-06-21 19:36:59 +00:00
async def get_avatar(ctx: bridge.Context, member: Member) -> None:
2024-06-21 19:36:59 +00:00
"""
Get the avatar of a member.
Parameters:
-----------
ctx : ApplicationContext
The discord context object.
member : Member
2024-06-21 19:36:59 +00:00
The member to get the avatar of.
2024-07-17 11:47:26 +00:00
"""
guild_avatar: Optional[str] = (
member.guild_avatar.url if member.guild_avatar else None
)
profile_avatar: Optional[str] = member.avatar.url if member.avatar else None
2024-06-21 19:36:59 +00:00
files: list[File] = [
2024-06-21 19:36:59 +00:00
await create_avatar_file(avatar)
for avatar in [guild_avatar, profile_avatar]
if avatar
]
if files:
await ctx.respond(files=files)
else:
await ctx.respond(content="member has no avatar.")
2024-06-21 19:36:59 +00:00
async def create_avatar_file(url: str) -> File:
2024-06-21 19:36:59 +00:00
"""
Create a discord file from an avatar url.
Parameters:
-----------
url : str
The url of the avatar.
Returns:
--------
File
2024-06-21 19:36:59 +00:00
The discord file.
"""
response: httpx.Response = await client.get(url, timeout=10)
2024-06-21 19:36:59 +00:00
response.raise_for_status()
image_data: bytes = response.content
image_file: BytesIO = BytesIO(image_data)
2024-06-21 19:36:59 +00:00
image_file.seek(0)
return File(image_file, filename="avatar.png")