Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Simply a customs agent
DISCORD_TOKEN=YOUR DISCORD BOT TOKEN
SERVER_ID=YOUR SERVER ID (RIGHT CLICK ON SERVER ICON AND COPY ID)
LOGGER_FILENAME=YOUR LOG FILENAME
DASHBOARD_PORT=PORT OF THE DASHBOARD ON THE LOCAL MACHINE
```

:warning: Do not add space between `=`. Even for string, **do not** add `"`.
Expand Down Expand Up @@ -104,9 +105,14 @@ If this happens, script will ask you if you want to contact them by hand or auto

## Commands
To fix linter and format errors run these two commands:
```commandline
```sh
ruff check . --fix
```
```commandline
```sh
black .
```

To run tests:
```sh
pytest
```
27 changes: 27 additions & 0 deletions bot/application/miscellaneous/miscellaneous_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import socket

from bot.config.logger.logger import Logger
from bot.config.service_locator import ServiceLocator
from bot.domain.discord_client.discord_client import DiscordClient


class MiscellaneousService:

DOCKER_HOST: str = "host.docker.internal"

def __init__(self, discord_client: DiscordClient, dashboard_port: int):
self.__logger: Logger = ServiceLocator.get_dependency(Logger)
self.__discord_client = discord_client

self.__dashboard_board: int = dashboard_port

def __get_host_ip_address(self) -> str:
try:
ip_address: str = socket.gethostbyname(self.DOCKER_HOST)
return f"http://{ip_address}"
except socket.gaierror:
return "http://127.0.0.1"

def retrieve_dashboard_url(self) -> str:
ip_address: str = self.__get_host_ip_address()
return f"{ip_address}:{self.__dashboard_board}"
8 changes: 4 additions & 4 deletions bot/application/student/student_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

class StudentService(StudentRegisteredObservable, MemberRemovedObservable):

def __init__(self, student_repository: StudentRepository):
def __init__(
self, discord_client: DiscordClient, student_repository: StudentRepository
):
super().__init__()

self.__logger: Logger = ServiceLocator.get_dependency(Logger)
self.__discord_client: DiscordClient = ServiceLocator.get_dependency(
DiscordClient
)
self.__discord_client = discord_client

self.__student_repository = student_repository

Expand Down
1 change: 1 addition & 0 deletions bot/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ class DotenvConfigurationKey:
DISCORD_TOKEN: str = "DISCORD_TOKEN"
SERVER_ID: str = "SERVER_ID"
LOGGER_FILENAME: str = "LOGGER_FILENAME"
DASHBOARD_PORT: str = "DASHBOARD_PORT"
21 changes: 19 additions & 2 deletions bot/config/environment/context/application_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pymongo.collection import Collection

from bot.application.discord.discord_service import DiscordService
from bot.application.miscellaneous.miscellaneous_service import MiscellaneousService
from bot.application.student.exception.invalid_format_exception import (
InvalidFormatException,
)
Expand All @@ -30,6 +31,7 @@
)
from bot.resource.cog.association.association import AssociationCog
from bot.resource.cog.error_handler.error_handler import ErrorHandlerCog
from bot.resource.cog.miscellaneous.miscellaneous import MiscellaneousCog
from bot.resource.cog.registration.register_member import RegisterMemberCog
from bot.config.environment.dotenv_configuration import DotEnvConfiguration
from bot.config.logger.logger import Logger
Expand Down Expand Up @@ -83,10 +85,13 @@ async def build_application(self):

task_scheduler = TaskScheduler()

student_service = self._instantiate_student_service(student_repository)
student_service = self._instantiate_student_service(
discord_client, student_repository
)
discord_service = self._instantiate_discord_service(
discord_client, student_repository
)
miscellaneous_service = self._instantiate_miscellaneous_service(discord_client)

student_service.register_to_on_student_registered(discord_service)

Expand All @@ -96,12 +101,14 @@ async def build_application(self):
(StudentRepository, student_repository),
(StudentService, student_service),
(DiscordService, discord_service),
(MiscellaneousService, miscellaneous_service),
]
self.__register_dependencies(dependencies)

cogs = [
self._instantiate_register_member_cog(),
self._instantiate_association_cog(),
self._instantiate_miscellaneous_cog(),
self._instantiate_error_handler_cog(),
]
await self.__register_cogs(discord_client, cogs)
Expand Down Expand Up @@ -142,6 +149,10 @@ def _instantiate_association_cog(self) -> AssociationCog:
def _instantiate_register_member_cog(self) -> RegisterMemberCog:
pass

@abstractmethod
def _instantiate_miscellaneous_cog(self) -> MiscellaneousCog:
pass

