diff --git a/src/bot/startup_discord_extension.py b/src/bot/startup_discord_extension.py index 26594dcf..6095ca79 100644 --- a/src/bot/startup_discord_extension.py +++ b/src/bot/startup_discord_extension.py @@ -7,6 +7,7 @@ from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum from modules.base.command.afk_command import AFKCommand from modules.base.command.help_command import HelpCommand +from modules.base.command.info_command import InfoCommand from modules.base.command.ping_command import PingCommand from modules.base.command.purge_command import PurgeCommand from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent @@ -33,6 +34,7 @@ class StartupDiscordExtension(StartupExtensionABC): """ commands """ dc.add_command(AFKCommand) dc.add_command(HelpCommand) + dc.add_command(InfoCommand) dc.add_command(PingCommand) dc.add_command(PurgeCommand) """ events """ diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index 184a21d7..1040b9d7 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -12,7 +12,22 @@ "purge_message": "Na gut..., ich lösche alle Nachrichten wenns sein muss.", "afk_command_channel_missing_message": "Zu unfähig einem Sprachkanal beizutreten?", "afk_command_move_message": "Ich verschiebe dich ja schon... (◔_◔)", - "pong": "Pong" + "pong": "Pong", + "user_info": { + "title": "Gismo", + "description": "Informationen über mich", + "fields": { + "version": "Version", + "ontime": "Ontime", + "sent_message_count": "Gesendete Nachrichten", + "received_message_count": "Empfangene Nachrichten", + "deleted_message_count": "Gelöschte Nachrichten", + "received_command_count": "Empfangene Befehle", + "moved_users_count": "Verschobene Benutzer", + "modules": "Module" + }, + "footer": "" + } }, "boot_log": { "login_message": "Ich bin on the line :D\nDer Scheiß hat {} Sekunden gedauert" diff --git a/src/bot_core/model/__init__.py b/src/bot_core/model/__init__.py new file mode 100644 index 00000000..5aa5f0ed --- /dev/null +++ b/src/bot_core/model/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +gismo sh-edraft Gismo +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Dicord bot Gismo + +:copyright: (c) 2021 - 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'gismo_core.model' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de' +__version__ = '0.4.2' + +from collections import namedtuple + +# imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='4', micro='2') diff --git a/src/bot_core/model/embed_description.py b/src/bot_core/model/embed_description.py new file mode 100644 index 00000000..1b100b1d --- /dev/null +++ b/src/bot_core/model/embed_description.py @@ -0,0 +1,58 @@ +from bot_core.model.embed_description_field import EmbedDescriptionField + + +class EmbedDescription: + + def __init__( + self, + title: str = None, + description: str = None, + url: str = None, + color: str = None, + fields: list[EmbedDescriptionField] = None, + footer: str = None + ): + self._title = title + self._description = description + self._url = url + self._color = color + self._fields = fields + self._footer = footer + + @property + def title(self) -> str: + return self._title + + @title.setter + def title(self, value: str): + self._title = value + + @property + def description(self) -> str: + return self._description + + @description.setter + def description(self, value: str): + self._description = value + + @property + def url(self) -> str: + return self._url + + @property + def color(self) -> str: + return self._color + + @property + def fields(self) -> list[EmbedDescriptionField]: + return self._fields + + @fields.setter + def fields(self, value: list[EmbedDescriptionField]): + self._fields = value + + @property + def footer(self) -> str: + return self._footer + + diff --git a/src/bot_core/model/embed_description_field.py b/src/bot_core/model/embed_description_field.py new file mode 100644 index 00000000..cb47b02b --- /dev/null +++ b/src/bot_core/model/embed_description_field.py @@ -0,0 +1,27 @@ +class EmbedDescriptionField: + + def __init__( + self, + name: str, + value: str, + inline: bool + ): + self._name = name + self._value = value + self._inline = inline + + @property + def name(self) -> str: + return self._name + + @property + def value(self) -> str: + return self._value + + @value.setter + def value(self, value: str): + self._value = value + + @property + def inline(self) -> bool: + return self._inline diff --git a/src/bot_core/service/embed_service.py b/src/bot_core/service/embed_service.py new file mode 100644 index 00000000..a5550084 --- /dev/null +++ b/src/bot_core/service/embed_service.py @@ -0,0 +1,23 @@ +import discord + +from bot_core.model.embed_description import EmbedDescription + + +class EmbedService: + + @staticmethod + def get_embed(description: EmbedDescription) -> discord.Embed: + embed = discord.Embed( + title=description.title, + url=description.url, + description=description.description, + color=int(description.color, 16) + ) + + for field in description.fields: + embed.add_field(name=field.name, value=field.value, inline=field.inline) + + if description.footer is not None: + embed.set_footer(text=description.footer) + + return embed diff --git a/src/modules/base/command/info_command.py b/src/modules/base/command/info_command.py new file mode 100644 index 00000000..abc0c2c7 --- /dev/null +++ b/src/modules/base/command/info_command.py @@ -0,0 +1,91 @@ +from datetime import datetime + +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 + +import bot +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 modules.base.configuration.base_server_settings import BaseServerSettings + + +class InfoCommand(DiscordCommandABC): + + def __init__( + self, + config: ConfigurationABC, + logger: LoggerABC, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsServiceABC, + 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._t = translate + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.command() + async def info(self, ctx: Context): + self._logger.debug(__name__, f'Received command info {ctx}') + self._client_utils.received_command(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( + self._t.transform('modules.base.user_info.title'), + self._t.transform('modules.base.user_info.description'), + '', + 'ef9d0d', + List(EmbedDescriptionField), + self._t.transform('modules.base.user_info.footer'), + ) + + embed_description.fields.append( + EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.version'), bot.__version__, True) + ) + 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) + embed_description.fields.append( + EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.ontime'), f'{ontime}h', True) + ) + embed_description.fields.append( + EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.sent_message_count'), client.sent_message_count, True) + ) + 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'] + embed_description.fields.append( + EmbedDescriptionField(self._t.transform('modules.base.user_info.fields.modules'), '\n'.join(modules), False) + ) + + await self._message_service.send_ctx_msg( + ctx, + EmbedService.get_embed(embed_description) + ) + self._logger.trace(__name__, f'Finished info command') diff --git a/src/modules/base/command/info_command_service.py b/src/modules/base/command/info_command_service.py deleted file mode 100644 index d97c17e8..00000000 --- a/src/modules/base/command/info_command_service.py +++ /dev/null @@ -1,94 +0,0 @@ -from datetime import datetime - -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 - -import bot -from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC -from bot_core.abc.message_service_abc import MessageServiceABC -from modules.base.configuration.base_server_settings import BaseServerSettings - - -class InfoCommandService(DiscordCommandABC): - - def __init__( - self, - config: ConfigurationABC, - logger: LoggerABC, - message_service: MessageServiceABC, - bot: DiscordBotServiceABC, - client_utils: ClientUtilsServiceABC - ): - DiscordCommandABC.__init__(self) - - self._config = config - self._logger = logger - self._message_service = message_service - self._bot = bot - self._client_utils = client_utils - - self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') - - @commands.command() - async def info(self, ctx: Context): - # - # Todo: Use native embeds!!! - # - self._logger.debug(__name__, f'Received command info {ctx}') - self._client_utils.received_command(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( - settings.info_command_message.embed_description.title, - settings.info_command_message.embed_description.description, - settings.info_command_message.embed_description.url, - settings.info_command_message.embed_description.color, - List(EmbedDescriptionField), - settings.info_command_message.embed_description.footer - ) - - for i in range(len(settings.info_command_message.embed_description.fields)): - settings_field = settings.info_command_message.embed_description.fields[i] - field = EmbedDescriptionField(settings_field.name, settings_field.value, settings_field.inline) - - if settings_field.value == '$version': - field.value = bot.__version__ - - elif settings_field.value == '$ontime': - 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) - field.value = f'{ontime}h' - - elif settings_field.value == '$sent_message_count': - field.value = client.sent_message_count - - elif settings_field.value == '$received_message_count': - field.value = client.received_message_count - - elif settings_field.value == '$deleted_message_count': - field.value = client.deleted_message_count - - elif settings_field.value == '$received_command_count': - field.value = client.received_command_count - - elif settings_field.value == '$moved_users_count': - field.value = client.moved_users_count - - elif settings_field.value == '$modules': - field.value = '' - for module in ModuleABC.__subclasses__(): - field.value += f'{module.__name__}\n' - - embed_description.fields.append(field) - - await self._message_service.send_ctx_msg( - ctx, - EmbedService.get_embed(embed_description) - ) - self._logger.trace(__name__, f'Finished info command')