Merge pull request '0.3 - level set (#26)' (#101) from #26 into 0.3

Reviewed-on: sh-edraft.de/kd_discord_bot#101
Reviewed-by: Ebola-Chan <nick.jungmann@gmail.com>
Closes #26
This commit is contained in:
Sven Heidemann 2022-11-09 19:51:22 +01:00
commit 27ccdbd980
3 changed files with 81 additions and 17 deletions

View File

@ -62,7 +62,7 @@
"gold": "Gold", "gold": "Gold",
"green": "Grün", "green": "Grün",
"greyple": "Graugrün", "greyple": "Graugrün",
"light_gray": "Hellgrau", "light_grey": "Hellgrau",
"magenta": "Magenta", "magenta": "Magenta",
"orange": "Orange", "orange": "Orange",
"purple": "Violett", "purple": "Violett",
@ -175,6 +175,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!",
@ -188,10 +191,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!"
}
}, },
"down": { "down": {
"already_first": "{} hat bereits das erste Level.", "already_first": "{} hat bereits das erste Level.",
@ -202,6 +208,12 @@
"already_last": "{} hat bereits das höchste Level.", "already_last": "{} hat bereits das höchste Level.",
"success": "{} wurde auf Level {} hochgesetzt :)", "success": "{} wurde auf Level {} hochgesetzt :)",
"failed": "{} konnte nicht hochgesetzt werden :(" "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 :(",
"not_found": "Das Level {} konnte nicht gefunden werden :("
} }
}, },
"database": {}, "database": {},

View File

@ -57,6 +57,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):
@ -146,15 +156,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')
@ -316,3 +318,54 @@ class LevelGroup(DiscordCommandABC):
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.up.failed').format(member.name)) 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') 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.not_found').format(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]

View File

@ -1,7 +1,6 @@
import discord import discord
from cpl_discord.container import Guild, Role from cpl_discord.container import Guild
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from discord import Permissions, Colour from discord import Permissions, Colour
from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.database_logger import DatabaseLogger
@ -34,7 +33,7 @@ class LevelSeeder(DataSeederABC):
self._logger.debug(__name__, f'Created role {level.name}') self._logger.debug(__name__, f'Created role {level.name}')
levels = self._levels.find_levels_by_server_id(server.server_id) levels = self._levels.find_levels_by_server_id(server.server_id)
if levels is None or levels.where(lambda l: l == level).first_or_default() is None: if levels is None or levels.where(lambda l: l.name == level.name).first_or_default() is None:
self._levels.add_level(level) self._levels.add_level(level)
self._logger.debug(__name__, f'Saved level {level.name}') self._logger.debug(__name__, f'Saved level {level.name}')
except discord.errors.Forbidden as e: except discord.errors.Forbidden as e: