Added user-info command & improved embed handling

This commit is contained in:
Sven Heidemann 2022-07-17 14:15:29 +02:00
parent 89c2d192c8
commit 5ae525121c
5 changed files with 138 additions and 169 deletions

View File

@ -10,6 +10,7 @@ from modules.base.command.help_command import HelpCommand
from modules.base.command.info_command import InfoCommand from modules.base.command.info_command import InfoCommand
from modules.base.command.ping_command import PingCommand from modules.base.command.ping_command import PingCommand
from modules.base.command.purge_command import PurgeCommand from modules.base.command.purge_command import PurgeCommand
from modules.base.command.user_info_command import UserInfoCommand
from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent
from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent from modules.base.events.base_on_member_remove_event import BaseOnMemberRemoveEvent
from modules.base.events.base_on_message_event import BaseOnMessageEvent from modules.base.events.base_on_message_event import BaseOnMessageEvent
@ -37,6 +38,7 @@ class StartupDiscordExtension(StartupExtensionABC):
dc.add_command(InfoCommand) dc.add_command(InfoCommand)
dc.add_command(PingCommand) dc.add_command(PingCommand)
dc.add_command(PurgeCommand) dc.add_command(PurgeCommand)
dc.add_command(UserInfoCommand)
""" events """ """ events """
# on_member_join # on_member_join
dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent) dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent)

View File

@ -13,7 +13,7 @@
"afk_command_channel_missing_message": "Zu unfähig einem Sprachkanal beizutreten?", "afk_command_channel_missing_message": "Zu unfähig einem Sprachkanal beizutreten?",
"afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)", "afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)",
"pong": "Pong", "pong": "Pong",
"user_info": { "info": {
"title": "Gismo", "title": "Gismo",
"description": "Informationen über mich", "description": "Informationen über mich",
"fields": { "fields": {
@ -27,6 +27,20 @@
"modules": "Module" "modules": "Module"
}, },
"footer": "" "footer": ""
},
"user_info": {
"fields": {
"id": "Id",
"name": "Name",
"discord_join": "Discord beigetreten am",
"last_join": "Server beigetreten am",
"xp": "XP",
"roles": "Rollen",
"joins": "Beitritte",
"lefts": "Abgänge",
"warnings": "Verwarnungen"
},
"footer": ""
} }
}, },
"boot_log": { "boot_log": {

View File

@ -1,5 +1,6 @@
from datetime import datetime from datetime import datetime
import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC from cpl_discord.command import DiscordCommandABC
@ -41,51 +42,28 @@ class InfoCommand(DiscordCommandABC):
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command() @commands.command()
async def info(self, ctx: Context): async def info(self, ctx: Context, *, wait: int = None):
self._logger.debug(__name__, f'Received command info {ctx}') self._logger.debug(__name__, f'Received command info {ctx},{wait}')
self._client_utils.received_command(ctx.guild.id) self._client_utils.received_command(ctx.guild.id)
client = self._client_utils.get_client(self._bot.user.id, ctx.guild.id) client = self._client_utils.get_client(self._bot.user.id, ctx.guild.id)
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
embed_description = EmbedDescription( embed = discord.Embed(
self._t.transform('modules.base.user_info.title'), title=self._t.transform('modules.base.info.title'),
self._t.transform('modules.base.user_info.description'), description=self._t.transform('modules.base.info.description'),
'', color=int('ef9d0d', 16)
'ef9d0d',
List(EmbedDescriptionField),
self._t.transform('modules.base.user_info.footer'),
) )
embed_description.fields.append( embed.add_field(name=self._t.transform('modules.base.info.fields.version'), value=bot.__version__)
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.version'), bot.__version__, True)
)
start_time = self._config.get_configuration('Bot_StartTime') start_time = self._config.get_configuration('Bot_StartTime')
ontime = round((datetime.now() - datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S.%f')).total_seconds() / 3600, 2) ontime = round((datetime.now() - datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S.%f')).total_seconds() / 3600, 2)
embed_description.fields.append( embed.add_field(name=self._t.transform('modules.base.info.fields.ontime'), value=f'{ontime}h')
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.ontime'), f'{ontime}h', True) embed.add_field(name=self._t.transform('modules.base.info.fields.sent_message_count'), value=client.sent_message_count, inline=False)
) embed.add_field(name=self._t.transform('modules.base.info.fields.received_message_count'), value=client.received_message_count)
embed_description.fields.append( embed.add_field(name=self._t.transform('modules.base.info.fields.deleted_message_count'), value=client.deleted_message_count, inline=False)
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.sent_message_count'), client.sent_message_count, True) embed.add_field(name=self._t.transform('modules.base.info.fields.received_command_count'), value=client.received_command_count)
) embed.add_field(name=self._t.transform('modules.base.info.fields.moved_users_count'), value=client.moved_users_count)
embed_description.fields.append(
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.received_message_count'), client.received_message_count, True)
)
embed_description.fields.append(
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.deleted_message_count'), client.deleted_message_count, True)
)
embed_description.fields.append(
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.received_command_count'), client.received_command_count, True)
)
embed_description.fields.append(
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.moved_users_count'), client.moved_users_count, False)
)
modules = ['Base', 'BootLog', 'Database', 'Permission'] modules = ['Base', 'BootLog', 'Database', 'Permission']
embed_description.fields.append( embed.add_field(name=self._t.transform('modules.base.info.fields.modules'), value='\n'.join(modules), inline=False)
EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.modules'), '\n'.join(modules), False)
)
await self._message_service.send_ctx_msg( await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
ctx,
EmbedService.get_embed(embed_description)
)
self._logger.trace(__name__, f'Finished info command') self._logger.trace(__name__, f'Finished info command')

View File

@ -0,0 +1,105 @@
from typing import Optional
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from cpl_translation import TranslatePipe
from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.model.embed_description import EmbedDescription
from bot_core.model.embed_description_field import EmbedDescriptionField
from bot_core.service.embed_service import EmbedService
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from modules.base.configuration.base_server_settings import BaseServerSettings
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class UserInfoCommand(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC,
permissions: PermissionServiceABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
user_joined_servers: UserJoinedServerRepositoryABC,
translate: TranslatePipe
):
DiscordCommandABC.__init__(self)
self._config = config
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._permissions = permissions
self._servers = servers
self._users = users
self._user_joined_servers = user_joined_servers
self._t = translate
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command(name='user-info')
async def user_info(self, ctx: Context, member: Optional[discord.Member] = None, *, wait: int = None):
self._logger.debug(__name__, f'Received command user-info {ctx}:{member},{wait}')
self._client_utils.received_command(ctx.guild.id)
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 purge command')
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)
joins = self._user_joined_servers.get_user_joined_servers_by_user_id(user.user_id)
embed = discord.Embed(
title=self._t.transform('modules.base.user_info.title'),
description=self._t.transform('modules.base.user_info.description'),
color=int('ef9d0d', 16)
)
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=member.created_at, inline=False)
embed.add_field(name=self._t.transform('modules.base.user_info.fields.last_join'), value=member.joined_at, inline=False)
embed.add_field(name=self._t.transform('modules.base.user_info.fields.xp'), value=str(user.xp))
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)
joins_string = ''
for join in joins:
joins_string += f'{join.joined_on}\n'
embed.add_field(name=self._t.transform('modules.base.user_info.fields.joins'), value=joins_string)
lefts_string = ''
for join in joins:
if join.leaved_on is None:
if lefts_string == '':
lefts_string = '/'
continue
lefts_string += f'{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.warnings'), value='Not Implemented yet', inline=False)
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
self._logger.trace(__name__, f'Finished user-info command')

