diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index 25cf4a0..2c6f7d8 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -15,6 +15,7 @@ from .channel import ( from .member import GuildMember +from ... import Role from ...utils import Snowflake, Timestamp from ...utils.api_model import APIModelBase from ...utils.conversion import try_enum @@ -390,7 +391,6 @@ class Guild(APIModelBase): self.icon_hash = data.get("icon_hash") self.banner = data.get("banner") self.unavailable = data.get("unavailable", False) - self.roles = data.get("roles", {}) self.permissions = data.get("permissions") self.welcome_screen = data.get("welcome_screen") self.guild_scheduled_events = data.get("guild_scheduled_events") @@ -478,6 +478,10 @@ class Guild(APIModelBase): channel = _choose_channel_type(channel) self.channels[Snowflake(int(channel.id))] = channel + for role in data.get("roles", []): + role["guild_id"] = self.id + self.roles[Snowflake(int(role.id))] = Role.from_dict(role) + return self @overload diff --git a/melisa/models/guild/role.py b/melisa/models/guild/role.py index 70e97b9..5865fe2 100644 --- a/melisa/models/guild/role.py +++ b/melisa/models/guild/role.py @@ -4,11 +4,12 @@ from __future__ import annotations from dataclasses import dataclass -from typing import TYPE_CHECKING, Dict +from typing import Dict from ...utils import Snowflake from ...utils.api_model import APIModelBase from ...utils.types import APINullable, UNDEFINED +from ..message.colors import Color @dataclass(repr=False) @@ -25,7 +26,7 @@ class Role(APIModelBase): Integer representation of hexadecimal color code hoist: :class:`bool` If this role is pinned in the user listing - icon: :class:`str` + icon: Optional[:class:`str`] Role icon hash unicode_emoji: :class:`str` Role unicode emoji @@ -37,11 +38,13 @@ class Role(APIModelBase): Whether this role is managed by an integration mentionable: :class:`bool` Whether this role is mentionable + guild_id: Optional[:class:`Snowflake`] + Id of guild where role is """ id: APINullable[Snowflake] = UNDEFINED name: APINullable[str] = UNDEFINED - color: APINullable[int] = UNDEFINED + color: APINullable[Color] = UNDEFINED hoist: APINullable[bool] = UNDEFINED icon: APINullable[str] = UNDEFINED unicode_emoji: APINullable[str] = UNDEFINED @@ -49,6 +52,7 @@ class Role(APIModelBase): permission: APINullable[str] = UNDEFINED managed: APINullable[bool] = UNDEFINED mentionable: APINullable[bool] = UNDEFINED + guild_id: APINullable[Snowflake] = UNDEFINED @classmethod def from_dict(cls, data: Dict[str, any]) -> Role: @@ -62,9 +66,9 @@ class Role(APIModelBase): self: Role = super().__new__(cls) - self.id = int(data["id"]) + self.id = Snowflake(data["id"]) self.name = data.get("name") - self.color = data.get("color") + self.color = Color(data.get("color", 0)) self.hoist = data.get("hoist", False) self.icon = data.get("icon") self.unicode_emoji = data.get("unicode_emoji") @@ -72,5 +76,13 @@ class Role(APIModelBase): self.permission = data.get("permission") self.managed = data.get("managed", False) self.mentionable = data.get("mentionable", False) + self.guild_id = data.get("guild_id") return self + + @property + def guild(self): + if self.guild_id is None: + return None + else: + return self._client.cache.get_guild(self.guild_id)