0.3 - /level edit (#103) #108

Merged
edraft merged 5 commits from #103 into 0.3 2022-11-11 22:02:27 +01:00
2 changed files with 105 additions and 26 deletions

View File

@ -192,7 +192,13 @@
"permission_int": "Berechtigungen"
},
"create": {
"created": "Level {} mit Berechtigungen {} wurde erstellt."
"created": "Level {} mit Berechtigungen {} wurde erstellt :D"
},
"edit": {
"edited": "Level {} wurde bearbeitet :D",
"color_invalid": "Die Farbe {} ist ungültig!",
"permission_invalid": "Der Berechtigungswert {} ist ungültig!",
"not_found": "Level {} nicht gefunden!"
},
"remove": {
"success": "Level {} wurde entfernt :D",

View File

@ -55,6 +55,31 @@ class LevelGroup(DiscordCommandABC):
self._level_service = level_service
self._level_seeder = level_seeder
self._colors = [
('blue', discord.Colour.blue().to_rgb()),
('dark_blue', discord.Colour.dark_blue().to_rgb()),
('dark_gold', discord.Colour.dark_gold().to_rgb()),
('dark_gray', discord.Colour.dark_gray().to_rgb()),
('dark_green', discord.Colour.dark_green().to_rgb()),
('dark_grey', discord.Colour.dark_grey().to_rgb()),
('dark_magenta', discord.Colour.dark_magenta().to_rgb()),
('dark_orange', discord.Colour.dark_orange().to_rgb()),
('dark_purple', discord.Colour.dark_purple().to_rgb()),
('dark_red', discord.Colour.dark_red().to_rgb()),
('dark_teal', discord.Colour.dark_teal().to_rgb()),
('default', discord.Colour.default().to_rgb()),
('gold', discord.Colour.gold().to_rgb()),
('green', discord.Colour.green().to_rgb()),
('greyple', discord.Colour.greyple().to_rgb()),
('light_grey', discord.Colour.light_grey().to_rgb()),
('magenta', discord.Colour.magenta().to_rgb()),
('orange', discord.Colour.orange().to_rgb()),
('purple', discord.Colour.purple().to_rgb()),
('red', discord.Colour.red().to_rgb()),
('teal', discord.Colour.teal().to_rgb()),
('yellow', discord.Colour.yellow().to_rgb())
]
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
async def _seed_levels(self, channel: discord.TextChannel):
@ -168,33 +193,81 @@ class LevelGroup(DiscordCommandABC):
@create.autocomplete('color')
async def create_color_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
colors = [
('blue', discord.Colour.blue().to_rgb()),
('dark_blue', discord.Colour.dark_blue().to_rgb()),
('dark_gold', discord.Colour.dark_gold().to_rgb()),
('dark_gray', discord.Colour.dark_gray().to_rgb()),
('dark_green', discord.Colour.dark_green().to_rgb()),
('dark_grey', discord.Colour.dark_grey().to_rgb()),
('dark_magenta', discord.Colour.dark_magenta().to_rgb()),
('dark_orange', discord.Colour.dark_orange().to_rgb()),
('dark_purple', discord.Colour.dark_purple().to_rgb()),
('dark_red', discord.Colour.dark_red().to_rgb()),
('dark_teal', discord.Colour.dark_teal().to_rgb()),
('default', discord.Colour.default().to_rgb()),
('gold', discord.Colour.gold().to_rgb()),
('green', discord.Colour.green().to_rgb()),
('greyple', discord.Colour.greyple().to_rgb()),
('light_grey', discord.Colour.light_grey().to_rgb()),
('magenta', discord.Colour.magenta().to_rgb()),
('orange', discord.Colour.orange().to_rgb()),
('purple', discord.Colour.purple().to_rgb()),
('red', discord.Colour.red().to_rgb()),
('teal', discord.Colour.teal().to_rgb()),
('yellow', discord.Colour.yellow().to_rgb())
]
# value in rg format see:
# https://discordpy.readthedocs.io/en/latest/api.html#discord.Colour.to_rgb
return [app_commands.Choice(name=self._t.transform(f'common.colors.{color}'), value=f'rgb({code[0]}, {code[1]}, {code[2]})') for color, code in colors]
return [app_commands.Choice(name=self._t.transform(f'common.colors.{color}'), value=f'rgb({code[0]}, {code[1]}, {code[2]})') for color, code in self._colors]
@level.command()
@commands.guild_only()
async def edit(self, ctx: Context, level: str, name: str = None, color: str = None, min_xp: int = None, permissions: int = None):
self._logger.debug(__name__, f'Received command level edit {ctx}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
self._client_utils.received_command(ctx.guild.id)
if not self._permissions.is_member_admin(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 remove')
return
if ctx.guild is None:
return
server = self._servers.get_server_by_discord_id(ctx.guild.id)
level_from_db = self._levels.get_levels_by_server_id(server.server_id).where(lambda l: l.name == level).single_or_default()
if level_from_db is None:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.not_found').format(level))
return
guild: Guild = self._bot.guilds.where(lambda g: g == ctx.guild).single()
role: Role = guild.roles.where(lambda r: r.name == level_from_db.name).single()
if name is not None:
level_from_db.name = name
if color is not None:
try:
level_from_db.color = hex(discord.Colour.from_str(color).value)
except Exception as e:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.color_invalid').format(color))
edraft marked this conversation as resolved Outdated

Wenn die Farbe nicht geparsed werden kann, wäre es gut, wenn der Anwender darüber benachrichtigt wird.

Wenn die Farbe nicht geparsed werden kann, wäre es gut, wenn der Anwender darüber benachrichtigt wird.
self._logger.error(__name__, f'Error parsing color {color}', e)
return
if min_xp is not None:
level_from_db.min_xp = min_xp
if permissions is not None:
try:
level_from_db.permissions = discord.Permissions(permissions).value
except Exception as e:
edraft marked this conversation as resolved
Review

Auch hier, wenn die Permission nicht geparsed werden kann, soll der Anwender darüber benachrichtigt werden.

Auch hier, wenn die Permission nicht geparsed werden kann, soll der Anwender darüber benachrichtigt werden.
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.permission_invalid').format(permissions))
self._logger.error(__name__, f'Error parsing permissions {permissions}', e)
return
try:
self._levels.update_level(level_from_db)
self._db.save_changes()
await role.edit(name=level_from_db.name, permissions=discord.Permissions(level_from_db.permissions), colour=discord.Colour(int(level_from_db.color, 16)))
self._logger.info(__name__, f'Saved level {level_from_db.name} with color {level_from_db.color}, min_xp {level_from_db.min_xp} and permissions {level_from_db.permissions}')
except Exception as e:
self._logger.error(__name__, f'Could not save level {level} with color {color}, min_xp {min_xp} and permissions {permissions}', e)
else:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.edited').format(level))
await self._seed_levels(ctx.channel)
self._logger.trace(__name__, f'Finished command level edit')
@edit.autocomplete('level')
async def edit_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]
@edit.autocomplete('color')
async def edit_color_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
# value in rg format see:
# https://discordpy.readthedocs.io/en/latest/api.html#discord.Colour.to_rgb
return [app_commands.Choice(name=self._t.transform(f'common.colors.{color}'), value=f'rgb({code[0]}, {code[1]}, {code[2]})') for color, code in self._colors]
@level.command()
@commands.guild_only()