Improved project file structure
This commit is contained in:
1
tests/custom/discord/src/discord_bot/__init__.py
Normal file
1
tests/custom/discord/src/discord_bot/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
40
tests/custom/discord/src/discord_bot/application.py
Normal file
40
tests/custom/discord/src/discord_bot/application.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from cpl_core.application import ApplicationABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.application.discord_bot_application_abc import DiscordBotApplicationABC
|
||||
from cpl_discord.configuration.discord_bot_settings import DiscordBotSettings
|
||||
from cpl_discord.service.discord_bot_service import DiscordBotService
|
||||
from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC
|
||||
|
||||
|
||||
class Application(DiscordBotApplicationABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
self._bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC)
|
||||
self._logger: LoggerABC = services.get_service(LoggerABC)
|
||||
self._bot_settings: DiscordBotSettings = config.get_configuration(DiscordBotSettings)
|
||||
|
||||
async def configure(self):
|
||||
pass
|
||||
|
||||
async def main(self):
|
||||
try:
|
||||
self._logger.debug(__name__, f'Starting...\n')
|
||||
self._logger.trace(__name__, f'Try to start {DiscordBotService.__name__}')
|
||||
await self._bot.start_async()
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, 'Start failed', e)
|
||||
|
||||
async def stop_async(self):
|
||||
try:
|
||||
self._logger.trace(__name__, f'Try to stop {DiscordBotService.__name__}')
|
||||
await self._bot.close()
|
||||
self._logger.trace(__name__, f'Stopped {DiscordBotService.__name__}')
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, 'stop failed', e)
|
||||
|
||||
Console.write_line()
|
18
tests/custom/discord/src/discord_bot/appsettings.json
Normal file
18
tests/custom/discord/src/discord_bot/appsettings.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"TimeFormatSettings": {
|
||||
"DateFormat": "%Y-%m-%d",
|
||||
"TimeFormat": "%H:%M:%S",
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_dev.log",
|
||||
"ConsoleLogLevel": "TRACE",
|
||||
"FileLogLevel": "TRACE"
|
||||
},
|
||||
"DiscordBotSettings": {
|
||||
"Token": "",
|
||||
"Prefix": "!cd "
|
||||
}
|
||||
}
|
46
tests/custom/discord/src/discord_bot/discord-bot.json
Normal file
46
tests/custom/discord/src/discord_bot/discord-bot.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "discord-bot",
|
||||
"Version": {
|
||||
"Major": "0",
|
||||
"Minor": "0",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"URL": "",
|
||||
"CopyrightDate": "",
|
||||
"CopyrightName": "",
|
||||
"LicenseName": "",
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2022.7.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli>=2022.7.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "discord.main",
|
||||
"EntryPoint": "discord",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
30
tests/custom/discord/src/discord_bot/main.py
Normal file
30
tests/custom/discord/src/discord_bot/main.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import asyncio
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.application import ApplicationBuilder, ApplicationABC
|
||||
|
||||
from discord_bot.application import Application
|
||||
from discord_bot.startup import Startup
|
||||
|
||||
|
||||
class Main:
|
||||
|
||||
def __init__(self):
|
||||
self._app: Optional[Application] = None
|
||||
|
||||
async def main(self):
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
self._app: ApplicationABC = await app_builder.build_async()
|
||||
await self._app.run_async()
|
||||
|
||||
async def stop(self):
|
||||
await self._app.stop_async()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main = Main()
|
||||
try:
|
||||
asyncio.run(main.main())
|
||||
except KeyboardInterrupt:
|
||||
asyncio.run(main.stop())
|
34
tests/custom/discord/src/discord_bot/startup.py
Normal file
34
tests/custom/discord/src/discord_bot/startup.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
from cpl_discord import get_discord_collection
|
||||
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
|
||||
from modules.hello_world.on_ready_event import OnReadyEvent
|
||||
from modules.hello_world.on_ready_test_event import OnReadyTestEvent
|
||||
from modules.hello_world.ping_command import PingCommand
|
||||
from modules.hello_world.purge_command import PurgeCommand
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
|
||||
configuration.add_json_file('appsettings.json', optional=True)
|
||||
configuration.add_environment_variables('CPL_')
|
||||
configuration.add_environment_variables('DISCORD_')
|
||||
|
||||
return configuration
|
||||
|
||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||
services.add_logging()
|
||||
services.add_discord()
|
||||
dc_collection = get_discord_collection(services)
|
||||
dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyEvent)
|
||||
dc_collection.add_event(DiscordEventTypesEnum.on_ready.value, OnReadyTestEvent)
|
||||
dc_collection.add_command(PingCommand)
|
||||
dc_collection.add_command(PurgeCommand)
|
||||
|
||||
return services.build_service_provider()
|
0
tests/custom/discord/src/modules/__init__.py
Normal file
0
tests/custom/discord/src/modules/__init__.py
Normal file
1
tests/custom/discord/src/modules/hello_world/__init__.py
Normal file
1
tests/custom/discord/src/modules/hello_world/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "hello-world",
|
||||
"Version": {
|
||||
"Major": "0",
|
||||
"Minor": "0",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"URL": "",
|
||||
"CopyrightDate": "",
|
||||
"CopyrightName": "",
|
||||
"LicenseName": "",
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2022.7.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli>=2022.7.0.post1"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "library",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "hello_world.main",
|
||||
"EntryPoint": "hello-world",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
import discord
|
||||
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.events.on_ready_abc import OnReadyABC
|
||||
from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC
|
||||
|
||||
|
||||
class OnReadyEvent(OnReadyABC):
|
||||
|
||||
def __init__(self, logger: LoggerABC, bot: DiscordBotServiceABC):
|
||||
OnReadyABC.__init__(self)
|
||||
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):
|
||||
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('---Rolemember', rm, type(rm))
|
||||
|
||||
for m in g.members:
|
||||
self._log('--Member', m, type(m))
|
||||
for mr in m.roles:
|
||||
self._log('--Memberole', mr, type(mr))
|
||||
for rm in mr.members:
|
||||
self._log('---Rolemember', rm, type(rm))
|
||||
|
||||
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}')
|
@@ -0,0 +1,12 @@
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.events.on_ready_abc import OnReadyABC
|
||||
|
||||
|
||||
class OnReadyTestEvent(OnReadyABC):
|
||||
|
||||
def __init__(self, logger: LoggerABC):
|
||||
OnReadyABC.__init__(self)
|
||||
self._logger = logger
|
||||
|
||||
async def on_ready(self):
|
||||
self._logger.info(__name__, 'Test second on ready')
|
28
tests/custom/discord/src/modules/hello_world/ping_command.py
Normal file
28
tests/custom/discord/src/modules/hello_world/ping_command.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands import Context
|
||||
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.command.discord_command_abc import DiscordCommandABC
|
||||
from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC
|
||||
|
||||
|
||||
class PingCommand(DiscordCommandABC):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
logger: LoggerABC,
|
||||
bot: DiscordBotServiceABC,
|
||||
):
|
||||
DiscordCommandABC.__init__(self)
|
||||
|
||||
self._logger = logger
|
||||
self._bot = bot
|
||||
|
||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||
|
||||
@commands.hybrid_command()
|
||||
async def ping(self, ctx: Context):
|
||||
self._logger.debug(__name__, f'Received command ping {ctx}')
|
||||
self._logger.info(__name__, f'Bot name {self._bot.user.name}')
|
||||
self._logger.trace(__name__, f'Finished ping command')
|
||||
await ctx.send('Pong')
|
@@ -0,0 +1,31 @@
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands import Context
|
||||
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.command.discord_command_abc import DiscordCommandABC
|
||||
from cpl_discord.service.discord_bot_service_abc import DiscordBotServiceABC
|
||||
|
||||
|
||||
class PurgeCommand(DiscordCommandABC):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
logger: LoggerABC,
|
||||
bot: DiscordBotServiceABC,
|
||||
):
|
||||
DiscordCommandABC.__init__(self)
|
||||
|
||||
self._logger = logger
|
||||
self._bot = bot
|
||||
|
||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||
|
||||
@commands.hybrid_command()
|
||||
async def purge(self, ctx: Context):
|
||||
self._logger.debug(__name__, f'Received command ping {ctx}')
|
||||
self._logger.info(__name__, f'Bot name {self._bot.user.name}')
|
||||
self._logger.trace(__name__, f'Finished ping command')
|
||||
await ctx.channel.purge()
|
||||
if ctx.interaction is None:
|
||||
return
|
||||
await ctx.interaction.response.send_message('Purged this channel xD')
|
1
tests/custom/discord/src/tests/__init__.py
Normal file
1
tests/custom/discord/src/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
Reference in New Issue
Block a user