Improved cpl-query & added first types from discord

This commit is contained in:
Sven Heidemann 2022-09-11 21:57:27 +02:00
parent 3c5ca594d5
commit 988965ed52
15 changed files with 211 additions and 3 deletions

View File

@ -0,0 +1 @@
# imports

View File

@ -0,0 +1,11 @@
import discord
from cpl_discord.container.container import Container
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_query.extension import List
class CategoryChannel(discord.CategoryChannel, Container):
def __init__(self, _t: discord.CategoryChannel):
Container.__init__(self, _t, CategoryChannel)

View File

@ -0,0 +1,30 @@
from typing import Callable
class Container:
def __init__(self, _o: object, _t: type):
self._object = _o
self._type = _t
def __to_type(_f: Callable, _t: type):
def wrapper(*args, **kwargs):
result = _f(*args, **kwargs)
return _t(result)
return wrapper
def __getitem__(self, item):
result = self._object[item]
if isinstance(result, type(self._guild)):
result = self._type(result)
return result
def __getattr__(self, item):
result = getattr(self._object, item)
if callable(result):
result = self.__to_type(result, self._type)
return result
def __repr__(self):
return repr(self._object)

View File

@ -0,0 +1,42 @@
import discord
from cpl_discord.container.category_channel import CategoryChannel
from cpl_discord.container.container import Container
from cpl_discord.container.member import Member
from cpl_discord.container.role import Role
from cpl_discord.container.text_channel import TextChannel
from cpl_discord.container.voice_channel import VoiceChannel
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_query.extension import List
class Guild(discord.Guild, Container):
def __init__(self, _t: discord.Guild):
self._object: discord.Guild = _t
Container.__init__(self, _t, Guild)
@property
def categories(self) -> List[CategoryChannel]:
return List(CategoryChannel, ToContainersConverter.convert(self._object.categories, CategoryChannel))
@property
def members(self) -> List[Member]:
return List(Member, ToContainersConverter.convert(self._object.members, Member))
@property
def roles(self) -> List[Role]:
return List(Role, ToContainersConverter.convert(self._object.roles, Role))
@property
def text_channels(self) -> List[TextChannel]:
return List(TextChannel, ToContainersConverter.convert(self._object.text_channels, TextChannel))
@property
def threads(self) -> List[TextChannel]:
return List(TextChannel, ToContainersConverter.convert(self._object.threads, TextChannel))
@property
def voice_channels(self) -> List[VoiceChannel]:
return List(VoiceChannel, ToContainersConverter.convert(self._object.voice_channels, VoiceChannel))

View File

@ -0,0 +1,11 @@
import discord
from cpl_discord.container.container import Container
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_query.extension import List
class Member(discord.Member, Container):
def __init__(self, _t: discord.Member):
Container.__init__(self, _t, Member)

View File

@ -0,0 +1,16 @@
import discord
from cpl_discord.container.container import Container
from cpl_discord.container.member import Member
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_query.extension import List
class Role(discord.Role, Container):
def __init__(self, _t: discord.Role):
Container.__init__(self, _t, Role)
@property
def members(self) -> List[discord.Member]:
return List(discord.Member, ToContainersConverter.convert(self._object.members, Member))

View File

@ -0,0 +1,11 @@
import discord
from cpl_discord.container.container import Container
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_query.extension import List
class TextChannel(discord.TextChannel, Container):
def __init__(self, _t: discord.TextChannel):
Container.__init__(self, _t, TextChannel)

View File

@ -0,0 +1,11 @@
import discord
from cpl_discord.container.container import Container
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_query.extension import List
class VoiceChannel(discord.VoiceChannel, Container):
def __init__(self, _t: discord.VoiceChannel):
Container.__init__(self, _t, VoiceChannel)

View File

@ -0,0 +1,13 @@
from typing import Union, Sequence
from cpl_discord.container.container import Container
class ToContainersConverter:
@staticmethod
def convert(_l: Union[list[object], Sequence[object]], _t: type) -> list[Container]:
values = []
for e in _l:
values.append(_t(e))
return values

View File

@ -0,0 +1 @@
# imports

View File

