Added user set command #22 #143
@ -2,6 +2,7 @@ from typing import Optional, List
|
||||
|
||||
import discord
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_discord.command import DiscordCommandABC
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
from cpl_translation import TranslatePipe
|
||||
@ -32,6 +33,7 @@ class UserGroup(DiscordCommandABC):
|
||||
client_utils: ClientUtilsServiceABC,
|
||||
permissions: PermissionServiceABC,
|
||||
servers: ServerRepositoryABC,
|
||||
db: DatabaseContextABC,
|
||||
users: UserRepositoryABC,
|
||||
user_joined_servers: UserJoinedServerRepositoryABC,
|
||||
user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC,
|
||||
@ -47,6 +49,7 @@ class UserGroup(DiscordCommandABC):
|
||||
self._client_utils = client_utils
|
||||
self._permissions = permissions
|
||||
self._servers = servers
|
||||
self._db = db
|
||||
self._users = users
|
||||
self._user_joined_servers = user_joined_servers
|
||||
self._user_joined_voice_channel = user_joined_voice_channel
|
||||
@ -130,7 +133,7 @@ class UserGroup(DiscordCommandABC):
|
||||
@commands.guild_only()
|
||||
@CommandChecks.check_is_ready()
|
||||
async def get(self, ctx: Context, atr: str, member: discord.Member = None):
|
||||
self._logger.debug(__name__, f'Received command user-info {ctx}:{member}')
|
||||
self._logger.debug(__name__, f'Received command user-get {ctx}:{member}')
|
||||
|
||||
is_mod = self._permissions.is_member_moderator(ctx.author)
|
||||
if member is not None and not is_mod:
|
||||
@ -167,3 +170,49 @@ class UserGroup(DiscordCommandABC):
|
||||
async def get_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
|
||||
atr_list = ['xp', 'ontime']
|
||||
return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]
|
||||
|
||||
@user.command()
|
||||
@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):
|
||||
self._logger.debug(__name__, f'Received command user-set {ctx}:{member}')
|
||||
|
||||
if value == '':
|
||||
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.no_value'))
|
||||
return
|
||||
|
||||
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)
|
||||
|
||||
match atr:
|
||||
case 'xp':
|
||||
try:
|
||||
user.xp = int(value)
|
||||
self._users.update_user(user)
|
||||
self._db.save_changes()
|
||||
except Exception as e:
|
||||
await self._logger.trace(__name__, f'Value couldn\'t be converted to int\n'+e)
|
||||
# ToDo: Add text for this exception
|
||||
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.value_type'))
|
||||
return
|
||||
|
||||
Ebola-Chan marked this conversation as resolved
Outdated
|
||||
case other:
|
||||
# ToDo: Move atr_not_found
|
||||
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform(
|
||||
'modules.base.user.error.atr_not_found').format(atr))
|
||||
return
|
||||
|
||||
# ToDo: Add text for set
|
||||
await self._message_service.send_interaction_msg(
|
||||
ctx.interaction,
|
||||
self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value)
|
||||
)
|
||||
|
||||
Ebola-Chan marked this conversation as resolved
Outdated
edraft
commented
Der ist bei weitem unnötig xD Des Weiteren ist DatabaseError um das update_user abzufangen ungenau bzw als solches auch nicht ötig. Der ist bei weitem unnötig xD
Wenn wir mal in check_level reinschauen sehen wir, dass dort alle Fehler abefangen werden.
Des Weiteren ist DatabaseError um das update_user abzufangen ungenau bzw als solches auch nicht ötig.
Ebola-Chan
commented
Errorhandling entfernt. Nur TypeError wird behandelt. Errorhandling entfernt. Nur TypeError wird behandelt.
|
||||
@set.autocomplete('atr')
|
||||
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
|
||||
atr_list = ['xp']
|
||||
return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]
|
||||
Ebola-Chan marked this conversation as resolved
edraft
commented
Es wäre ne idee, dass diese strings irgendwie aus translation oder config kommen. Also das man da halt ne liste von diesen Strings hat. Ist auch diesen drei Befehlen immer der Fall Es wäre ne idee, dass diese strings irgendwie aus translation oder config kommen. Also das man da halt ne liste von diesen Strings hat. Ist auch diesen drei Befehlen immer der Fall
|
||||
|
Loading…
Reference in New Issue
Block a user
Relativ ungenau. Entweder änderst du die beschreibung von dem error oder du machst zwei try except blöcke, einmal für den type cast und dann für die manipulation des users inkl. level check
Das Exceptionhandling für das "check_level" würde ich lieber dann in der Methode selber machen und das in einem separaten Issue, da ich dafür weitere Exceptionklassen erstellen möchte, um Exceptions in der Methode besser von den allgemeinen Databaseexceptions zu differenzieren.