Added remove command #91

This commit is contained in:
Sven Heidemann 2022-11-07 20:54:27 +01:00
parent 6a18b15447
commit b8fcaa48a5
2 changed files with 77 additions and 16 deletions

View File

@ -176,6 +176,9 @@
}, },
"level": { "level": {
"new_level_message": "<@{}> ist nun Level {}", "new_level_message": "<@{}> ist nun Level {}",
"seeding_started": "Levelsystem wird neu geladen.",
"seeding_failed": "Levelsystem konnte nicht neu geladen werden.",
"seeding_finished": "Levelsystem wurde Erfolgreich neu geladen.",
"error": { "error": {
"nothing_found": "Keine Level Einträge gefunden.", "nothing_found": "Keine Level Einträge gefunden.",
"level_with_name_already_exists": "Ein Level mit dem Namen {} existiert bereits!", "level_with_name_already_exists": "Ein Level mit dem Namen {} existiert bereits!",
@ -189,10 +192,13 @@
"permission_int": "Berechtigungen" "permission_int": "Berechtigungen"
}, },
"create": { "create": {
"created": "Level {} mit Berechtigungen {} wurde erstellt.", "created": "Level {} mit Berechtigungen {} wurde erstellt."
"seeding_started": "Levelsystem wird neu geladen.", },
"seeding_failed": "Levelsystem konnte nicht neu geladen werden.", "remove": {
"seeding_finished": "Levelsystem wurde Erfolgreich neu geladen." "success": "Level {} wurde entfernt :D",
"error": {
"not_found": "Level {} nicht gefunden!"
}
} }
}, },
"database": {}, "database": {},

View File

@ -3,6 +3,7 @@ from typing import List as TList
import discord import discord
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC from cpl_discord.command import DiscordCommandABC
from cpl_discord.container import Guild, Role
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe from cpl_translation import TranslatePipe
from discord import app_commands from discord import app_commands
@ -53,6 +54,16 @@ class LevelGroup(DiscordCommandABC):
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
async def _seed_levels(self, channel: discord.TextChannel):
# send message to ctx.channel because send_ctx_msg resolves ctx
try:
await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_started'))
await self._level_seeder.seed()
await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_finished'))
except Exception as e:
self._logger.error(__name__, f'Level seeding failed', e)
await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_failed'))
@commands.hybrid_group() @commands.hybrid_group()
@commands.guild_only() @commands.guild_only()
async def level(self, ctx: Context): async def level(self, ctx: Context):
@ -104,6 +115,14 @@ class LevelGroup(DiscordCommandABC):
async def create(self, ctx: Context, name: str, color: str, min_xp: int, permissions: int): async def create(self, ctx: Context, name: str, color: str, min_xp: int, permissions: int):
self._logger.debug(__name__, f'Received command level create {ctx}') self._logger.debug(__name__, f'Received command level create {ctx}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
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
try: try:
color = hex(discord.Colour.from_str(color).value) color = hex(discord.Colour.from_str(color).value)
except Exception as e: except Exception as e:
@ -116,9 +135,6 @@ class LevelGroup(DiscordCommandABC):
self._logger.error(__name__, f'Error parsing permissions {permissions}', e) self._logger.error(__name__, f'Error parsing permissions {permissions}', e)
return return
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
if ctx.guild is None: if ctx.guild is None:
return return
@ -142,15 +158,7 @@ class LevelGroup(DiscordCommandABC):
self._logger.error(__name__, f'Could not save level {name} with color {color}, min_xp {min_xp} and permissions {permissions}', e) self._logger.error(__name__, f'Could not save level {name} with color {color}, min_xp {min_xp} and permissions {permissions}', e)
else: else:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.create.created').format(name, permissions)) await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.create.created').format(name, permissions))
await self._seed_levels(ctx.channel)
# send message to ctx.channel because send_ctx_msg resolves ctx
try:
await self._message_service.send_channel_message(ctx.channel, self._t.transform('modules.level.create.seeding_started'))
await self._level_seeder.seed()
await self._message_service.send_channel_message(ctx.channel, self._t.transform('modules.level.create.seeding_finished'))
except Exception as e:
self._logger.error(__name__, f'Level seeding failed', e)
await self._message_service.send_channel_message(ctx.channel, self._t.transform('modules.level.create.seeding_failed'))
self._logger.trace(__name__, f'Finished command level create') self._logger.trace(__name__, f'Finished command level create')
@ -184,3 +192,50 @@ class LevelGroup(DiscordCommandABC):
# value in rg format see: # value in rg format see:
# https://discordpy.readthedocs.io/en/latest/api.html#discord.Colour.to_rgb # 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 colors]
@level.command()
@commands.guild_only()
async def remove(self, ctx: Context, level: str):
self._logger.debug(__name__, f'Received command level remove {ctx}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return
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:
self._logger.trace(__name__, f'Finished command level remove')
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).first_or_default()
if level_from_db is None:
self._logger.debug(__name__, f'level {level} not found')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.remove.error.not_found').format(level))
self._logger.trace(__name__, f'Finished command level remove')
return
try:
self._levels.delete_level(level_from_db)
self._db_context.save_changes()
guild: Guild = self._bot.guilds.where(lambda g: g == ctx.guild).single()
role: Role = guild.roles.where(lambda r: r.name == level).single_or_default()
if role is not None:
await role.delete()
self._logger.info(__name__, f'Removed level {level}')
except Exception as e:
self._logger.error(__name__, f'Could not remove level {level}', e)
else:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.remove.success').format(level))
await self._seed_levels(ctx.channel)
self._logger.trace(__name__, f'Finished command level remove')
@remove.autocomplete('level')
async def remove_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]