Merge pull request 'Added user get command #21' (#142) from #21 into 0.3

Reviewed-on: sh-edraft.de/kd_discord_bot#142
Reviewed-by: Sven Heidemann <sven.heidemann@sh-edraft.de>
Closes #21
This commit is contained in:
Sven Heidemann 2022-12-11 19:27:35 +01:00
commit 7026b3abac
2 changed files with 79 additions and 26 deletions

View File

@ -160,7 +160,8 @@
"removed": "Presence wurde entfernt.", "removed": "Presence wurde entfernt.",
"max_char_count_exceeded": "Der Text darf nicht mehr als 128 Zeichen lang sein!" "max_char_count_exceeded": "Der Text darf nicht mehr als 128 Zeichen lang sein!"
}, },
"user_info": { "user": {
"info": {
"fields": { "fields": {
"id": "Id", "id": "Id",
"name": "Name", "name": "Name",
@ -174,6 +175,12 @@
"warnings": "Verwarnungen" "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": { "boot_log": {

View File

@ -1,10 +1,11 @@
from typing import Optional from typing import Optional, List
import discord import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_discord.command import DiscordCommandABC from cpl_discord.command import DiscordCommandABC
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.ext import commands from discord.ext import commands
from discord.ext.commands import Context from discord.ext.commands import Context
@ -83,26 +84,28 @@ class UserGroup(DiscordCommandABC):
color=int('ef9d0d', 16) color=int('ef9d0d', 16)
) )
ujvs = self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id) ontime = self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id).sum(
ontime = ujvs.sum(lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) 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.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.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.discord_join'),
embed.add_field(name=self._t.transform('modules.base.user_info.fields.last_join'), value=self._date.transform(member.joined_at), inline=False) value=self._date.transform(member.created_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.last_join'),
embed.add_field(name=self._t.transform('modules.base.user_info.fields.ontime'), value=str(ontime)) 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 = '' roles = ''
for role in member.roles: for role in member.roles:
roles += f'{role.name}\n' 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: if is_mod or member == ctx.author:
joins_string = '' joins_string = ''
for join in joins: for join in joins:
joins_string += f'{self._date.transform(join.joined_on)}\n' 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: if is_mod or member == ctx.author:
lefts_string = '' lefts_string = ''
@ -113,11 +116,54 @@ class UserGroup(DiscordCommandABC):
continue continue
lefts_string += f'{self._date.transform(join.leaved_on)}\n' 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: 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 # send to interaction because of sensitive data
await self._message_service.send_interaction_msg(ctx.interaction, embed, wait_before_delete=wait) await self._message_service.send_interaction_msg(ctx.interaction, embed, wait_before_delete=wait)
self._logger.trace(__name__, f'Finished user-info command') 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(round(
self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.user_id)
.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))
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]