From c17f2a2db6351a1336b1639dbf82055f96cf4117 Mon Sep 17 00:00:00 2001 From: Louis Chauvet Date: Fri, 24 Apr 2020 22:53:57 +0200 Subject: [PATCH] =?UTF-8?q?[config]=20Ajout=20de=20la=20doc=20pour=20Chann?= =?UTF-8?q?el=20[config]=20R=C3=A9paration=20de=20Channel=20et=20Guild?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config_types/discord_types/channel.py | 98 ++++++++++++++++++- .../config_types/discord_types/guild.py | 2 + 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/config/config_types/discord_types/channel.py b/src/config/config_types/discord_types/channel.py index b29c7fc..95898b4 100644 --- a/src/config/config_types/discord_types/channel.py +++ b/src/config/config_types/discord_types/channel.py @@ -11,16 +11,48 @@ if typing.TYPE_CHECKING: class Channel(BaseType): + #: :class:`BotBase`: Client instance for checking client: BotBase + #: :class:`typing.Optional` [:class:`int`]: Current channel id + value: int + #: :class:`typing.Optional` [:class:`discord.TextChannel`]: Current guild instance + channel_instance: typing.Optional[discord.TextChannel] def __init__(self, client): + """ + Base Channel type for config. + + :param BotBase client: Client instance + + :Basic usage: + + >>> Channel(client) #doctest: +SKIP + + """ self.value = 0 self.channel_instance = None self.client = client - def check_value(self, value): + def check_value(self, value: typing.Union[int, discord.TextChannel]) -> bool: + """ + Check if value is correct + + If bot is not connected, always True + + :Basic usage: + + >>> my_channel = Channel(client) #doctest: +SKIP + >>> my_channel.check_value(invalid_id_or_channel) #doctest: +SKIP + False + >>> my_channel.check_value(valid_id_or_channel) #doctest: +SKIP + True + + :param value: Value to test + :type value: Union[int, discord.TextChannel] + :return: True if guild exists + """ id = value - if isinstance(value, discord.Guild): + if isinstance(value, discord.TextChannel): id = value.id if not self.client.is_ready(): self.client.warning(f"No check for channel {value} because client is not initialized!") @@ -30,21 +62,83 @@ class Channel(BaseType): return True def set(self, value): + """ + Set value of parameter + + :Basic usage: + + >>> my_channel = Channel(client) #doctest: +SKIP + >>> my_channel.set(valid_id_or_channel) #doctest: +SKIP + >>> my_channel.set(invalid_id_or_channel) #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.TextChannel] + """ if not self.check_value(value): raise ValueError("Attempt to set incompatible value.") + if isinstance(value, discord.TextChannel): + value = value.id self.value = value self._update() def get(self): + """ + Get value of parameter + + :Basic usage: + + >>> 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 + >>> my_channel.set(valid_id_or_channel) #doctest: +SKIP + >>> my_channel.get() #doctest: +SKIP + 23411424132412 + + :return: Guild object if client is connected, else id + :rtype: Union[int, discord.Guild] + """ if self.channel_instance is None: self._update() return self.channel_instance or self.value def to_save(self): + """ + 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 + 123412412421 + + :return: Current id + :rtype: Optional[int] + """ return self.value or 0 def load(self, value): + """ + Load value from config + :Basic usage: + + >>> my_channel = Channel(client) #doctest: +SKIP + >>> my_channel.set(valid_id) #doctest: +SKIP + >>> my_channel.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.TextChannel] + """ if self.check_value(value): raise ValueError("Attempt to load incompatible value.") self.set(value) diff --git a/src/config/config_types/discord_types/guild.py b/src/config/config_types/discord_types/guild.py index 164a97f..f7df824 100644 --- a/src/config/config_types/discord_types/guild.py +++ b/src/config/config_types/discord_types/guild.py @@ -80,6 +80,8 @@ class Guild(BaseType): """ if not self.check_value(value): raise ValueError("Attempt to set incompatible value.") + if isinstance(value, discord.Guild): + value = value.id self.value = value self._update()