feat(Guild): add Role

This commit is contained in:
grey-cat-1908 2022-05-31 11:12:15 +03:00
parent 17df39d574
commit 433a647168
2 changed files with 22 additions and 6 deletions

View file

@ -15,6 +15,7 @@ from .channel import (
from .member import GuildMember from .member import GuildMember
from ... import Role
from ...utils import Snowflake, Timestamp from ...utils import Snowflake, Timestamp
from ...utils.api_model import APIModelBase from ...utils.api_model import APIModelBase
from ...utils.conversion import try_enum from ...utils.conversion import try_enum
@ -390,7 +391,6 @@ class Guild(APIModelBase):
self.icon_hash = data.get("icon_hash") self.icon_hash = data.get("icon_hash")
self.banner = data.get("banner") self.banner = data.get("banner")
self.unavailable = data.get("unavailable", False) self.unavailable = data.get("unavailable", False)
self.roles = data.get("roles", {})
self.permissions = data.get("permissions") self.permissions = data.get("permissions")
self.welcome_screen = data.get("welcome_screen") self.welcome_screen = data.get("welcome_screen")
self.guild_scheduled_events = data.get("guild_scheduled_events") self.guild_scheduled_events = data.get("guild_scheduled_events")
@ -478,6 +478,10 @@ class Guild(APIModelBase):
channel = _choose_channel_type(channel) channel = _choose_channel_type(channel)
self.channels[Snowflake(int(channel.id))] = 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 return self
@overload @overload

View file

@ -4,11 +4,12 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict from typing import Dict
from ...utils import Snowflake from ...utils import Snowflake
from ...utils.api_model import APIModelBase from ...utils.api_model import APIModelBase
from ...utils.types import APINullable, UNDEFINED from ...utils.types import APINullable, UNDEFINED
from ..message.colors import Color
@dataclass(repr=False) @dataclass(repr=False)
@ -25,7 +26,7 @@ class Role(APIModelBase):
Integer representation of hexadecimal color code Integer representation of hexadecimal color code
hoist: :class:`bool` hoist: :class:`bool`
If this role is pinned in the user listing If this role is pinned in the user listing
icon: :class:`str` icon: Optional[:class:`str`]
Role icon hash Role icon hash
unicode_emoji: :class:`str` unicode_emoji: :class:`str`
Role unicode emoji Role unicode emoji
@ -37,11 +38,13 @@ class Role(APIModelBase):
Whether this role is managed by an integration Whether this role is managed by an integration
mentionable: :class:`bool` mentionable: :class:`bool`
Whether this role is mentionable Whether this role is mentionable
guild_id: Optional[:class:`Snowflake`]
Id of guild where role is
""" """
id: APINullable[Snowflake] = UNDEFINED id: APINullable[Snowflake] = UNDEFINED
name: APINullable[str] = UNDEFINED name: APINullable[str] = UNDEFINED
color: APINullable[int] = UNDEFINED color: APINullable[Color] = UNDEFINED
hoist: APINullable[bool] = UNDEFINED hoist: APINullable[bool] = UNDEFINED
icon: APINullable[str] = UNDEFINED icon: APINullable[str] = UNDEFINED
unicode_emoji: APINullable[str] = UNDEFINED unicode_emoji: APINullable[str] = UNDEFINED
@ -49,6 +52,7 @@ class Role(APIModelBase):
permission: APINullable[str] = UNDEFINED permission: APINullable[str] = UNDEFINED
managed: APINullable[bool] = UNDEFINED managed: APINullable[bool] = UNDEFINED
mentionable: APINullable[bool] = UNDEFINED mentionable: APINullable[bool] = UNDEFINED
guild_id: APINullable[Snowflake] = UNDEFINED
@classmethod @classmethod
def from_dict(cls, data: Dict[str, any]) -> Role: def from_dict(cls, data: Dict[str, any]) -> Role:
@ -62,9 +66,9 @@ class Role(APIModelBase):
self: Role = super().__new__(cls) self: Role = super().__new__(cls)
self.id = int(data["id"]) self.id = Snowflake(data["id"])
self.name = data.get("name") self.name = data.get("name")
self.color = data.get("color") self.color = Color(data.get("color", 0))
self.hoist = data.get("hoist", False) self.hoist = data.get("hoist", False)
self.icon = data.get("icon") self.icon = data.get("icon")
self.unicode_emoji = data.get("unicode_emoji") self.unicode_emoji = data.get("unicode_emoji")
@ -72,5 +76,13 @@ class Role(APIModelBase):
self.permission = data.get("permission") self.permission = data.get("permission")
self.managed = data.get("managed", False) self.managed = data.get("managed", False)
self.mentionable = data.get("mentionable", False) self.mentionable = data.get("mentionable", False)
self.guild_id = data.get("guild_id")
return self return self
@property
def guild(self):
if self.guild_id is None:
return None
else:
return self._client.cache.get_guild(self.guild_id)