[config] Ajout de role
This commit is contained in:
parent
c17f2a2db6
commit
dcd36dd13b
@ -0,0 +1 @@
|
||||
import bot_base
|
@ -18,7 +18,7 @@ class Channel(BaseType):
|
||||
#: :class:`typing.Optional` [:class:`discord.TextChannel`]: Current guild instance
|
||||
channel_instance: typing.Optional[discord.TextChannel]
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client: BotBase) -> None:
|
||||
"""
|
||||
Base Channel type for config.
|
||||
|
||||
@ -61,7 +61,7 @@ class Channel(BaseType):
|
||||
return True
|
||||
return True
|
||||
|
||||
def set(self, value):
|
||||
def set(self, value: typing.Union[int, discord.TextChannel]):
|
||||
"""
|
||||
Set value of parameter
|
||||
|
||||
@ -84,7 +84,7 @@ class Channel(BaseType):
|
||||
self.value = value
|
||||
self._update()
|
||||
|
||||
def get(self):
|
||||
def get(self) -> typing.Union[int, discord.Channel]:
|
||||
"""
|
||||
Get value of parameter
|
||||
|
||||
@ -101,14 +101,14 @@ class Channel(BaseType):
|
||||
>>> my_channel.get() #doctest: +SKIP
|
||||
23411424132412
|
||||
|
||||
:return: Guild object if client is connected, else id
|
||||
:rtype: Union[int, discord.Guild]
|
||||
:return: Channel object if client is connected, else id
|
||||
:rtype: Union[int, discord.Channel]
|
||||
"""
|
||||
if self.channel_instance is None:
|
||||
self._update()
|
||||
return self.channel_instance or self.value
|
||||
|
||||
def to_save(self):
|
||||
def to_save(self) -> int:
|
||||
"""
|
||||
Return id of channel
|
||||
|
||||
@ -119,11 +119,11 @@ class Channel(BaseType):
|
||||
123412412421
|
||||
|
||||
:return: Current id
|
||||
:rtype: Optional[int]
|
||||
:rtype: int
|
||||
"""
|
||||
return self.value or 0
|
||||
|
||||
def load(self, value):
|
||||
def load(self, value: typing.Union[int, discord.Channel]) -> None:
|
||||
"""
|
||||
Load value from config
|
||||
|
||||
@ -150,3 +150,5 @@ class Channel(BaseType):
|
||||
else:
|
||||
self.channel_instance = None
|
||||
|
||||
def __repr__(self):
|
||||
return f'<config_types.discord_types.Channel object with value {self.value}>'
|
||||
|
@ -62,7 +62,7 @@ class Guild(BaseType):
|
||||
return True
|
||||
return True
|
||||
|
||||
def set(self, value: typing.Union[int, discord.Guild]):
|
||||
def set(self, value: typing.Union[int, discord.Guild]) -> None:
|
||||
"""
|
||||
Set value of parameter
|
||||
|
||||
@ -108,7 +108,7 @@ class Guild(BaseType):
|
||||
self._update()
|
||||
return self.guild_instance or self.value
|
||||
|
||||
def to_save(self) -> typing.Optional:
|
||||
def to_save(self) -> int:
|
||||
"""
|
||||
Return id of guild
|
||||
|
||||
@ -119,11 +119,11 @@ class Guild(BaseType):
|
||||
123412412421
|
||||
|
||||
:return: Current id
|
||||
:rtype: Optional[int]
|
||||
:rtype: int
|
||||
"""
|
||||
return self.value or 0
|
||||
|
||||
def load(self, value):
|
||||
def load(self, value: typing.Union[int, discord.Guild]) -> None:
|
||||
"""
|
||||
Load value from config
|
||||
|
||||
@ -145,7 +145,7 @@ class Guild(BaseType):
|
||||
self._update()
|
||||
|
||||
def __repr__(self):
|
||||
return f'<config_types.discord_types.guild object with value {self.value}>'
|
||||
return f'<config_types.discord_types.Guild object with value {self.value}>'
|
||||
|
||||
def _update(self):
|
||||
if self.client.is_ready() and self.guild_instance is None:
|
||||
|
@ -1,34 +1,152 @@
|
||||
from __future__ import annotations
|
||||
import typing
|
||||
|
||||
import discord
|
||||
|
||||
from config.config_types.base_type import BaseType
|
||||
if typing.TYPE_CHECKING:
|
||||
from bot_base import BotBase
|
||||
|
||||
|
||||
class Role(BaseType):
|
||||
#: :class:`BotBase`: Client instance for checking
|
||||
client: BotBase
|
||||
#: :class:`typing.Optional` [:class:`int`]: Current channel id
|
||||
value: int
|
||||
#: :class:`typing.Optional` [:class:`discord.Role`]: Current guild instance
|
||||
role_instance: typing.Optional[discord.Role]
|
||||
|
||||
def __init__(self, client):
|
||||
self.value = None
|
||||
def __init__(self, client: BotBase) -> None:
|
||||
"""
|
||||
Base Role type for config.
|
||||
|
||||
:param BotBase client: Client instance
|
||||
|
||||
:Basic usage:
|
||||
|
||||
>>> Role(client) #doctest: +SKIP
|
||||
<config_types.discord_type.Role object with value None>
|
||||
"""
|
||||
self.value = 0
|
||||
self.role_instance = None
|
||||
self.client = client
|
||||
|
||||
def check_value(self, value):
|
||||
def check_value(self, value: typing.Union[int, discord.Role]) -> bool:
|
||||
"""
|
||||
Check if value is correct
|
||||
|
||||
If bot is not connected, always True
|
||||
|
||||
:Basic usage:
|
||||
|
||||
>>> my_role = Role(client) #doctest: +SKIP
|
||||
>>> my_role.check_value(invalid_id_or_role) #doctest: +SKIP
|
||||
False
|
||||
>>> my_role.check_value(valid_id_or_role) #doctest: +SKIP
|
||||
True
|
||||
|
||||
:param value: Value to test
|
||||
:type value: Union[int, discord.Role]
|
||||
:return: True if guild exists
|
||||
"""
|
||||
id = value
|
||||
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!")
|
||||
return True
|
||||
if self.client.get_channel(id):
|
||||
return True
|
||||
return True
|
||||
|
||||
def set(self, value):
|
||||
if self.check_value(value):
|
||||
self.value = value
|
||||
return
|
||||
def set(self, value: typing.Union[int, discord.Role]) -> None:
|
||||
"""
|
||||
Set value of parameter
|
||||
|
||||
:Basic usage:
|
||||
|
||||
>>> my_role = Role(client) #doctest: +SKIP
|
||||
>>> my_role.set(valid_id_or_role) #doctest: +SKIP
|
||||
>>> my_role.set(invalid_id_or_role) #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.Role]
|
||||
"""
|
||||
if not self.check_value(value):
|
||||
raise ValueError("Attempt to set incompatible value.")
|
||||
if isinstance(value, discord.Role):
|
||||
value = value.id
|
||||
self.value = value
|
||||
self._update()
|
||||
|
||||
def get(self):
|
||||
return self.value
|
||||
def get(self) -> typing.Union[int, discord.Role]:
|
||||
"""
|
||||
Get value of parameter
|
||||
|
||||
def to_save(self):
|
||||
return self.value
|
||||
:Basic usage:
|
||||
|
||||
def load(self, value):
|
||||
>>> my_role = Role(client) #doctest: +SKIP
|
||||
>>> my_role.set(valid_id_or_role) #doctest: +SKIP
|
||||
>>> my_role.get() #doctest: +SKIP
|
||||
<discord.guild.Guild at 0x...>
|
||||
|
||||
If client is not connected:
|
||||
>>> my_role = Role(client) #doctest: +SKIP
|
||||
>>> my_role.set(valid_id_or_role) #doctest: +SKIP
|
||||
>>> my_role.get() #doctest: +SKIP
|
||||
23411424132412
|
||||
|
||||
:return: Role 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) -> int:
|
||||
"""
|
||||
Return id of channel
|
||||
|
||||
:Basic usage:
|
||||
>>> my_role = Role(client) #doctest: +SKIP
|
||||
>>> my_role.set(valid_id_or_role) #doctest: +SKIP
|
||||
>>> my_role.to_save() #doctest: +SKIP
|
||||
123412412421
|
||||
|
||||
:return: Current id
|
||||
:rtype: int
|
||||
"""
|
||||
return self.value or 0
|
||||
|
||||
def load(self, value: typing.Union[int, discord.Role]) -> None:
|
||||
"""
|
||||
Load value from config
|
||||
|
||||
:Basic usage:
|
||||
|
||||
>>> my_role = Role(client) #doctest: +SKIP
|
||||
>>> my_role.set(valid_id) #doctest: +SKIP
|
||||
>>> my_role.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.Role]
|
||||
"""
|
||||
if self.check_value(value):
|
||||
raise ValueError("Attempt to load incompatible value.")
|
||||
self.value = value
|
||||
self.set(value)
|
||||
self._update()
|
||||
|
||||
def _update(self):
|
||||
if self.client.is_ready() and self.role_instance is None:
|
||||
self.channel_instance = self.client.get_role(self.value)
|
||||
else:
|
||||
self.channel_instance = None
|
||||
|
||||
def __repr__(self):
|
||||
return f'<config_types.discord_types.Channel object with value {self.value}>'
|
Loading…
Reference in New Issue
Block a user