diff --git a/.gitignore b/.gitignore index 18ecdac..366dcf4 100755 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,10 @@ __pycache__/ .vscode/ sausage_bot/data/json +sausage_bot/data/db sausage_bot/data/logs sausage_bot/data/.env sausage_bot/testing +sausage_bot/test/test_parse/* -out -sausage_bot/data/db -sausage_bot/test/test_parse/* \ No newline at end of file +out \ No newline at end of file diff --git a/dockerfile b/dockerfile index 952a14d..b8be953 100755 --- a/dockerfile +++ b/dockerfile @@ -13,4 +13,4 @@ RUN pipenv install --system --deploy --ignore-pipfile VOLUME [ "/data" ] # Run bot -ENTRYPOINT ["python", "-m", "sausage_bot", "-l", "-lm", "-lp", "-d", "--data-dir", "/data"] +CMD ["python", "-m", "sausage_bot", "--log", "--verbose", "--log-print", "--log-database", "--debug", "--data-dir", "/data"] diff --git a/sausage_bot/cogs/stats.py b/sausage_bot/cogs/stats.py index 1d58e04..3c5d0eb 100755 --- a/sausage_bot/cogs/stats.py +++ b/sausage_bot/cogs/stats.py @@ -9,13 +9,15 @@ from sausage_bot.util.log import log -def get_role_numbers(): +def get_role_numbers(hide_bots: bool = None): 'Get roles and number of members' - guild = discord_commands.get_guild() - member_count = guild.member_count + log.debug(f'`hide_bots` is {hide_bots}') + roles_info = discord_commands.get_roles( + filter_zeroes=True, filter_bots=hide_bots + ) return { - 'member_count': member_count, - 'roles': discord_commands.get_roles(filter_zeroes=True) + 'member_count': len(roles_info), + 'roles': roles_info } @@ -44,29 +46,38 @@ def __init__(self, bot): self.bot = bot # Tasks - @tasks.loop(minutes=5) + @tasks.loop( + minutes=config.env.int('STATS_LOOP', default=5) + ) async def update_stats(): ''' Update interesting stats in a channel post and write the info to the log db. The channel is defined in stats settings db. ''' - def tabify( + async def tabify( dict_in: dict, headers: list, ): - hide_roles = stats_settings['hide_roles'] - hide_roles_lower = [x.lower() for x in hide_roles] + hide_roles = await db_helper.get_output( + template_info=envs.stats_db_schema, + select=('value'), + where=[('setting', 'hide_role')] + ) + hide_roles_lower = [x[0].lower() for x in hide_roles] # TODO var msg log.debug(f'Using this for filter:\n{hide_roles_lower}') text_out = '' if isinstance(dict_in, dict): log.debug( 'Checking `sort_abc` ({}) and `sort_321` ({})'.format( - stats_settings['sort_roles_abc'], - stats_settings['sort_roles_321'] + eval(stats_settings['sort_roles_abc']), + eval(stats_settings['sort_roles_321']) ) ) + if not eval(stats_settings['sort_roles_abc']) and\ + not eval(stats_settings['sort_roles_321']): + stats_settings['sort_roles_abc'] = True if stats_settings['sort_roles_abc']: dict_in = dict(sorted( dict_in.items(), key=lambda x: x[1]['name'] @@ -74,7 +85,7 @@ def tabify( log.debug( f'Sorting roles alphabetically: {list(dict_in)[0:4]}' ) - if stats_settings['sort_roles_321']: + elif eval(stats_settings['sort_roles_321']): dict_in = dict(sorted( dict_in.items(), key=lambda x: x[1]['members'], reverse=True @@ -83,6 +94,7 @@ def tabify( f'Sorting roles by number of members: ' f'{list(dict_in)[0:4]}' ) + # Tabulate the output dict_out = { 'name': [], @@ -92,6 +104,7 @@ def tabify( if role.lower() not in hide_roles_lower: if role != '@everyone': # Add an if to check for filter bot roles + dict_out['name'].append(dict_in[role]['name']) dict_out['members'].append( dict_in[role]['members'] @@ -112,6 +125,7 @@ def tabify( template_info=envs.stats_db_schema ) ) + log.debug(f'`stats_settings` is {stats_settings}') if stats_settings['channel']: stats_channel = stats_settings['channel'] else: @@ -122,18 +136,29 @@ def tabify( lines_in_codebase = _codebase['total_lines'] files_in_codebase = _codebase['total_files'] # Get server members - members = get_role_numbers() - # Update log database if not alredy this day + members = get_role_numbers( + hide_bots=eval(stats_settings['hide_bot_roles']) + ) + # Update log database if not already this day + log.debug('Logging stats') date_exist = await db_helper.get_output( template_info=envs.stats_db_log_schema, order_by=[('datetime', 'DESC')], single=True ) - if datetime_handling.get_dt( - format='date' - ) > datetime_handling.get_dt( - format='date', dt=date_exist[0] - ): + log_stats = False + if date_exist: + if datetime_handling.get_dt( + format='date' + ) > datetime_handling.get_dt( + format='date', dt=date_exist + ): + log_stats = True + else: + log.verbose('Today has already been logged, skipping...') + elif date_exist is None: + log_stats = True + if log_stats: stats_log_inserts.append( ( str(datetime_handling.get_dt('ISO8601')), @@ -146,22 +171,26 @@ def tabify( template_info=envs.stats_db_log_schema, inserts=stats_log_inserts ) - else: - log.verbose('Today has already been logged, skipping...') # Update the stats-msg - if stats_settings['show_role_stats']: + if eval(stats_settings['show_role_stats']): total_members = members['member_count'] - roles_members = tabify( + roles_members = await tabify( dict_in=members['roles'], headers=['Rolle', 'Brukere'] ) dt_log = datetime_handling.get_dt('datetimefull') stats_msg = '' - if stats_settings['show_role_stats']: - stats_msg += f'> Medlemmer\n```'\ + log.debug('`show_role_stats` is {})'.format( + stats_settings['show_role_stats'] + )) + if eval(stats_settings['show_role_stats']): + stats_msg += f'### Medlemmer\n```'\ f'Antall medlemmer: {total_members}\n\n'\ f'{roles_members}```\n' - if stats_settings['show_code_stats']: - stats_msg += f'> Kodebase\n```'\ + log.debug('`show_code_stats` is {}'.format( + stats_settings['show_code_stats'] + )) + if eval(stats_settings['show_code_stats']): + stats_msg += f'### Kodebase\n```'\ f'Antall filer med kode: {files_in_codebase}\n'\ f'Antall linjer med kode: {lines_in_codebase}```\n' stats_msg += f'```(Serverstats sist oppdatert: {dt_log})```\n' @@ -186,7 +215,7 @@ async def setup(bot): log.verbose('Checking db') # Convert json to sqlite db-files if exists stats_file_inserts = None - stats_settings_inserts = None + stats_settings_inserts = envs.stats_db_schema['inserts'] stats_log_inserts = None if file_io.file_size(envs.stats_file): log.verbose('Found old json file') diff --git a/sausage_bot/util/cogs.py b/sausage_bot/util/cogs.py index 4d3e9a8..b2b9fbc 100755 --- a/sausage_bot/util/cogs.py +++ b/sausage_bot/util/cogs.py @@ -257,8 +257,7 @@ def get_cogs_list(cogs_file): log.debug(f'Got this from `cogs_file`: {cogs_file}') text_out = '```{}```'.format( tabulate( - cogs_file, headers=['Cog', 'Status'], - tablefmt='presto' + cogs_file, headers=['Cog', 'Status'] ) ) log.debug(f'Returning:\n{text_out}') diff --git a/sausage_bot/util/db_helper.py b/sausage_bot/util/db_helper.py index ded3628..851a9e6 100755 --- a/sausage_bot/util/db_helper.py +++ b/sausage_bot/util/db_helper.py @@ -333,6 +333,7 @@ async def insert_many_all( input_singles = False input_multiples = False _cmd = f'INSERT INTO {table_name} VALUES(' + print(inserts) if isinstance(inserts[0], str): _cmd += ', '.join('?'*len(inserts)) input_singles = True diff --git a/sausage_bot/util/discord_commands.py b/sausage_bot/util/discord_commands.py index 72963c3..c5914d2 100755 --- a/sausage_bot/util/discord_commands.py +++ b/sausage_bot/util/discord_commands.py @@ -133,16 +133,21 @@ def get_sorted_scheduled_events(): return out -def get_roles(filter_zeroes=None): +def get_roles(filter_zeroes=None, filter_bots=None): ''' Get a dict of all roles on server and their ID's #autodoc skip# ''' + log.debug(f'`filter_zeroes` is {filter_zeroes}') + log.debug(f'`filter_bots` is {filter_bots}') roles_dict = {} # Get all roles and their IDs for role in get_guild().roles: if filter_zeroes and len(role.members) == 0: continue + if filter_bots: + if role.is_bot_managed(): + continue roles_dict[role.name.lower()] = { 'name': role.name, 'id': role.id, @@ -217,11 +222,12 @@ async def update_stats_post(stats_info, stats_channel): ''' 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]}') + 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 diff --git a/sausage_bot/util/envs.py b/sausage_bot/util/envs.py index 793d647..04bd6ea 100755 --- a/sausage_bot/util/envs.py +++ b/sausage_bot/util/envs.py @@ -195,6 +195,15 @@ 'items': [ 'setting TEXT NOT NULL', 'value TEXT NOT NULL' + ], + 'inserts': [ + ['channel', ''], + ['hide_role', ''], + ['hide_bot_roles', True], + ['show_code_stats', False], + ['show_role_stats', True], + ['sort_roles_abc', False], + ['sort_roles_321', False] ] } diff --git a/sausage_bot/util/feeds_core.py b/sausage_bot/util/feeds_core.py index d7ee486..d00c0aa 100755 --- a/sausage_bot/util/feeds_core.py +++ b/sausage_bot/util/feeds_core.py @@ -445,8 +445,7 @@ def split_list(lst, chunk_size): table_out = tabulate( tabular_data=feeds_out, headers=headers, - maxcolwidths=maxcolwidths, - tablefmt='presto' + maxcolwidths=maxcolwidths ) return split_lengthy_list(table_out) diff --git a/sausage_bot/util/log/log.py b/sausage_bot/util/log/log.py index 7672085..e9ffa93 100755 --- a/sausage_bot/util/log/log.py +++ b/sausage_bot/util/log/log.py @@ -43,25 +43,28 @@ def log_function( pretty Prettify specific output. Works on dict, list and tuple sameline Reuse line for next output ''' - if color is None: - color = Fore.GREEN - else: - color = eval('Fore.{}'.format(color.upper())) - if extra_color is None: - extra_color = Fore.GREEN - else: - extra_color = eval('Fore.{}'.format(extra_color.upper())) + if args.log_print: + if color is None: + color = Fore.GREEN + else: + color = eval('Fore.{}'.format(color.upper())) + if extra_color is None: + extra_color = Fore.GREEN + else: + extra_color = eval('Fore.{}'.format(extra_color.upper())) function_name = log_func_name() - if args.log_highlight is not None and str(args.log_highlight)\ - in function_name['name']: - color = Fore.RED + if args.log_print: + if args.log_highlight is not None and str(args.log_highlight)\ + in function_name['name']: + color = Fore.RED dt = pendulum.now(config.TIMEZONE) _dt_full = dt.format(f'DD.MM.YYYY HH.mm.ss') - log_out = '{color}{style}[ {dt} ] '.format( - color=color, - style=Style.BRIGHT, - dt=_dt_full - ) + if args.log_print: + log_out = '{color}{style}[ {dt} ] '.format( + color=color, + style=Style.BRIGHT, + dt=_dt_full + ) if args.log_print: if extra_info: log_out += '[ {extra_info} ]'.format( @@ -87,7 +90,6 @@ def log_function( else: log_out += str(log_in) if sameline: - # TODO Denne fungerer nok kanskje ikke helt som forventa? try: max_cols, max_rows = os.get_terminal_size(0) except (OSError): @@ -95,7 +97,7 @@ def log_function( msg_len = len(str(log_out)) rem_len = max_cols - msg_len - 2 print('{}{}'.format( - log_out, ' ' * rem_len + log_out, ' '*rem_len ), end='\r') else: print(log_out) @@ -105,7 +107,6 @@ def log_function( log_out += '[ {} ] '.format(function_name['name']) log_out += str(log_in) else: - log_out += '\n' dt = pendulum.now(config.TIMEZONE) _dt_rev = dt.format(f'YYYY-MM-DD HH.mm.ss') _logfilename = envs.LOG_DIR / f'{_dt_rev}.log'