diff --git a/src/config/config_types/discord_types/channel.py b/src/config/config_types/discord_types/channel.py index cd9bf21..6563b1f 100644 --- a/src/config/config_types/discord_types/channel.py +++ b/src/config/config_types/discord_types/channel.py @@ -15,7 +15,7 @@ class Channel(BaseType): client: BotBase #: :class:`typing.Optional` [:class:`int`]: Current channel id value: int - #: :class:`typing.Optional` [:class:`discord.TextChannel`]: Current guild instance + #: :class:`typing.Optional` [:class:`discord.TextChannel`]: Current channel instance channel_instance: typing.Optional[discord.TextChannel] def __init__(self, client: BotBase) -> None: @@ -49,7 +49,7 @@ class Channel(BaseType): :param value: Value to test :type value: Union[int, discord.TextChannel] - :return: True if guild exists + :return: True if channel exists """ id = value if isinstance(value, discord.TextChannel): @@ -93,7 +93,7 @@ class Channel(BaseType): >>> my_channel = Channel(client) #doctest: +SKIP >>> my_channel.set(valid_id_or_channel) #doctest: +SKIP >>> my_channel.get() #doctest: +SKIP - + If client is not connected: >>> my_channel = Channel(client) #doctest: +SKIP @@ -113,6 +113,7 @@ class Channel(BaseType): Return id of channel :Basic usage: + >>> my_channel = Channel(client) #doctest: +SKIP >>> my_channel.set(valid_id_or_channel) #doctest: +SKIP >>> my_channel.to_save() #doctest: +SKIP diff --git a/src/config/config_types/discord_types/guild.py b/src/config/config_types/discord_types/guild.py index 66a74f2..2ace07d 100644 --- a/src/config/config_types/discord_types/guild.py +++ b/src/config/config_types/discord_types/guild.py @@ -113,6 +113,7 @@ class Guild(BaseType): Return id of guild :Basic usage: + >>> my_guild = Guild(client) #doctest: +SKIP >>> my_guild.set(valid_id_or_guild) #doctest: +SKIP >>> my_guild.to_save() #doctest: +SKIP diff --git a/src/config/config_types/discord_types/role.py b/src/config/config_types/discord_types/role.py index 17d72d0..4c19d61 100644 --- a/src/config/config_types/discord_types/role.py +++ b/src/config/config_types/discord_types/role.py @@ -11,9 +11,9 @@ if typing.TYPE_CHECKING: class Role(BaseType): #: :class:`BotBase`: Client instance for checking client: BotBase - #: :class:`typing.Optional` [:class:`int`]: Current channel id + #: :class:`typing.Optional` [:class:`int`]: Current role id value: int - #: :class:`typing.Optional` [:class:`discord.Role`]: Current guild instance + #: :class:`typing.Optional` [:class:`discord.Role`]: Current role instance role_instance: typing.Optional[discord.Role] def __init__(self, client: BotBase) -> None: @@ -47,15 +47,15 @@ class Role(BaseType): :param value: Value to test :type value: Union[int, discord.Role] - :return: True if guild exists + :return: True if role exists """ id = value - if isinstance(value, discord.TextChannel): + if isinstance(value, discord.Role): id = value.id if not self.client.is_ready(): - self.client.warning(f"No check for channel {value} because client is not initialized!") + self.client.warning(f"No check for role {value} because client is not initialized!") return True - if self.client.get_channel(id): + if self.client.get_role(id): return True return True @@ -91,7 +91,7 @@ class Role(BaseType): >>> my_role = Role(client) #doctest: +SKIP >>> my_role.set(valid_id_or_role) #doctest: +SKIP >>> my_role.get() #doctest: +SKIP - + If client is not connected: >>> my_role = Role(client) #doctest: +SKIP @@ -100,17 +100,18 @@ class Role(BaseType): 23411424132412 :return: Role object if client is connected, else id - :rtype: Union[int, discord.Guild] + :rtype: Union[int, discord.Role] """ - if self.channel_instance is None: + if self.role_instance is None: self._update() - return self.channel_instance or self.value + return self.role_instance or self.value def to_save(self) -> int: """ - Return id of channel + Return id of role :Basic usage: + >>> my_role = Role(client) #doctest: +SKIP >>> my_role.set(valid_id_or_role) #doctest: +SKIP >>> my_role.to_save() #doctest: +SKIP @@ -144,9 +145,9 @@ class Role(BaseType): def _update(self): if self.client.is_ready() and self.role_instance is None: - self.channel_instance = self.client.get_role(self.value) + self.role_instance = self.client.get_role(self.value) else: - self.channel_instance = None + self.role_instance = None def __repr__(self): - return f'' \ No newline at end of file + return f'' \ No newline at end of file diff --git a/src/config/config_types/discord_types/user.py b/src/config/config_types/discord_types/user.py index 420d6db..6aa146d 100644 --- a/src/config/config_types/discord_types/user.py +++ b/src/config/config_types/discord_types/user.py @@ -1,36 +1,153 @@ from __future__ import annotations - import typing -from config.config_types.base_type import BaseType +import discord +from config.config_types.base_type import BaseType if typing.TYPE_CHECKING: from bot_base import BotBase + class User(BaseType): - + #: :class:`BotBase`: Client instance for checking client: BotBase + #: :class:`typing.Optional` [:class:`int`]: Current user id + value: int + #: :class:`typing.Optional` [:class:`discord.User`]: Current user instance + user_instance: typing.Optional[discord.User] - def __init__(self, client): - self.value = None + def __init__(self, client: BotBase) -> None: + """ + Base User type for config. + + :param BotBase client: Client instance + + :Basic usage: + + >>> User(client) #doctest: +SKIP + + """ + self.value = 0 + self.user_instance = None self.client = client - def check_value(self, value): + def check_value(self, value: typing.Union[int, discord.User]) -> bool: + """ + Check if value is correct + + If bot is not connected, always True + + :Basic usage: + + >>> my_user = User(client) #doctest: +SKIP + >>> my_user.check_value(invalid_id_or_user) #doctest: +SKIP + False + >>> my_user.check_value(valid_id_or_user) #doctest: +SKIP + True + + :param value: Value to test + :type value: Union[int, discord.User] + :return: True if user exists + """ + id = value + if isinstance(value, discord.User): + id = value.id + if not self.client.is_ready(): + self.client.warning(f"No check for user {value} because client is not initialized!") + return True + if self.client.get_user(id): + return True return True - def set(self, value): - if self.check_value(value): - self.value = value - return - raise ValueError("Attempt to set incompatible value.") + def set(self, value: typing.Union[int, discord.User]) -> None: + """ + Set value of parameter - def get(self): - return self.value + :Basic usage: - def to_save(self): - return self.value + >>> my_user = User(client) #doctest: +SKIP + >>> my_user.set(valid_id_or_user) #doctest: +SKIP + >>> my_user.set(invalid_id_or_user) #doctest: +SKIP +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ValueError: ... - def load(self, value): + :raise ValueError: if attempt to set invalid value + :param value: value to set + :type value: Union[int, discord.User] + """ + if not self.check_value(value): + raise ValueError("Attempt to set incompatible value.") + if isinstance(value, discord.User): + value = value.id + self.value = value + self._update() + + def get(self) -> typing.Union[int, discord.User]: + """ + Get value of parameter + + :Basic usage: + + >>> my_user = User(client) #doctest: +SKIP + >>> my_user.set(valid_id_or_user) #doctest: +SKIP + >>> my_user.get() #doctest: +SKIP + + + If client is not connected: + >>> my_user = User(client) #doctest: +SKIP + >>> my_user.set(valid_id_or_user) #doctest: +SKIP + >>> my_user.get() #doctest: +SKIP + 23411424132412 + + :return: User object if client is connected, else id + :rtype: Union[int, discord.User] + """ + if self.user_instance is None: + self._update() + return self.user_instance or self.value + + def to_save(self) -> int: + """ + Return id of user + + :Basic usage: + + >>> my_user = User(client) #doctest: +SKIP + >>> my_user.set(valid_id_or_user) #doctest: +SKIP + >>> my_user.to_save() #doctest: +SKIP + 123412412421 + + :return: Current id + :rtype: int + """ + return self.value or 0 + + def load(self, value: typing.Union[int, discord.User]) -> None: + """ + Load value from config + + :Basic usage: + + >>> my_user = User(client) #doctest: +SKIP + >>> my_user.set(valid_id) #doctest: +SKIP + >>> my_user.set(invalid_id) #doctest: +SKIP +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ValueError: ... + + :raise ValueError: if attempt to set invalid value + :param value: value to set + :type value: Union[int, discord.User] + """ if self.check_value(value): raise ValueError("Attempt to load incompatible value.") - self.value = value \ No newline at end of file + self.set(value) + self._update() + + def _update(self): + if self.client.is_ready() and self.user_instance is None: + self.user_instance = self.client.get_user(self.value) + else: + self.user_instance = None + + def __repr__(self): + return f'' \ No newline at end of file