Added user-info command & fixed info command

This commit is contained in:
Sven Heidemann 2022-01-15 18:59:15 +01:00
parent c74a74a3c5
commit 4bbb2b5c45
9 changed files with 215 additions and 40 deletions

View File

@ -42,6 +42,7 @@ from modules.base.service.help_command_service import HelpCommandService
from modules.base.service.info_command_service import InfoCommandService
from modules.base.service.ping_command_service import PingCommandService
from modules.base.service.purge_command_service import PurgeCommandService
from modules.base.service.user_info_command_service import UserInfoCommandService
from modules.boot_log.boot_log import BootLog
from modules.database.database import Database
from modules.permission.abc.permission_service_abc import PermissionServiceABC
@ -98,11 +99,12 @@ class Startup(StartupABC):
services.add_singleton(PermissionServiceABC, PermissionService)
# commands
services.add_singleton(CommandABC, PingCommandService)
services.add_singleton(CommandABC, PurgeCommandService)
services.add_singleton(CommandABC, AFKCommandService)
services.add_singleton(CommandABC, HelpCommandService)
services.add_singleton(CommandABC, InfoCommandService)
services.add_transient(CommandABC, PingCommandService)
services.add_transient(CommandABC, PurgeCommandService)
services.add_transient(CommandABC, AFKCommandService)
services.add_transient(CommandABC, HelpCommandService)
services.add_transient(CommandABC, InfoCommandService)
services.add_transient(CommandABC, UserInfoCommandService)
# modules
services.add_transient(ModuleABC, Database)

View File

@ -24,4 +24,4 @@ class MessageServiceABC(ABC):
async def send_dm_message(self, message: Union[str, discord.Embed], receiver: Union[discord.User, discord.Member]): pass
@abstractmethod
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False): pass
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False, wait_before_delete: int = None): pass

View File

@ -11,7 +11,7 @@ class EmbedDescription:
description: str = None,
url: str = None,
color: str = None,
fields: List[EmbedDescriptionField] = None,
fields: list[EmbedDescriptionField] = None,
footer: str = None
):
self._title = title
@ -25,10 +25,18 @@ class EmbedDescription:
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

View File

@ -76,7 +76,7 @@ class MessageService(MessageServiceABC):
self._db.save_changes()
self._logger.info(__name__, f'Sent message to user {receiver.id}')
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False):
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None, is_persistent: bool = False, wait_before_delete: int = None):
if ctx is None:
self._logger.warn(__name__, 'Message context is empty')
self._logger.debug(__name__, f'Message: {message}')
@ -95,6 +95,9 @@ class MessageService(MessageServiceABC):
self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}')
self._clients.append_sent_message_count(self._bot.user.id, ctx.guild.id, 1)
self._db.save_changes()
if wait_before_delete is not None:
await asyncio.sleep(wait_before_delete)
if is_persistent:
await self.delete_message(ctx.message)
return

View File

@ -26,6 +26,7 @@ from modules.base.service.help_command_service import HelpCommandService
from modules.base.service.info_command_service import InfoCommandService
from modules.base.service.ping_command_service import PingCommandService
from modules.base.service.purge_command_service import PurgeCommandService
from modules.base.service.user_info_command_service import UserInfoCommandService
from modules.permission.abc.permission_service_abc import PermissionServiceABC
from gismo_core.abc.events.on_member_join_abc import OnMemberJoinABC
from gismo_core.abc.events.on_member_remove_abc import OnMemberRemoveABC
@ -38,24 +39,25 @@ from gismo_core.abc.module_abc import ModuleABC
class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceStateUpdateABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
clients: ClientRepositoryABC,
servers: ServerRepositoryABC,
known_users: KnownUserRepositoryABC,
users: UserRepositoryABC,
user_joins: UserJoinedServerRepositoryABC,
user_joins_vc: UserJoinedVoiceChannelRepositoryABC,
bot: BotServiceABC,
db: DatabaseContextABC,
messenger: MessageServiceABC,
permission_service: PermissionServiceABC,
ping_command: PingCommandService,
purge_command: PurgeCommandService,
afk_command: AFKCommandService,
help_command: HelpCommandService,
info_command: InfoCommandService,
self,
config: ConfigurationABC,
logger: LoggerABC,
clients: ClientRepositoryABC,
servers: ServerRepositoryABC,
known_users: KnownUserRepositoryABC,
users: UserRepositoryABC,
user_joins: UserJoinedServerRepositoryABC,
user_joins_vc: UserJoinedVoiceChannelRepositoryABC,
bot: BotServiceABC,
db: DatabaseContextABC,
messenger: MessageServiceABC,
permission_service: PermissionServiceABC,
ping_command: PingCommandService,
purge_command: PurgeCommandService,
afk_command: AFKCommandService,
help_command: HelpCommandService,
info_command: InfoCommandService,
user_info_command: UserInfoCommandService
):
self._config = config
self._logger = logger
@ -86,6 +88,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
self._bot.add_cog(afk_command)
self._bot.add_cog(help_command)
self._bot.add_cog(info_command)
self._bot.add_cog(user_info_command)
self._logger.info(__name__, f'Module {type(self)} loaded')

