From fdd83577295c784576b9072609cb279a2448c92a Mon Sep 17 00:00:00 2001 From: Nick Jungmann Date: Sun, 11 Dec 2022 00:22:34 +0100 Subject: [PATCH 1/2] Added user get command #21 --- kdb-bot/src/bot/translation/de.json | 33 +++++---- .../src/modules/base/command/user_group.py | 71 +++++++++++++++---- 2 files changed, 78 insertions(+), 26 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 8005bb12..2c88a59e 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -160,20 +160,27 @@ "removed": "Presence wurde entfernt.", "max_char_count_exceeded": "Der Text darf nicht mehr als 128 Zeichen lang sein!" }, - "user_info": { - "fields": { - "id": "Id", - "name": "Name", - "discord_join": "Discord beigetreten am", - "last_join": "Server beigetreten am", - "xp": "XP", - "ontime": "Ontime", - "roles": "Rollen", - "joins": "Beitritte", - "lefts": "Abgänge", - "warnings": "Verwarnungen" + "user": { + "info": { + "fields": { + "id": "Id", + "name": "Name", + "discord_join": "Discord beigetreten am", + "last_join": "Server beigetreten am", + "xp": "XP", + "ontime": "Ontime", + "roles": "Rollen", + "joins": "Beitritte", + "lefts": "Abgänge", + "warnings": "Verwarnungen" + }, + "footer": "" }, - "footer": "" + "get": { + "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(", + "xp": "{} hat {} xp", + "ontime": "{} war insgesamt {} Stunden aktiv in einem Sprachkanal" + } } }, "boot_log": { diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 712730b8..b3866bf5 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -1,10 +1,11 @@ -from typing import Optional +from typing import Optional, List import discord from cpl_core.configuration import ConfigurationABC from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe +from discord import app_commands from discord.ext import commands from discord.ext.commands import Context @@ -83,26 +84,28 @@ class UserGroup(DiscordCommandABC): color=int('ef9d0d', 16) ) - ujvs = self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id) - ontime = ujvs.sum(lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) + ontime = self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id).sum( + lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) - embed.add_field(name=self._t.transform('modules.base.user_info.fields.id'), value=member.id) - embed.add_field(name=self._t.transform('modules.base.user_info.fields.name'), value=member.name) - embed.add_field(name=self._t.transform('modules.base.user_info.fields.discord_join'), value=self._date.transform(member.created_at), inline=False) - embed.add_field(name=self._t.transform('modules.base.user_info.fields.last_join'), value=self._date.transform(member.joined_at), inline=False) - embed.add_field(name=self._t.transform('modules.base.user_info.fields.xp'), value=str(user.xp)) - embed.add_field(name=self._t.transform('modules.base.user_info.fields.ontime'), value=str(ontime)) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.id'), value=member.id) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.name'), value=member.name) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.discord_join'), + value=self._date.transform(member.created_at), inline=False) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.last_join'), + value=self._date.transform(member.joined_at), inline=False) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.xp'), value=str(user.xp)) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.ontime'), value=str(ontime)) roles = '' for role in member.roles: roles += f'{role.name}\n' - embed.add_field(name=self._t.transform('modules.base.user_info.fields.roles'), value=roles, inline=False) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.roles'), value=roles, inline=False) if is_mod or member == ctx.author: joins_string = '' for join in joins: joins_string += f'{self._date.transform(join.joined_on)}\n' - embed.add_field(name=self._t.transform('modules.base.user_info.fields.joins'), value=joins_string) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.joins'), value=joins_string) if is_mod or member == ctx.author: lefts_string = '' @@ -113,11 +116,53 @@ class UserGroup(DiscordCommandABC): continue lefts_string += f'{self._date.transform(join.leaved_on)}\n' - embed.add_field(name=self._t.transform('modules.base.user_info.fields.lefts'), value=lefts_string) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.lefts'), value=lefts_string) if is_mod or member == ctx.author: - embed.add_field(name=self._t.transform('modules.base.user_info.fields.warnings'), value=self._t.transform('common.not_implemented_yet'), inline=False) + embed.add_field(name=self._t.transform('modules.base.user.info.fields.warnings'), + value=self._t.transform('common.not_implemented_yet'), inline=False) # send to interaction because of sensitive data await self._message_service.send_interaction_msg(ctx.interaction, embed, wait_before_delete=wait) self._logger.trace(__name__, f'Finished user-info command') + + @user.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + async def get(self, ctx: Context, atr: str, member: discord.Member = None): + self._logger.debug(__name__, f'Received command user-info {ctx}:{member}') + + is_mod = self._permissions.is_member_moderator(ctx.author) + if member is not None and not is_mod: + await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) + return + + 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) + + match atr: + case 'xp': + value = str(user.xp) + + case 'ontime': + value = str( + self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id) + .sum(lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) + ) + + case other: + await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.get.atr_not_found').format(atr)) + return + + await self._message_service.send_interaction_msg( + ctx.interaction, + self._t.transform(f'modules.base.user.get.{atr}').format(member.mention, value) + ) + + @get.autocomplete('atr') + async def get_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + atr_list = ['xp', 'ontime'] + return [app_commands.Choice(name=atr, value=atr) for atr in atr_list] -- 2.45.2 From 31464df3f606fe600df3180fd0e084a097cd9074 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Dec 2022 19:26:48 +0100 Subject: [PATCH 2/2] Fixed ontime rounding #21 --- kdb-bot/src/modules/base/command/user_group.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index b3866bf5..077a32d3 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -148,10 +148,11 @@ class UserGroup(DiscordCommandABC): value = str(user.xp) case 'ontime': - value = str( + value = str(round( self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id) - .sum(lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) - ) + .sum(lambda join: (join.leaved_on - join.joined_on).total_seconds() / 3600), + 2 + )) case other: await self._message_service.send_interaction_msg(ctx.interaction, self._t.transform('modules.base.user.get.atr_not_found').format(atr)) -- 2.45.2