feat(bot): ✨ Add pfp color fetching on profile update.
This commit is contained in:
@@ -8,7 +8,7 @@ from payphone.core import constants, formatting
|
||||
|
||||
async def ReplyEmbed(
|
||||
reference: discord.MessageReference, call_id: int
|
||||
) -> (discord.Embed | None, models.Message | None):
|
||||
) -> tuple[discord.Embed | None, models.Message | None]:
|
||||
"""Generates the embed sent on calls to represent replies.
|
||||
|
||||
Args:
|
||||
@@ -16,7 +16,8 @@ async def ReplyEmbed(
|
||||
call_id (int): The ID of the call the message was sent in.
|
||||
|
||||
Returns:
|
||||
(discord.Embed | None, models.Message | None): The generated embed and the replied message.
|
||||
tuple[discord.Embed | None, models.Message | None]: The generated embed and the replied
|
||||
message.
|
||||
"""
|
||||
|
||||
message = (
|
||||
@@ -50,7 +51,11 @@ async def ReplyEmbed(
|
||||
)
|
||||
|
||||
embed = discord.Embed(
|
||||
color=constants.EMBED_COLOR,
|
||||
color=(
|
||||
constants.EMBED_COLOR
|
||||
if not is_this_channel and message.mask is not None
|
||||
else message.profile.color or constants.EMBED_COLOR
|
||||
),
|
||||
description=(
|
||||
formatting.trim(last_content.content)
|
||||
if last_content.was_propagated or is_this_channel
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import io
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import discord
|
||||
@@ -6,10 +8,14 @@ from discord.utils import find
|
||||
|
||||
from tortoise import transactions, functions, expressions, exceptions
|
||||
|
||||
import colorthief
|
||||
|
||||
import httpx
|
||||
|
||||
from payphone import bot
|
||||
from payphone.db import models, utils
|
||||
from payphone.cogs import base
|
||||
from payphone.core import constants, formatting as fmt
|
||||
from payphone.core import constants
|
||||
|
||||
|
||||
class Users(base.Cog):
|
||||
@@ -63,6 +69,88 @@ class Users(base.Cog):
|
||||
users_map.values(), ["leaderboard_position", "leaderboard_calls_count"], 500
|
||||
)
|
||||
|
||||
@commands.Cog.listener()
|
||||
@transactions.atomic()
|
||||
async def on_user_update(self, _before: discord.User, after: discord.User) -> None:
|
||||
"""Updates a user's Profile.
|
||||
|
||||
Args:
|
||||
_before (discord.User): The user before the change.
|
||||
after (discord.User): The user after the change.
|
||||
"""
|
||||
|
||||
profile = await models.Profile.get_or_none(user_id=after.id, server_id=None)
|
||||
|
||||
is_updated = False
|
||||
|
||||
if profile is None:
|
||||
return
|
||||
|
||||
if profile.avatar_url != after.display_avatar.url:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(after.display_avatar.url)
|
||||
|
||||
if response.status_code not in range(200, 300):
|
||||
return
|
||||
|
||||
f = io.BytesIO(response.content)
|
||||
|
||||
(r, g, b) = colorthief.ColorThief(f).get_color(quality=1)
|
||||
|
||||
profile.color = (r << 16) | (g << 8) | b
|
||||
|
||||
is_updated = True
|
||||
|
||||
if profile.name != after.display_name:
|
||||
profile.name = after.display_name
|
||||
is_updated = True
|
||||
|
||||
if is_updated:
|
||||
await profile.save()
|
||||
|
||||
@commands.Cog.listener()
|
||||
@transactions.atomic()
|
||||
async def on_member_update(
|
||||
self, _before: discord.Member, after: discord.Member
|
||||
) -> None:
|
||||
"""Updates a member's profile.
|
||||
|
||||
Args:
|
||||
_before (discord.Member): The member's profile before the update.
|
||||
after (discord.Member): The member's profile after the update.
|
||||
"""
|
||||
|
||||
profile = await models.Profile.get_or_none(
|
||||
user_id=after.id, server_id=after.guild.id
|
||||
)
|
||||
|
||||
is_updated = False
|
||||
|
||||
if profile is None:
|
||||
return
|
||||
|
||||
if profile.avatar_url != after.display_avatar.url:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(after.display_avatar.url)
|
||||
|
||||
if response.status_code not in range(200, 300):
|
||||
return
|
||||
|
||||
f = io.BytesIO(response.content)
|
||||
|
||||
(r, g, b) = colorthief.ColorThief(f).get_color(quality=1)
|
||||
|
||||
profile.color = (r << 16) | (g << 8) | b
|
||||
|
||||
is_updated = True
|
||||
|
||||
if profile.name != after.display_name:
|
||||
profile.name = after.display_name
|
||||
is_updated = True
|
||||
|
||||
if is_updated:
|
||||
await profile.save()
|
||||
|
||||
@commands.command()
|
||||
@transactions.atomic()
|
||||
async def mask(self, ctx: commands.Context, toggle: bool | None = None) -> None:
|
||||
@@ -270,6 +358,7 @@ class Users(base.Cog):
|
||||
if logs != "":
|
||||
embeds.append(
|
||||
discord.Embed(
|
||||
color=profile.color,
|
||||
title="Call History",
|
||||
description="Last calls:\n" + logs,
|
||||
thumbnail=profile.avatar_url,
|
||||
@@ -335,7 +424,7 @@ class Users(base.Cog):
|
||||
)
|
||||
|
||||
embed = discord.Embed(
|
||||
color=constants.EMBED_COLOR,
|
||||
color=profile.color or constants.EMBED_COLOR,
|
||||
timestamp=datetime.now(timezone.utc),
|
||||
fields=[
|
||||
discord.EmbedField(
|
||||
|
||||
Reference in New Issue
Block a user