Added discord command & event to cpl g schematics #143

This commit is contained in:
2022-12-08 10:36:23 +01:00
parent 4fbb3ec838
commit 2f8dc63cdc
10 changed files with 146 additions and 16 deletions

View File

View File

@@ -0,0 +1,44 @@
import textwrap
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
class Command(GenerateSchematicABC):
def __init__(self, *args: str):
GenerateSchematicABC.__init__(self, *args)
def get_code(self) -> str:
code = """\
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from discord.ext import commands
from discord.ext.commands import Context
class $Name(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._bot = bot
@commands.hybrid_command()
async def ping(self, ctx: Context):
await ctx.send('Pong')
"""
return self.build_code_str(code, Name=self._class_name)
@classmethod
def register(cls):
GenerateSchematicABC.register(
cls,
'command',
[]
)

View File

@@ -0,0 +1,63 @@
import sys
import textwrap
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
from cpl_core.console import Console
from cpl_core.utils import String
class Command(GenerateSchematicABC):
def __init__(self, name: str, schematic: str, path: str):
GenerateSchematicABC.__init__(self, name, schematic, path)
event = None
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
for event_type in DiscordEventTypesEnum:
event_name = event_type.value.__name__.replace("ABC", '')
if event_name in name:
name = name.replace(event_name, "")
event = event_name
break
if event is None:
Console.error(f'No valid event found in name {name}')
sys.exit()
self._event_class = f'{event}ABC'
self._name = f'{String.convert_to_snake_case(name)}_{String.convert_to_snake_case(self._event_class.replace("ABC", ""))}_{schematic}.py'
self._class_name = f'{String.first_to_upper(name)}{self._event_class.replace("ABC", "")}{String.first_to_upper(schematic)}'
def get_code(self) -> str:
code = """\
from cpl_core.logging import LoggerABC
from cpl_discord.events import $EventClass
from cpl_discord.service import DiscordBotServiceABC
class $Name($EventClass):
def __init__(
self,
logger: LoggerABC,
bot: DiscordBotServiceABC,
):
OnReadyABC.__init__(self)
self._logger = logger
self._bot = bot
async def on_ready(self):
pass
"""
return self.build_code_str(code, Name=self._class_name, EventClass=self._event_class)
@classmethod
def register(cls):
GenerateSchematicABC.register(
cls,
'event',
[]
)

View File

@@ -9,4 +9,4 @@ class OnGroupJoinABC(ABC):
@abstractmethod
async def on_group_join(
self, chhanel: discord.GroupChannel, user: discord.User): pass
self, channel: discord.GroupChannel, user: discord.User): pass