0.3 - level set (#26) #101

Merged
edraft merged 7 commits from #26 into 0.3 2022-11-09 19:51:23 +01:00
2 changed files with 56 additions and 0 deletions
Showing only changes of commit c666ceb51e - Show all commits

View File

@ -202,6 +202,11 @@
"already_last": "{} hat bereits das höchste Level.",
"success": "{} wurde auf Level {} hochgesetzt :)",
"failed": "{} konnte nicht hochgesetzt werden :("
},
"set": {
"already_level": "{} hat bereits das Level {} :/",
"success": "{} ist nun Level {} :)",
"failed": "Das Level von {} konnte nicht auf {} gesetzt werden :("
}
},
"database": {},

View File

@ -268,3 +268,54 @@ class LevelGroup(DiscordCommandABC):
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.up.failed').format(member.name))
self._logger.trace(__name__, f'Finished command level up')
@level.command()
@commands.guild_only()
async def set(self, ctx: Context, member: discord.Member, level: str):
self._logger.debug(__name__, f'Received command level up {ctx} {member}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
self._logger.trace(__name__, f'Finished command level up')
return
if ctx.guild is None:
return
if member.bot:
return
server = self._servers.get_server_by_discord_id(ctx.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id)
current_level = self._level_service.get_level(user)
new_level = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).single_or_default()
if new_level is None:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.failed').format(member.name, level))
self._logger.trace(__name__, f'Finished command level set')
return
if current_level.name == level:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.already_level').format(member.name, level))
self._logger.trace(__name__, f'Finished command level set')
return
try:
user.xp = new_level.min_xp
self._users.update_user(user)
self._db.save_changes()
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.success').format(member.name, new_level.name))
await self._level_service.set_level(user)
except Exception as e:
self._logger.error(__name__, f'Cannot set level {level} for {member.name}', e)
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.set.failed').format(member.name))
self._logger.trace(__name__, f'Finished command level set')
@set.autocomplete('level')
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id)
levels = self._levels.get_levels_by_server_id(server.server_id).select(lambda l: l.name)
return [app_commands.Choice(name=level, value=level) for level in levels]