Fixed client utils & Added add, remove and reset commands #179

This commit is contained in:
Sven Heidemann 2023-01-13 12:39:45 +01:00
parent 5d3ceff3bf
commit 2401e58827
5 changed files with 68 additions and 17 deletions

View File

@ -187,7 +187,13 @@
"type_error": "Der angegebene Wert ist keine Zahl! :("
}
},
"add": {
"xp": "Die {} von {} wurden um {} erhöht"
},
"remove": {
"xp": "Die {} von {} wurden um {} verringert"
},
"reset": {
"xp": "Die {} von {} wurden entfernt",
"ontime": "Die {} von {} wurden entfernt"
},

View File

@ -1,3 +1,4 @@
import datetime
from abc import ABC, abstractmethod
from datetime import datetime
from typing import Callable

View File

@ -17,6 +17,7 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.abc.client_repository_abc import ClientRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC
from bot_data.abc.user_message_count_per_hour_repository_abc import UserMessageCountPerHourRepositoryABC
from bot_data.model.user import User
from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour
@ -33,8 +34,8 @@ class ClientUtilsService(ClientUtilsABC):
bot: DiscordBotServiceABC,
servers: ServerRepositoryABC,
clients: ClientRepositoryABC,
umcphs: UserMessageCountPerHourRepositoryABC,
user_joined_vc: UserJoinedVoiceChannelRepositoryABC,
umcphs: UserMessageCountPerHourRepositoryABC,
message_service: MessageServiceABC,
db: DatabaseContextABC,
t: TranslatePipe,
@ -90,8 +91,6 @@ class ClientUtilsService(ClientUtilsABC):
async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool:
result = await self.check_if_bot_is_ready_yet()
if not result:
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.errors.bot_not_ready_yet'),
without_tracking=True)
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('common.errors.bot_not_ready_yet'),

View File

@ -68,6 +68,35 @@ class UserGroup(DiscordCommandABC):
self._atr_list = [(key, self._atr_dict[key]) for key in self._atr_dict]
async def _handle_atr_calc(self, ctx: Context, atr: str, value: int, member: discord.Member, is_remove=False):
if member is None or not isinstance(member, discord.Member):
member = ctx.author
server = self._servers.find_server_by_discord_id(ctx.guild.id)
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)
if atr == 'xp':
if is_remove:
user.xp -= value
else:
user.xp += value
self._users.update_user(user)
self._db.save_changes()
await self._level.check_level(member)
else:
await self._message_service.send_interaction_msg(
ctx.interaction,
self._t.transform('modules.base.user.error.atr_not_found').format(atr)
)
return
type = 'add' if not is_remove else 'remove'
await self._message_service.send_interaction_msg(
ctx.interaction,
self._t.transform(f'modules.base.user.{type}.{atr.lower()}').format(atr, value, member.mention)
)
@commands.hybrid_group()
@commands.guild_only()
async def user(self, ctx: Context):
@ -180,7 +209,7 @@ class UserGroup(DiscordCommandABC):
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def set(self, ctx: Context, atr: str, value: str, member: discord.Member = None):
async def set(self, ctx: Context, atr: str, value: int, member: discord.Member = None):
self._logger.debug(__name__, f'Received command user-set {atr} {ctx}:{member}')
if member is None or not isinstance(member, discord.Member):
@ -190,14 +219,8 @@ class UserGroup(DiscordCommandABC):
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id)
if atr == 'xp':
if not value.isnumeric():
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform('modules.base.user.set.error.value_type_not_numeric')
)
return
try:
user.xp = int(value)
user.xp = value
except TypeError as te:
self._logger.error(__name__, f'String value couldn\'t be converted to int', te)
await self._message_service.send_interaction_msg(
@ -227,6 +250,32 @@ class UserGroup(DiscordCommandABC):
atr_list = [('xp', self._atr_dict['xp'])]
return [app_commands.Choice(name=value, value=key) for key, value in atr_list]
@user.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def add(self, ctx: Context, atr: str, value: int, member: discord.Member = None):
self._logger.debug(__name__, f'Received command user-add {atr}-={value} {ctx}:{member}')
await self._handle_atr_calc(ctx, atr, value, member)
@add.autocomplete('atr')
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
atr_list = [('xp', self._atr_dict['xp'])]
return [app_commands.Choice(name=value, value=key) for key, value in atr_list]
@user.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def remove(self, ctx: Context, atr: str, value: int, member: discord.Member = None):
self._logger.debug(__name__, f'Received command user-remove {atr}-={value} {ctx}:{member}')
await self._handle_atr_calc(ctx, atr, value, member, is_remove=True)
@remove.autocomplete('atr')
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
atr_list = [('xp', self._atr_dict['xp'])]
return [app_commands.Choice(name=value, value=key) for key, value in atr_list]
@user.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@ -246,10 +295,6 @@ class UserGroup(DiscordCommandABC):
self._db.save_changes()
await self._level.check_level(member)
elif atr == 'ontime':
self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id)
self._db.save_changes()
else:
await self._message_service.send_interaction_msg(
ctx.interaction,

View File

@ -7,7 +7,7 @@ from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_discord.events import OnVoiceStateUpdateABC
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.helper.event_checks import EventChecks
from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
@ -33,7 +33,7 @@ class BaseOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC):
users: UserRepositoryABC,
user_joins: UserJoinedServerRepositoryABC,
user_joins_vc: UserJoinedVoiceChannelRepositoryABC,
client_utils: ClientUtilsServiceABC,
client_utils: ClientUtilsABC,
db: DatabaseContextABC,
):
OnVoiceStateUpdateABC.__init__(self)