0.4.2 - user-info Befehl #45
@ -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.info_command_service import InfoCommandService
|
||||||
from modules.base.service.ping_command_service import PingCommandService
|
from modules.base.service.ping_command_service import PingCommandService
|
||||||
from modules.base.service.purge_command_service import PurgeCommandService
|
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.boot_log.boot_log import BootLog
|
||||||
from modules.database.database import Database
|
from modules.database.database import Database
|
||||||
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
||||||
@ -98,11 +99,12 @@ class Startup(StartupABC):
|
|||||||
services.add_singleton(PermissionServiceABC, PermissionService)
|
services.add_singleton(PermissionServiceABC, PermissionService)
|
||||||
|
|
||||||
# commands
|
# commands
|
||||||
services.add_singleton(CommandABC, PingCommandService)
|
services.add_transient(CommandABC, PingCommandService)
|
||||||
services.add_singleton(CommandABC, PurgeCommandService)
|
services.add_transient(CommandABC, PurgeCommandService)
|
||||||
services.add_singleton(CommandABC, AFKCommandService)
|
services.add_transient(CommandABC, AFKCommandService)
|
||||||
services.add_singleton(CommandABC, HelpCommandService)
|
services.add_transient(CommandABC, HelpCommandService)
|
||||||
services.add_singleton(CommandABC, InfoCommandService)
|
services.add_transient(CommandABC, InfoCommandService)
|
||||||
|
services.add_transient(CommandABC, UserInfoCommandService)
|
||||||
|
|
||||||
# modules
|
# modules
|
||||||
services.add_transient(ModuleABC, Database)
|
services.add_transient(ModuleABC, Database)
|
||||||
|
@ -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
|
async def send_dm_message(self, message: Union[str, discord.Embed], receiver: Union[discord.User, discord.Member]): pass
|
||||||
|
|
||||||
@abstractmethod
|
@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
|
||||||
|
@ -11,7 +11,7 @@ class EmbedDescription:
|
|||||||
description: str = None,
|
description: str = None,
|
||||||
url: str = None,
|
url: str = None,
|
||||||
color: str = None,
|
color: str = None,
|
||||||
fields: List[EmbedDescriptionField] = None,
|
fields: list[EmbedDescriptionField] = None,
|
||||||
footer: str = None
|
footer: str = None
|
||||||
):
|
):
|
||||||
self._title = title
|
self._title = title
|
||||||
@ -25,10 +25,18 @@ class EmbedDescription:
|
|||||||
def title(self) -> str:
|
def title(self) -> str:
|
||||||
return self._title
|
return self._title
|
||||||
|
|
||||||
|
@title.setter
|
||||||
|
def title(self, value: str):
|
||||||
|
self._title = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def description(self) -> str:
|
def description(self) -> str:
|
||||||
return self._description
|
return self._description
|
||||||
|
|
||||||
|
@description.setter
|
||||||
|
def description(self, value: str):
|
||||||
|
self._description = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def url(self) -> str:
|
def url(self) -> str:
|
||||||
return self._url
|
return self._url
|
||||||
|
@ -76,7 +76,7 @@ class MessageService(MessageServiceABC):
|
|||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
self._logger.info(__name__, f'Sent message to user {receiver.id}')
|
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:
|
if ctx is None:
|
||||||
self._logger.warn(__name__, 'Message context is empty')
|
self._logger.warn(__name__, 'Message context is empty')
|
||||||
self._logger.debug(__name__, f'Message: {message}')
|
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._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._clients.append_sent_message_count(self._bot.user.id, ctx.guild.id, 1)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
|
if wait_before_delete is not None:
|
||||||
|
await asyncio.sleep(wait_before_delete)
|
||||||
|
|
||||||
if is_persistent:
|
if is_persistent:
|
||||||
await self.delete_message(ctx.message)
|
await self.delete_message(ctx.message)
|
||||||
return
|
return
|
||||||
|
@ -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.info_command_service import InfoCommandService
|
||||||
from modules.base.service.ping_command_service import PingCommandService
|
from modules.base.service.ping_command_service import PingCommandService
|
||||||
from modules.base.service.purge_command_service import PurgeCommandService
|
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 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_join_abc import OnMemberJoinABC
|
||||||
from gismo_core.abc.events.on_member_remove_abc import OnMemberRemoveABC
|
from gismo_core.abc.events.on_member_remove_abc import OnMemberRemoveABC
|
||||||
@ -56,6 +57,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
afk_command: AFKCommandService,
|
afk_command: AFKCommandService,
|
||||||
help_command: HelpCommandService,
|
help_command: HelpCommandService,
|
||||||
info_command: InfoCommandService,
|
info_command: InfoCommandService,
|
||||||
|
user_info_command: UserInfoCommandService
|
||||||
):
|
):
|
||||||
self._config = config
|
self._config = config
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
@ -86,6 +88,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._bot.add_cog(afk_command)
|
self._bot.add_cog(afk_command)
|
||||||
self._bot.add_cog(help_command)
|
self._bot.add_cog(help_command)
|
||||||
self._bot.add_cog(info_command)
|
self._bot.add_cog(info_command)
|
||||||
|
self._bot.add_cog(user_info_command)
|
||||||
|
|
||||||
self._logger.info(__name__, f'Module {type(self)} loaded')
|
self._logger.info(__name__, f'Module {type(self)} loaded')
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ class BaseSettings(ConfigurationModelABC):
|
|||||||
self._afk_command_move_message: str = ''
|
self._afk_command_move_message: str = ''
|
||||||
self._help_command_reference_url: str = ''
|
self._help_command_reference_url: str = ''
|
||||||
self._info_command_message: EmbedDescriptionSettings = EmbedDescriptionSettings()
|
self._info_command_message: EmbedDescriptionSettings = EmbedDescriptionSettings()
|
||||||
|
self._user_info_command_message: EmbedDescriptionSettings = EmbedDescriptionSettings()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def welcome_message(self) -> str:
|
def welcome_message(self) -> str:
|
||||||
@ -82,6 +83,10 @@ class BaseSettings(ConfigurationModelABC):
|
|||||||
def info_command_message(self) -> EmbedDescriptionSettings:
|
def info_command_message(self) -> EmbedDescriptionSettings:
|
||||||
return self._info_command_message
|
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):
|
def from_dict(self, settings: dict):
|
||||||
try:
|
try:
|
||||||
self._welcome_message = settings['WelcomeMessage']
|
self._welcome_message = settings['WelcomeMessage']
|
||||||
@ -99,6 +104,7 @@ class BaseSettings(ConfigurationModelABC):
|
|||||||
self._afk_command_move_message = settings['AFKCommandMoveMessage']
|
self._afk_command_move_message = settings['AFKCommandMoveMessage']
|
||||||
self._help_command_reference_url = settings['HelpCommandReferenceUrl']
|
self._help_command_reference_url = settings['HelpCommandReferenceUrl']
|
||||||
self._info_command_message.from_dict(settings['InfoCommandMessage'])
|
self._info_command_message.from_dict(settings['InfoCommandMessage'])
|
||||||
|
self._user_info_command_message.from_dict(settings['UserInfoCommandMessage'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
||||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||||
|
@ -37,4 +37,4 @@ class HelpCommandService(CommandABC):
|
|||||||
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
|
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
|
||||||
is_persistent = persistent_flag == '--stay'
|
is_persistent = persistent_flag == '--stay'
|
||||||
await self._message_service.send_ctx_msg(ctx, settings.help_command_reference_url, is_persistent=is_persistent)
|
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')
|
||||||
|
@ -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.command_abc import CommandABC
|
||||||
from gismo_core.abc.message_service_abc import MessageServiceABC
|
from gismo_core.abc.message_service_abc import MessageServiceABC
|
||||||
from gismo_core.abc.module_abc import ModuleABC
|
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.model.embed_description_field import EmbedDescriptionField
|
||||||
from gismo_core.service.embed_service import EmbedService
|
from gismo_core.service.embed_service import EmbedService
|
||||||
from modules.base.base_settings import BaseSettings
|
from modules.base.base_settings import BaseSettings
|
||||||
@ -41,35 +42,54 @@ class InfoCommandService(CommandABC):
|
|||||||
async def info(self, ctx: Context):
|
async def info(self, ctx: Context):
|
||||||
self._logger.debug(__name__, f'Received command info {ctx}')
|
self._logger.debug(__name__, f'Received command info {ctx}')
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
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)
|
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)):
|
embed_description = EmbedDescription(
|
||||||
field = embed_description.fields[i]
|
settings.info_command_message.embed_description.title,
|
||||||
if field.value == '$version':
|
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__
|
field.value = gismo.__version__
|
||||||
elif field.value == '$ontime':
|
|
||||||
|
elif settings_field.value == '$ontime':
|
||||||
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)
|
||||||
field.value = f'{ontime}h'
|
field.value = f'{ontime}h'
|
||||||
elif field.value == '$sent_message_count':
|
|
||||||
|
elif settings_field.value == '$sent_message_count':
|
||||||
field.value = client.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
|
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
|
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
|
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
|
field.value = client.moved_users_count
|
||||||
elif field.value == '$modules':
|
|
||||||
|
elif settings_field.value == '$modules':
|
||||||
field.value = ''
|
field.value = ''
|
||||||
for module in ModuleABC.__subclasses__():
|
for module in ModuleABC.__subclasses__():
|
||||||
field.value += f'{module.__name__}\n'
|
field.value += f'{module.__name__}\n'
|
||||||
|
|
||||||
|
embed_description.fields.append(field)
|
||||||
|
|
||||||
await self._message_service.send_ctx_msg(
|
await self._message_service.send_ctx_msg(
|
||||||
ctx,
|
ctx,
|
||||||
EmbedService.get_embed(embed_description)
|
EmbedService.get_embed(embed_description)
|
||||||
)
|
)
|
||||||
self._logger.trace(__name__, f'Finished ping command')
|
self._logger.trace(__name__, f'Finished info command')
|
||||||
|
133
src/modules/base/service/user_info_command_service.py
Normal file
133
src/modules/base/service/user_info_command_service.py
Normal 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')
|
Reference in New Issue
Block a user