View File

@ -25,6 +25,7 @@ class BaseSettings(ConfigurationModelABC):
self._afk_command_move_message: str = ''
self._help_command_reference_url: str = ''
self._info_command_message: EmbedDescriptionSettings = EmbedDescriptionSettings()
self._user_info_command_message: EmbedDescriptionSettings = EmbedDescriptionSettings()
@property
def welcome_message(self) -> str:
@ -82,6 +83,10 @@ class BaseSettings(ConfigurationModelABC):
def info_command_message(self) -> EmbedDescriptionSettings:
return self._info_command_message
@property
def user_info_command_message(self) -> EmbedDescriptionSettings:
return self._user_info_command_message
def from_dict(self, settings: dict):
try:
self._welcome_message = settings['WelcomeMessage']
@ -99,6 +104,7 @@ class BaseSettings(ConfigurationModelABC):
self._afk_command_move_message = settings['AFKCommandMoveMessage']
self._help_command_reference_url = settings['HelpCommandReferenceUrl']
self._info_command_message.from_dict(settings['InfoCommandMessage'])
self._user_info_command_message.from_dict(settings['UserInfoCommandMessage'])
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -37,4 +37,4 @@ class HelpCommandService(CommandABC):
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
is_persistent = persistent_flag == '--stay'
await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent)
self._logger.trace(__name__, f'Finished ping command')
self._logger.trace(__name__, f'Finished help command')

View File

@ -12,6 +12,7 @@ from gismo_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.abc.module_abc import ModuleABC
from gismo_core.model.embed_description import EmbedDescription
from gismo_core.model.embed_description_field import EmbedDescriptionField
from gismo_core.service.embed_service import EmbedService
from modules.base.base_settings import BaseSettings
@ -41,35 +42,54 @@ class InfoCommandService(CommandABC):
async def info(self, ctx: Context):
self._logger.debug(__name__, f'Received command info {ctx}')
self._client_utils.received_command(ctx.guild.id)
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
embed_description = settings.info_command_message.embed_description
client = self._client_utils.get_client(self._bot.user.id, ctx.guild.id)
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
for i in range(len(embed_description.fields)):
field = embed_description.fields[i]
if field.value == '$version':
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 = gismo.__version__
elif field.value == '$ontime':
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 field.value == '$sent_message_count':
elif settings_field.value == '$sent_message_count':
field.value = client.sent_message_count
elif field.value == '$received_message_count':
elif settings_field.value == '$received_message_count':
field.value = client.received_message_count
elif field.value == '$deleted_message_count':
elif settings_field.value == '$deleted_message_count':
field.value = client.deleted_message_count
elif field.value == '$received_command_count':
elif settings_field.value == '$received_command_count':
field.value = client.received_command_count
elif field.value == '$moved_users_count':
elif settings_field.value == '$moved_users_count':
field.value = client.moved_users_count
elif field.value == '$modules':
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 ping command')
self._logger.trace(__name__, f'Finished info command')

View File

@ -0,0 +1,133 @@
from datetime import datetime
from typing import Union, Optional
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_query.extension import List
from discord.ext import commands
from discord.ext.commands import Context
import gismo
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.abc.module_abc import ModuleABC
from gismo_core.model.embed_description import EmbedDescription
from gismo_core.model.embed_description_field import EmbedDescriptionField
from gismo_core.service.embed_service import EmbedService
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
from gismo_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
from gismo_data.abc.user_repository_abc import UserRepositoryABC
from modules.base.base_settings import BaseSettings
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class UserInfoCommandService(CommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
message_service: MessageServiceABC,
bot: BotServiceABC,
client_utils: ClientUtilsServiceABC,
permissions: PermissionServiceABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
user_joined_servers: UserJoinedServerRepositoryABC
):
CommandABC.__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):
self._logger.debug(__name__, f'Received command user-info {ctx}:{member},{wait}')
self._client_utils.received_command(ctx.guild.id)
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
if not self._permissions.is_member_moderator(ctx.author):
await self._message_service.send_ctx_msg(ctx, settings.no_permissions_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')