From 5bfa99c0975405800e3a1b1470317027ee564fc0 Mon Sep 17 00:00:00 2001 From: geirawsm Date: Fri, 17 May 2024 23:55:46 +0200 Subject: [PATCH] Fixed stats posting and removing of message --- sausage_bot/cogs/stats.py | 24 +++++++++++++++++++----- sausage_bot/util/discord_commands.py | 27 ++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/sausage_bot/cogs/stats.py b/sausage_bot/cogs/stats.py index c53e67a..f2f3da0 100755 --- a/sausage_bot/cogs/stats.py +++ b/sausage_bot/cogs/stats.py @@ -4,6 +4,7 @@ from discord.ext import commands, tasks import discord from tabulate import tabulate +import typing from sausage_bot.util import envs, datetime_handling, file_io, config from sausage_bot.util import discord_commands, db_helper @@ -104,7 +105,8 @@ async def stats_posting_start( name='stop', description='Stop posting' ) async def stats_posting_stop( - self, interaction: discord.Interaction + self, interaction: discord.Interaction, + remove_post: typing.Literal['Yes', 'No'] ): await interaction.response.defer(ephemeral=True) log.log('Task stopped') @@ -117,6 +119,18 @@ async def stats_posting_stop( ], updates=('status', 'stopped') ) + if remove_post.lower() == 'yes': + stats_settings = dict( + await db_helper.get_output( + template_info=envs.stats_db_schema, + select=('setting', 'value') + ) + ) + if len(stats_settings['channel']) > 0: + stats_channel = stats_settings['channel'] + else: + stats_channel = 'stats' + await discord_commands.remove_stats_post(stats_channel) await interaction.followup.send( 'Stats posting stopped' ) @@ -289,12 +303,13 @@ async def tabify( dict_out, headers=headers, numalign='center' ) ) - log.debug(f'Returning: {text_out}') + log.debug(f'Returning: {text_out[0:100]}...') return text_out else: log.more('`dict_in` is not a dict. Check the input.') - log.log('Starting `update_stats`') + upd_mins = config.env.int('STATS_LOOP', default=5) + log.log(f'Starting `update_stats`, updating each {upd_mins} minute') stats_settings = dict( await db_helper.get_output( template_info=envs.stats_db_schema, @@ -373,7 +388,7 @@ async def tabify( stats_msg += f'```(Serverstats sist oppdatert: {dt_log})```\n' log.verbose( f'Trying to post stats to `{stats_channel}`:\n' - f'{stats_msg}' + f'{stats_msg[0:100]}...' ) await discord_commands.update_stats_post( stats_msg, stats_channel @@ -436,7 +451,6 @@ async def setup(bot): log.verbose('Registering cog to bot') await bot.add_cog(Stats(bot)) - Stats.update_stats.start() task_list = await db_helper.get_output( template_info=envs.tasks_db_schema, diff --git a/sausage_bot/util/discord_commands.py b/sausage_bot/util/discord_commands.py index c4c483b..8f21a31 100755 --- a/sausage_bot/util/discord_commands.py +++ b/sausage_bot/util/discord_commands.py @@ -266,9 +266,34 @@ async def update_stats_post(stats_info, stats_channel): return if found_stats_msg is False: # TODO var msg - log.debug('Found post with `Serverstats:`, editing...') + log.debug('Creating stats message') await channel_out.send(stats_info) +async def remove_stats_post(stats_channel): + ''' + Remove stats-post + #autodoc skip# + ''' + server_channels = get_text_channel_list() + if stats_channel in server_channels: + log.debug(f'Found stats channel {stats_channel}') + channel_out = config.bot.get_channel(server_channels[stats_channel]) + found_stats_msg = False + async for msg in channel_out.history(limit=10): + # TODO var msg + log.debug(f'Got msg: ({msg.author.id}) {msg.content[0:50]}...') + if str(msg.author.id) == config.BOT_ID: + if 'Serverstats sist' in str(msg.content): + # TODO var msg + log.debug('Found post with `Serverstats sist`, removing...') + await msg.delete() + found_stats_msg = True + return + if found_stats_msg is False: + # TODO var msg + log.debug('No stats post found') + + if __name__ == "__main__": pass