@ -5,8 +5,11 @@ from cpl_core.console import Console
from cpl_core.environment import ApplicationEnvironmentABC from cpl_core.environment import ApplicationEnvironmentABC
from cpl_core.logging import LoggerABC, LoggingSettings, LoggingLevelEnum from cpl_core.logging import LoggerABC, LoggingSettings, LoggingLevelEnum
from cpl_discord.configuration.discord_bot_settings import DiscordBotSettings from cpl_discord.configuration.discord_bot_settings import DiscordBotSettings
from cpl_discord.container.guild import Guild
from cpl_discord.helper.ToContainersConverter import ToContainersConverter
from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC
from cpl_discord.service.discord_service_abc import DiscordServiceABC from cpl_discord.service.discord_service_abc import DiscordServiceABC
from cpl_query.extension.list import List
class DiscordBotService(DiscordBotServiceABC): class DiscordBotService(DiscordBotServiceABC):
@ -32,6 +35,7 @@ class DiscordBotService(DiscordBotServiceABC):
# setup super # setup super
DiscordBotServiceABC.__init__(self, command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all()) DiscordBotServiceABC.__init__(self, command_prefix=self._discord_settings.prefix, help_command=None, intents=discord.Intents().all())
self._base = super(DiscordBotServiceABC, self)
@staticmethod @staticmethod
def _is_string_invalid(x): def _is_string_invalid(x):
@ -77,3 +81,7 @@ class DiscordBotService(DiscordBotServiceABC):
self._logger.debug(__name__, f'Finished syncing commands') self._logger.debug(__name__, f'Finished syncing commands')
await self._discord_service.on_ready() await self._discord_service.on_ready()
@property
def guilds(self) -> List[Guild]:
return List(Guild, ToContainersConverter.convert(self._base.guilds, Guild))

View File

@ -1,8 +1,10 @@
from abc import ABC, abstractmethod from abc import abstractmethod
import discord
from discord.ext import commands from discord.ext import commands
from cpl_discord.container.guild import Guild
from cpl_query.extension.list import List
class DiscordBotServiceABC(commands.Bot): class DiscordBotServiceABC(commands.Bot):
@ -17,3 +19,7 @@ class DiscordBotServiceABC(commands.Bot):
@abstractmethod @abstractmethod
async def on_ready(self): pass async def on_ready(self): pass
@property
@abstractmethod
def guilds(self) -> List[Guild]: pass

View File

@ -0,0 +1,19 @@
from typing import Callable
from cpl_query.extension.iterable_abc import IterableABC
def select_query(_list: IterableABC, _f: Callable) -> any:
result = IterableABC()
result.extend(_f(_o) for _o in _list)
return result
def select_many_query(_list: IterableABC, _f: Callable) -> any:
result = IterableABC()
# The line below is pain. I don't understand anything of it...
# written on 09.11.2022 by Sven Heidemann
elements = [_a for _o in _list for _a in _f(_o)]
result.extend(elements)
return result

View File

@ -14,6 +14,7 @@ from cpl_query._query.for_each import for_each_query
from cpl_query._query.max_min import max_query, min_query from cpl_query._query.max_min import max_query, min_query
from cpl_query._query.order_by import order_by_descending_query, order_by_query from cpl_query._query.order_by import order_by_descending_query, order_by_query
from cpl_query._query.reverse import reverse_query from cpl_query._query.reverse import reverse_query
from cpl_query._query.select import select_query, select_many_query
from cpl_query._query.single import single_or_default_query, single_query from cpl_query._query.single import single_or_default_query, single_query
from cpl_query._query.skip_take import (skip_last_query, skip_query, from cpl_query._query.skip_take import (skip_last_query, skip_query,
take_last_query, take_query) take_last_query, take_query)
@ -94,6 +95,12 @@ class Iterable(IterableABC):
def single_or_default(self) -> Optional[any]: def single_or_default(self) -> Optional[any]:
return single_or_default_query(self) return single_or_default_query(self)
def select(self, _f: Callable) -> IterableABC:
return self.__to_self(select_query(self, _f))
def select_many(self, _f: Callable) -> IterableABC:
return self.__to_self(select_many_query(self, _f))
def skip(self, index: int) -> IterableABC: def skip(self, index: int) -> IterableABC:
return self.__to_self(skip_query(self, index)) return self.__to_self(skip_query(self, index))

View File

@ -1,12 +1,33 @@
import discord
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_discord.events.on_ready_abc import OnReadyABC from cpl_discord.events.on_ready_abc import OnReadyABC
from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC
class OnReadyEvent(OnReadyABC): class OnReadyEvent(OnReadyABC):
def __init__(self, logger: LoggerABC): def __init__(self, logger: LoggerABC, bot: DiscordBotServiceABC):
OnReadyABC.__init__(self) OnReadyABC.__init__(self)
self._logger = logger self._logger = logger
self._bot = bot
def _log(self, _t: str, _o: object, _type: type = None):
self._logger.debug(__name__, f'{_t} {_o} {_type}')
async def on_ready(self): async def on_ready(self):
self._logger.info(__name__, 'Hello World') self._logger.info(__name__, 'Hello World')
for g in self._bot.guilds:
self._log('-Guild', g, type(g))
for r in g.roles:
self._log('--Role', r, type(r))
for rm in r.members:
self._log('---Rolemembers', rm, type(rm))
for m in g.members:
self._log('--Member', m, type(m))
select = self._bot.guilds.select(lambda guild: (guild.name, guild.id))
self._logger.warn(__name__, f'Does cpl.query select work? {select}')
select_many = self._bot.guilds.select_many(lambda guild: guild.roles).where(lambda role: role.name == "Tester").first()
self._logger.warn(__name__, f'Does cpl.query select_many work? {select_many}')