@abstractmethod
def _instantiate_error_handler_cog(self) -> ErrorHandlerCog:
pass
Expand All @@ -162,7 +173,7 @@ def _instantiate_student_repository(

@abstractmethod
def _instantiate_student_service(
self, student_repository: StudentRepository
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> StudentService:
pass

Expand All @@ -172,6 +183,12 @@ def _instantiate_discord_service(
) -> DiscordService:
pass

@abstractmethod
def _instantiate_miscellaneous_service(
self, discord_client: DiscordClient
) -> MiscellaneousService:
pass

@abstractmethod
def _instantiate_tasks(self, discord_client: DiscordClient) -> List[Task]:
pass
14 changes: 12 additions & 2 deletions bot/config/environment/context/development_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from pymongo.collection import Collection

from bot.application.discord.discord_service import DiscordService
from bot.application.miscellaneous.miscellaneous_service import MiscellaneousService
from bot.application.student.student_service import StudentService
from bot.application.task.kick_unregistered_user_task import KickUnregisteredUserTask
from bot.application.task.sentence_of_the_day_task import SentenceOfTheDayTask
from bot.domain.task.task import Task
from bot.resource.cog.association.association import AssociationCog
from bot.resource.cog.error_handler.error_handler import ErrorHandlerCog
from bot.resource.cog.miscellaneous.miscellaneous import MiscellaneousCog
from bot.resource.cog.registration.register_member import RegisterMemberCog
from bot.config.environment.context.application_context import ApplicationContext
from bot.config.constants import ConfigurationFilename
Expand Down Expand Up @@ -53,6 +55,9 @@ def _instantiate_association_cog(self) -> AssociationCog:
def _instantiate_register_member_cog(self) -> RegisterMemberCog:
return RegisterMemberCog()

def _instantiate_miscellaneous_cog(self) -> MiscellaneousCog:
return MiscellaneousCog()

def _instantiate_error_handler_cog(self) -> ErrorHandlerCog:
return ErrorHandlerCog()

Expand All @@ -63,15 +68,20 @@ def _instantiate_student_repository(
return CachedStudentRepository(student_repository)

def _instantiate_student_service(
self, student_repository: StudentRepository
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> StudentService:
return StudentService(student_repository)
return StudentService(discord_client, student_repository)

def _instantiate_discord_service(
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> DiscordService:
return DiscordService(discord_client, student_repository)

def _instantiate_miscellaneous_service(
self, discord_client: DiscordClient
) -> MiscellaneousService:
return MiscellaneousService(discord_client, self._configuration.dashboard_port)

def _instantiate_tasks(self, discord_client: DiscordClient) -> List[Task]:
return [
KickUnregisteredUserTask(
Expand Down
14 changes: 12 additions & 2 deletions bot/config/environment/context/docker_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from pymongo.collection import Collection

from bot.application.discord.discord_service import DiscordService
from bot.application.miscellaneous.miscellaneous_service import MiscellaneousService
from bot.application.student.student_service import StudentService
from bot.application.task.kick_unregistered_user_task import KickUnregisteredUserTask
from bot.application.task.sentence_of_the_day_task import SentenceOfTheDayTask
from bot.domain.task.task import Task
from bot.resource.cog.association.association import AssociationCog
from bot.resource.cog.error_handler.error_handler import ErrorHandlerCog
from bot.resource.cog.miscellaneous.miscellaneous import MiscellaneousCog
from bot.resource.cog.registration.register_member import RegisterMemberCog
from bot.config.environment.context.application_context import ApplicationContext
from bot.config.logger.logger import Logger
Expand Down Expand Up @@ -51,6 +53,9 @@ def _instantiate_association_cog(self) -> AssociationCog:
def _instantiate_register_member_cog(self) -> RegisterMemberCog:
return RegisterMemberCog()

def _instantiate_miscellaneous_cog(self) -> MiscellaneousCog:
return MiscellaneousCog()

def _instantiate_error_handler_cog(self) -> ErrorHandlerCog:
return ErrorHandlerCog()

Expand All @@ -61,15 +66,20 @@ def _instantiate_student_repository(
return CachedStudentRepository(student_repository)

def _instantiate_student_service(
self, student_repository: StudentRepository
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> StudentService:
return StudentService(student_repository)
return StudentService(discord_client, student_repository)

def _instantiate_discord_service(
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> DiscordService:
return DiscordService(discord_client, student_repository)

def _instantiate_miscellaneous_service(
self, discord_client: DiscordClient
) -> MiscellaneousService:
return MiscellaneousService(discord_client, self._configuration.dashboard_port)

def _instantiate_tasks(self, discord_client: DiscordClient) -> List[Task]:
return [
KickUnregisteredUserTask(
Expand Down
14 changes: 12 additions & 2 deletions bot/config/environment/context/production_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from pymongo.collection import Collection

from bot.application.discord.discord_service import DiscordService
from bot.application.miscellaneous.miscellaneous_service import MiscellaneousService
from bot.application.student.student_service import StudentService
from bot.application.task.kick_unregistered_user_task import KickUnregisteredUserTask
from bot.application.task.sentence_of_the_day_task import SentenceOfTheDayTask
from bot.domain.task.task import Task
from bot.resource.cog.association.association import AssociationCog
from bot.resource.cog.error_handler.error_handler import ErrorHandlerCog
from bot.resource.cog.miscellaneous.miscellaneous import MiscellaneousCog
from bot.resource.cog.registration.register_member import RegisterMemberCog
from bot.config.environment.context.application_context import ApplicationContext
from bot.config.constants import ConfigurationFilename
Expand Down Expand Up @@ -53,6 +55,9 @@ def _instantiate_association_cog(self) -> AssociationCog:
def _instantiate_register_member_cog(self) -> RegisterMemberCog:
return RegisterMemberCog()

def _instantiate_miscellaneous_cog(self) -> MiscellaneousCog:
return MiscellaneousCog()

def _instantiate_error_handler_cog(self) -> ErrorHandlerCog:
return ErrorHandlerCog()

Expand All @@ -63,15 +68,20 @@ def _instantiate_student_repository(
return CachedStudentRepository(student_repository)

def _instantiate_student_service(
self, student_repository: StudentRepository
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> StudentService:
return StudentService(student_repository)
return StudentService(discord_client, student_repository)

def _instantiate_discord_service(
self, discord_client: DiscordClient, student_repository: StudentRepository
) -> DiscordService:
return DiscordService(discord_client, student_repository)

def _instantiate_miscellaneous_service(
self, discord_client: DiscordClient
) -> MiscellaneousService:
return MiscellaneousService(discord_client, self._configuration.dashboard_port)

def _instantiate_tasks(self, discord_client: DiscordClient) -> List[Task]:
return [
KickUnregisteredUserTask(
Expand Down
4 changes: 4 additions & 0 deletions bot/config/environment/dotenv_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ def server_id(self) -> int:
@property
def logger_filename(self) -> str:
return self.__get_string(DotenvConfigurationKey.LOGGER_FILENAME)

@property
def dashboard_port(self) -> int:
return self.__get_int(DotenvConfigurationKey.DASHBOARD_PORT)
2 changes: 0 additions & 2 deletions bot/resource/cog/association/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
from bot.resource.cog.association.embed.asetin_embed import AsetinEmbed
from bot.config.logger.logger import Logger
from bot.config.service_locator import ServiceLocator
from bot.domain.discord_client.discord_client import DiscordClient
from bot.resource.decorator.prohibit_self_message import prohibit_self_message


class AssociationCog(commands.Cog, name="Association"):

def __init__(self):
self.__bot = ServiceLocator.get_dependency(DiscordClient)
self.__logger: Logger = ServiceLocator.get_dependency(Logger)

@commands.command(
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions bot/resource/cog/miscellaneous/miscellaneous.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from discord import Message
from discord.ext import commands
from discord.ext.commands import Context

from bot.application.miscellaneous.miscellaneous_service import MiscellaneousService
from bot.config.logger.logger import Logger
from bot.config.service_locator import ServiceLocator
from bot.domain.constants import DiscordRole
from bot.domain.discord_client.discord_client import DiscordClient
from bot.resource.decorator.prohibit_self_message import prohibit_self_message
from bot.resource.decorator.role_check import role_check


class MiscellaneousCog(commands.Cog, name="Miscellaneous"):
def __init__(self):
self.__bot = ServiceLocator.get_dependency(DiscordClient)
self.__logger: Logger = ServiceLocator.get_dependency(Logger)
self.__miscellaneous_service: MiscellaneousService = (
ServiceLocator.get_dependency(MiscellaneousService)
)

@commands.command(
name="dashboard",
help="!dashboard",
brief="Retourne le lien du dashboard.",
)
@commands.dm_only()
@prohibit_self_message()
@role_check(DiscordRole.ASETIN, DiscordRole.ADMIN)
async def get_dashboard_url(self, context: Context):
self.__logger.info(
f"Exécution de la commande par {context.message.author}",
method="get_dashboard_url",
)

message: Message = context.message

dashboard_url: str = self.__miscellaneous_service.retrieve_dashboard_url()
await message.channel.send(dashboard_url)

self.__logger.info(
"La commande a été exécutée avec succès.", method="get_dashboard_url"
)
4 changes: 4 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ services:
volumes:
- /etc/localtime:/etc/localtime:ro
- ./logs:/buggybot/log
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
- HOST_IP=host.docker.internal
restart: always
Loading
Loading