Skip to content

Commit

Permalink
Fixed stats posting and removing of message
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed May 17, 2024
1 parent dcbcc93 commit 5bfa99c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
24 changes: 19 additions & 5 deletions sausage_bot/cogs/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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'
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion sausage_bot/util/discord_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5bfa99c

Please sign in to comment.