Improved formatting #164

This commit is contained in:
Sven Heidemann 2023-01-12 07:29:45 +01:00
parent 053c190c78
commit 5d6c55fc86
3 changed files with 91 additions and 51 deletions

View File

@ -32,13 +32,11 @@ class MessageServiceABC(ABC):
@abstractmethod
async def send_ctx_msg(
self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None,
is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None,
without_tracking=True
is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=True
) -> Optional[discord.Message]: pass
@abstractmethod
async def send_interaction_msg(
self, interaction: Interaction, message: Union[str, discord.Embed],
is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None,
without_tracking=True, **kwargs
self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False,
is_public: bool = False, wait_before_delete: int = None, without_tracking=True, **kwargs
): pass

View File

@ -18,13 +18,14 @@ from bot_data.abc.client_repository_abc import ClientRepositoryABC
class MessageService(MessageServiceABC):
def __init__(self, config: ConfigurationABC, logger: MessageLogger, bot: DiscordBotServiceABC, clients: ClientRepositoryABC, db: DatabaseContextABC):
def __init__(self, config: ConfigurationABC, logger: MessageLogger, bot: DiscordBotServiceABC,
clients: ClientRepositoryABC, db: DatabaseContextABC):
self._config = config
self._logger = logger
self._bot = bot
self._clients = clients
self._db = db
async def delete_messages(self, messages: List[discord.Message], guild_id: int, without_tracking=False):
self._logger.debug(__name__, f'Try to delete {messages.count()} messages')
server_st: ServerSettings = self._config.get_configuration(f'ServerSettings_{guild_id}')
@ -32,12 +33,13 @@ class MessageService(MessageServiceABC):
for message in messages:
await self.delete_message(message, mass_delete=True, without_tracking=without_tracking)
self._logger.debug(__name__, 'Deleting messages finished')
async def delete_message(self, message: discord.Message, mass_delete=False, without_tracking=False):
guild_id = \
message.guild.id if message.guild is not None else \
message.channel.guild.id if message.channel is not None and message.channel.guild is not None else \
message.reference.guild_id if message.reference is not None else None
message.channel.guild.id if message.channel is not None and message.channel.guild is not None else \
message.reference.guild_id if message.reference is not None and message.reference.guild_id is not None \
else None
server_st: ServerSettings = self._config.get_configuration(f'ServerSettings_{guild_id}')
if not mass_delete:
@ -53,8 +55,11 @@ class MessageService(MessageServiceABC):
self._clients.append_deleted_message_count(self._bot.user.id, guild_id, 1)
self._db.save_changes()
self._logger.info(__name__, f'Deleted message {message}')
async def send_channel_message(self, channel: discord.TextChannel, message: Union[str, discord.Embed], is_persistent: bool = False, wait_before_delete: int = None, without_tracking=False):
async def send_channel_message(
self, channel: discord.TextChannel, message: Union[str, discord.Embed], is_persistent: bool = False,
wait_before_delete: int = None, without_tracking=False
):
self._logger.debug(__name__, f'Try to send message\n\t{message}\n\tto: {channel}')
msg = None
try:
@ -77,8 +82,11 @@ class MessageService(MessageServiceABC):
return
await self.delete_message(msg, without_tracking)
async def send_dm_message(self, message: Union[str, discord.Embed], receiver: Union[discord.User, discord.Member], without_tracking=False):
async def send_dm_message(
self, message: Union[str, discord.Embed], receiver: Union[discord.User, discord.Member],
without_tracking=False
):
self._logger.debug(__name__, f'Try to send message\n\t{message}\n\tto: {receiver}')
try:
if isinstance(message, discord.Embed):
@ -92,13 +100,16 @@ class MessageService(MessageServiceABC):
self._clients.append_sent_message_count(self._bot.user.id, receiver.guild.id, 1)
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, is_public: bool = False, wait_before_delete: int = None, without_tracking=False) -> Optional[discord.Message]:
async def send_ctx_msg(
self, ctx: Context, message: Union[str, discord.Embed], file: discord.File = None,
is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=False
) -> Optional[discord.Message]:
if ctx is None:
self._logger.warn(__name__, 'Message context is empty')
self._logger.debug(__name__, f'Message: {message}')
return None
self._logger.debug(__name__, f'Try to send message\t\t{message}\n\tto: {ctx.channel}')
msg = None
try:
@ -125,7 +136,10 @@ class MessageService(MessageServiceABC):
return msg
async def send_interaction_msg(self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False, is_public: bool = False, wait_before_delete: int = None, without_tracking=False, **kwargs):
async def send_interaction_msg(
self, interaction: Interaction, message: Union[str, discord.Embed], is_persistent: bool = False,
is_public: bool = False, wait_before_delete: int = None, without_tracking=False, **kwargs
):
if interaction is None:
self._logger.warn(__name__, 'Message context is empty')
self._logger.debug(__name__, f'Message: {message}')

View File

