Improved warning handling #35

This commit is contained in:
Sven Heidemann 2023-02-22 07:25:24 +01:00
parent 19f47a01e5
commit 1eb625fe7a
3 changed files with 45 additions and 33 deletions

View File

@ -213,7 +213,8 @@
"messages": { "messages": {
"first": "Bei der nächsten verwarnung, wirst du auf das vorherige Level zurückgesetzt!", "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!", "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!" "third": "Bei der nächsten verwarnung, wirst du gekickt und zurückgesetzt!",
"kick": "Ich musste {} aufgrund zu vieler Verwarnungen kicken"
} }
} }
}, },

View File

@ -57,4 +57,4 @@ class BaseOnVoiceStateUpdateEventHelpChannel(OnVoiceStateUpdateABC):
a, a,
) )
self._logger.debug(__name__, f"Module {type(self)} stopped") self._logger.debug(__name__, f"Module {type(self)} stopped")

View File

@ -10,6 +10,7 @@ from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.abc.user_warning_repository_abc import UserWarningsRepositoryABC from bot_data.abc.user_warning_repository_abc import UserWarningsRepositoryABC
from bot_data.model.user_warnings import UserWarnings from bot_data.model.user_warnings import UserWarnings
from modules.level.service.level_service import LevelService from modules.level.service.level_service import LevelService
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class UserWarningsService: class UserWarningsService:
@ -25,6 +26,7 @@ class UserWarningsService:
level_service: LevelService, level_service: LevelService,
message_service: MessageServiceABC, message_service: MessageServiceABC,
t: TranslatePipe, t: TranslatePipe,
permissions: PermissionServiceABC,
): ):
self._logger = logger self._logger = logger
self._db = db self._db = db
@ -36,6 +38,7 @@ class UserWarningsService:
self._level_service = level_service self._level_service = level_service
self._message_service = message_service self._message_service = message_service
self._t = t self._t = t
self._permissions = permissions
async def add_warnings(self, guild_id: int, member_id: int, description: str, author_id: int = None): 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) server = self._servers.get_server_by_discord_id(guild_id)
@ -57,41 +60,49 @@ class UserWarningsService:
self._t.transform("modules.base.warnings.message").format(warning.description), member self._t.transform("modules.base.warnings.message").format(warning.description), member
) )
match existing_warnings.count(): if existing_warnings.count() == 1:
case 1: await self._message_service.send_dm_message(
await self._message_service.send_dm_message( self._t.transform("modules.base.warnings.messages.first"), member
self._t.transform("modules.base.warnings.messages.first"), member )
) elif existing_warnings.count() == 2:
case 2: server = self._servers.get_server_by_discord_id(guild_id)
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)
user = self._users.get_user_by_discord_id_and_server_id(member_id, server.id) level = self._level_service.get_level(user)
level = self._level_service.get_level(user) levels = self._levels.get_levels_by_server_id(server.id).order_by(lambda l: l.min_xp)
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() new_level = levels.where(lambda l: l.min_xp < level.min_xp).last()
user.xp = new_level.min_xp user.xp = new_level.min_xp
self._users.update_user(user) self._users.update_user(user)
self._db.save_changes() self._db.save_changes()
await self._message_service.send_dm_message( await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.messages.second"), member self._t.transform("modules.base.warnings.messages.second"), member
) )
case 3: elif existing_warnings.count() == 3:
server = self._servers.get_server_by_discord_id(guild_id) 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) 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) 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() new_level = levels.where(lambda l: l.min_xp > 0).order_by(lambda l: l.min_xp).last()
user.xp = new_level.min_xp user.xp = new_level.min_xp
self._users.update_user(user) self._users.update_user(user)
self._db.save_changes() self._db.save_changes()
await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.messages.third"), member
)
elif existing_warnings.count() >= 4:
user.xp = 0
self._users.update_user(user)
self._db.save_changes()
await member.kick()
mods = [
*self._permissions.get_admins(member.guild.id),
*self._permissions.get_moderators(member.guild.id),
]
for a in mods:
await self._message_service.send_dm_message( await self._message_service.send_dm_message(
self._t.transform("modules.base.warnings.messages.third"), member self._t.transform("modules.base.warnings.messages.kick").format(member.mention),
a,
) )
case 4:
user.xp = 0
self._users.update_user(user)
self._db.save_changes()
await member.kick()
def remove_warnings(self, id: int): def remove_warnings(self, id: int):
warning = self._warnings.get_user_warnings_by_id(id) warning = self._warnings.get_user_warnings_by_id(id)