View File

@ -1,130 +0,0 @@
from typing import Optional
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from modules.base.configuration.base_server_settings import BaseServerSettings
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class UserInfoCommandService(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC,
permissions: PermissionServiceABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
user_joined_servers: UserJoinedServerRepositoryABC
):
DiscordCommandABC.__init__(self)
self._config = config
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._permissions = permissions
self._servers = servers
self._users = users
self._user_joined_servers = user_joined_servers
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command(name='user-info')
async def user_info(self, ctx: Context, member: Optional[discord.Member] = None, *, wait: int = None):
#
# Todo: Use native embeds!!!
#
self._logger.debug(__name__, f'Received command user-info {ctx}:{member},{wait}')
self._client_utils.received_command(ctx.guild.id)
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, settings.no_permission_message)
self._logger.trace(__name__, f'Finished purge command')
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)
joins = self._user_joined_servers.get_user_joined_servers_by_user_id(user.user_id)
embed_description = EmbedDescription(
settings.user_info_command_message.embed_description.title.format(member.name),
settings.user_info_command_message.embed_description.description.format(member.name),
settings.user_info_command_message.embed_description.url,
settings.user_info_command_message.embed_description.color,
List(EmbedDescriptionField),
settings.user_info_command_message.embed_description.footer
)
for i in range(len(settings.user_info_command_message.embed_description.fields)):
settings_field = settings.user_info_command_message.embed_description.fields[i]
field = EmbedDescriptionField(settings_field.name, settings_field.value, settings_field.inline)
if settings_field.value == '$id':
field.value = settings_field.value.replace('$id', str(member.id))
elif settings_field.value == '$name':
field.value = settings_field.value.replace('$name', member.name)
elif settings_field.value == '$discord_join':
field.value = settings_field.value.replace('$discord_join', str(member.created_at))
elif settings_field.value == '$last_join':
field.value = settings_field.value.replace('$last_join', str(member.joined_at))
elif settings_field.value == '$xp':
field.value = settings_field.value.replace('$xp', str(user.xp))
elif settings_field.value == '$roles':
roles = ''
for role in member.roles:
roles += f'{role.name}\n'
field.value = settings_field.value.replace('$roles', roles)
elif settings_field.value == '$joins':
joins_string = ''
for join in joins:
joins_string += f'{join.joined_on}\n'
field.value = settings_field.value.replace('$joins', joins_string)
elif settings_field.value == '$leavings':
leavings_string = ''
for join in joins:
if join.leaved_on is None:
if leavings_string == '':
leavings_string = '/'
continue
leavings_string += f'{join.leaved_on}\n'
field.value = settings_field.value.replace('$leavings', leavings_string)
elif settings_field.value == '$warnings':
field.value = 'Not Implemented yet'
embed_description.fields.append(field)
await self._message_service.send_ctx_msg(
ctx,
EmbedService.get_embed(embed_description),
wait_before_delete=wait
)
self._logger.trace(__name__, f'Finished user-info command')