@ -192,16 +192,20 @@ class LevelGroup(DiscordCommandABC):
try:
self._levels.add_level(level)
self._db.save_changes()
self._logger.info(__name__,
f'Saved level {name} with color {color}, min_xp {min_xp} and permissions {permissions}')
self._logger.info(
__name__, f'Saved level {name} with color {color}, min_xp {min_xp} and permissions {permissions}'
)
except Exception as e:
self._logger.error(__name__,
f'Could not save level {name} with color {color}, min_xp {min_xp} and permissions {permissions}',
e)
self._logger.error(
__name__,
f'Could not save level {name} with color {color}, min_xp {min_xp} and permissions {permissions}',
e
)
else:
await self._message_service.send_ctx_msg(ctx,
self._t.transform('modules.level.create.created').format(name,
permissions))
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('modules.level.create.created').format(name, permissions)
)
await self._seed_levels(ctx)
self._logger.trace(__name__, f'Finished command level create')
@ -211,23 +215,30 @@ class LevelGroup(DiscordCommandABC):
app_commands.Choice[str]]:
# value in rg format see:
# https://discordpy.readthedocs.io/en/latest/api.html#discord.Colour.to_rgb
return [app_commands.Choice(name=self._t.transform(f'common.colors.{color}'),
value=f'rgb({code[0]}, {code[1]}, {code[2]})') for color, code in self._colors]
return [
app_commands.Choice(
name=self._t.transform(f'common.colors.{color}'), value=f'rgb({code[0]}, {code[1]}, {code[2]})'
) for color, code in self._colors
]
@level.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_admin()
async def edit(self, ctx: Context, level: str, name: str = None, color: str = None, min_xp: int = None,
permissions: int = None):
async def edit(
self, ctx: Context, level: str, name: str = None, color: str = None, min_xp: int = None,
permissions: int = None
):
self._logger.debug(__name__, f'Received command level edit {ctx}')
server = self._servers.get_server_by_discord_id(ctx.guild.id)
level_from_db = self._levels.get_levels_by_server_id(server.server_id).where(
lambda l: l.name == level).single_or_default()
if level_from_db is None:
await self._message_service.send_ctx_msg(ctx,
self._t.transform('modules.level.edit.not_found').format(level))
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('modules.level.edit.not_found').format(level)
)
return
guild: Guild = self._bot.guilds.where(lambda g: g == ctx.guild).single()
@ -240,9 +251,10 @@ class LevelGroup(DiscordCommandABC):
try:
level_from_db.color = hex(discord.Colour.from_str(color).value)
except Exception as e:
await self._message_service.send_ctx_msg(ctx,
self._t.transform('modules.level.edit.color_invalid').format(
color))
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('modules.level.edit.color_invalid').format(color)
)
self._logger.error(__name__, f'Error parsing color {color}', e)
return
@ -261,14 +273,21 @@ class LevelGroup(DiscordCommandABC):
try:
self._levels.update_level(level_from_db)
self._db.save_changes()
await role.edit(name=level_from_db.name, permissions=discord.Permissions(level_from_db.permissions),
colour=discord.Colour(int(level_from_db.color, 16)))
self._logger.info(__name__,
f'Saved level {level_from_db.name} with color {level_from_db.color}, min_xp {level_from_db.min_xp} and permissions {level_from_db.permissions}')
await role.edit(
name=level_from_db.name,
permissions=discord.Permissions(level_from_db.permissions),
colour=discord.Colour(int(level_from_db.color, 16))
)
self._logger.info(
__name__,
f'Saved level {level_from_db.name} with color {level_from_db.color}, min_xp {level_from_db.min_xp} and permissions {level_from_db.permissions}'
)
except Exception as e:
self._logger.error(__name__,
f'Could not save level {level} with color {color}, min_xp {min_xp} and permissions {permissions}',
e)
self._logger.error(
__name__,
f'Could not save level {level} with color {color}, min_xp {min_xp} and permissions {permissions}',
e
)
else:
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.edit.edited').format(level))
await self._seed_levels(ctx)
@ -285,8 +304,12 @@ class LevelGroup(DiscordCommandABC):
app_commands.Choice[str]]:
# value in rg format see:
# https://discordpy.readthedocs.io/en/latest/api.html#discord.Colour.to_rgb
return [app_commands.Choice(name=self._t.transform(f'common.colors.{color}'),
value=f'rgb({code[0]}, {code[1]}, {code[2]})') for color, code in self._colors]
return [
app_commands.Choice(
name=self._t.transform(f'common.colors.{color}'),
value=f'rgb({code[0]}, {code[1]}, {code[2]})'
) for color, code in self._colors
]
@level.command()
@commands.guild_only()
@ -300,9 +323,10 @@ class LevelGroup(DiscordCommandABC):
lambda l: l.name == level).first_or_default()
if level_from_db is None:
self._logger.debug(__name__, f'level {level} not found')
await self._message_service.send_ctx_msg(ctx,
self._t.transform('modules.level.remove.error.not_found').format(
level))
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('modules.level.remove.error.not_found').format(level)
)
self._logger.trace(__name__, f'Finished command level remove')
return
@ -317,8 +341,10 @@ class LevelGroup(DiscordCommandABC):
except Exception as e:
self._logger.error(__name__, f'Could not remove level {level}', e)
else:
await self._message_service.send_ctx_msg(ctx,
self._t.transform('modules.level.remove.success').format(level))
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('modules.level.remove.success').format(level)
)
await self._seed_levels(ctx)
self._logger.trace(__name__, f'Finished command level remove')
@ -417,8 +443,10 @@ class LevelGroup(DiscordCommandABC):
lambda l: l.name == level).single_or_default()
if new_level is None:
await self._message_service.send_ctx_msg(ctx,
self._t.transform('modules.level.set.not_found').format(level))
await self._message_service.send_ctx_msg(
ctx,
self._t.transform('modules.level.set.not_found').format(level)
)
self._logger.trace(__name__, f'Finished command level set')
return