Merge pull request 'Added user remove command #23' (#144) from #23 into 0.3

Reviewed-on: sh-edraft.de/kd_discord_bot#144
Reviewed-by: Sven Heidemann <sven.heidemann@sh-edraft.de>
Closes #23
This commit is contained in:
Sven Heidemann 2022-12-23 15:50:07 +01:00
commit 6273ce9cba
5 changed files with 60 additions and 13 deletions

View File

@ -184,11 +184,13 @@
"xp": "{} hat nun {} xp",
"error": {
"value_type_not_numeric": "Der angegebende Wert ist keine Ganzzahl! :(",
"type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :(",
"database_error": "Beim Speichern des Wertes kam es zu einem Fehler :(",
"general_error": "Bei der Ausführung dieses Befehls kam es zu einen Fehler :("
"type_error": "Der angegebene Wert konnte nicht als eine Ganzzahl interpretiert werden :("
}
},
"remove": {
"xp": "Die {} von {} wurden entfernt",
"ontime": "Die {} von {} wurden entfernt"
},
"error": {
"atr_not_found": "Das Attribut {} konnte nicht gefunden werden :("
}

View File

@ -35,3 +35,6 @@ class UserJoinedVoiceChannelRepositoryABC(ABC):
@abstractmethod
def delete_user_joined_voice_channel(self, user_joined_voice_channel: UserJoinedVoiceChannel): pass
@abstractmethod
def delete_user_joined_voice_channel_by_user_id(self, user_id: int): pass

View File

@ -119,3 +119,10 @@ class UserJoinedVoiceChannel(TableABC):
DELETE FROM `UserJoinedVoiceChannel`
WHERE `JoinId` = {self._join_id};
""")
@staticmethod
def delete_by_user_id_string(id: int) -> str:
return str(f"""
DELETE FROM `UserJoinedVoiceChannel`
WHERE `UserId` = {id}
""")

View File

@ -121,3 +121,7 @@ class UserJoinedVoiceChannelRepositoryService(UserJoinedVoiceChannelRepositoryAB
def delete_user_joined_voice_channel(self, user_joined_voice_channel: UserJoinedVoiceChannel):
self._logger.trace(__name__, f'Send SQL command: {user_joined_voice_channel.delete_string}')
self._context.cursor.execute(user_joined_voice_channel.delete_string)
def delete_user_joined_voice_channel_by_user_id(self, user_id: int):
self._logger.trace(__name__, f'Send SQL command: {UserJoinedVoiceChannel.delete_by_user_id_string}')
self._context.cursor.execute(UserJoinedVoiceChannel.delete_by_user_id_string(user_id))

View File

@ -194,16 +194,14 @@ class UserGroup(DiscordCommandABC):
if atr == self._atr_dict["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'))
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)
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(ctx.interaction, self._t.transform(
'modules.base.user.set.error.type_error'))
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.set.error.type_error'))
return
else:
self._users.update_user(user)
@ -211,16 +209,49 @@ class UserGroup(DiscordCommandABC):
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))
await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.error.atr_not_found').format(atr))
return
await self._message_service.send_interaction_msg(
ctx.interaction,
self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value)
)
await self._message_service.send_interaction_msg(ctx.interaction,self._t.transform(f'modules.base.user.set.{atr}').format(member.mention, value))
@set.autocomplete('atr')
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
atr_list = [self._atr_dict["xp"]]
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 remove(self, ctx: Context, atr: str, member: discord.Member = None):
self._logger.debug(__name__, f'Received command user-remove {ctx}:{member}')
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 == self._atr_dict["xp"]:
user.xp = 0
self._users.update_user(user)
self._db.save_changes()
await self._level.check_level(member)
elif atr == self._atr_dict["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, self._t.transform('modules.base.user.error.atr_not_found').format(atr))
return
await self._message_service.send_interaction_msg(
ctx.interaction,
self._t.transform(f'modules.base.user.remove.{atr}').format(atr, member.mention)
)
@remove.autocomplete('atr')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
atr_list = [self._atr_dict["xp"], self._atr_dict["ontime"]]
return [app_commands.Choice(name=atr, value=atr) for atr in atr_list]