Added warnings service #35

This commit is contained in:
Sven Heidemann 2023-02-21 18:24:01 +01:00
parent 8ff21debf0
commit 19f47a01e5
4 changed files with 108 additions and 1 deletions

View File

@ -207,6 +207,14 @@
},
"unregister": {
"success": "Verlinkung wurde entfernt :D"
},
"warnings": {
"message": "Du wurdest verwarnt. Der Grund ist: {}",
"messages": {
"first": "Bei der nächsten verwarnung, wirst du auf das vorherige Level zurückgesetzt!",
"second": "Bei der nächsten verwarnung, wirst du auf das erste Level zurückgesetzt!",
"third": "Bei der nächsten verwarnung, wirst du gekickt und zurückgesetzt!"
}
}
},
"boot_log": {

View File

@ -12,6 +12,7 @@ class UserWarnings(TableABC):
self,
description: str,
user: Optional[User],
author: Optional[User],
created_at: datetime = None,
modified_at: datetime = None,
id=0,
@ -19,7 +20,7 @@ class UserWarnings(TableABC):
self._id = id
self._description = description
self._user = user
self._author = user
self._author = author
TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at

View File

@ -0,0 +1,98 @@
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_data.abc.level_repository_abc import LevelRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.abc.user_warning_repository_abc import UserWarningsRepositoryABC
from bot_data.model.user_warnings import UserWarnings
from modules.level.service.level_service import LevelService
class UserWarningsService:
def __init__(
self,
logger: LoggerABC,
db: DatabaseContextABC,
bot: DiscordBotServiceABC,
warnings: UserWarningsRepositoryABC,
users: UserRepositoryABC,
servers: ServerRepositoryABC,
levels: LevelRepositoryABC,
level_service: LevelService,
message_service: MessageServiceABC,
t: TranslatePipe,
):
self._logger = logger
self._db = db
self._bot = bot
self._warnings = warnings
self._users = users
self._servers = servers
self._levels = levels
self._level_service = level_service
self._message_service = message_service
self._t = t
async def add_warnings(self, guild_id: int, member_id: int, description: str, author_id: int = None):
server = self._servers.get_server_by_discord_id(guild_id)
user = self._users.get_user_by_discord_id_and_server_id(member_id, server.id)
guild = self._bot.get_guild(guild_id)
member = guild.get_member(member_id)
author = None
if author_id is not None:
author = self._users.get_user_by_discord_id_and_server_id(author_id, server.id)
warning = UserWarnings(description, user, author)
self._warnings.add_user_warnings(warning)
self._db.save_changes()
existing_warnings = self._warnings.get_user_warnings_by_user_id(user.id)
await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.message").format(warning.description), member
)
match existing_warnings.count():
case 1:
await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.messages.first"), member
)
case 2:
server = self._servers.get_server_by_discord_id(guild_id)
user = self._users.get_user_by_discord_id_and_server_id(member_id, server.id)
level = self._level_service.get_level(user)
levels = self._levels.get_levels_by_server_id(server.id).order_by(lambda l: l.min_xp)
new_level = levels.where(lambda l: l.min_xp < level.min_xp).last()
user.xp = new_level.min_xp
self._users.update_user(user)
self._db.save_changes()
await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.messages.second"), member
)
case 3:
server = self._servers.get_server_by_discord_id(guild_id)
user = self._users.get_user_by_discord_id_and_server_id(member_id, server.id)
levels = self._levels.get_levels_by_server_id(server.id)
new_level = levels.where(lambda l: l.min_xp > 0).order_by(lambda l: l.min_xp).last()
user.xp = new_level.min_xp
self._users.update_user(user)
self._db.save_changes()
await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.messages.third"), member
)
case 4:
user.xp = 0
self._users.update_user(user)
self._db.save_changes()
await member.kick()
def remove_warnings(self, id: int):
warning = self._warnings.get_user_warnings_by_id(id)
self._warnings.delete_user_warnings(warning)