Skip to content

Commit

Permalink
Merge pull request #9 from f11y11/auto_updates
Browse files Browse the repository at this point in the history
Multiple fixes, logging consistency and removal of auto updates
  • Loading branch information
fuyu authored Apr 28, 2023
2 parents 5fda50a + 529e1ef commit 9daa1a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
3 changes: 1 addition & 2 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
case_insensitive=True,
intents=intents,
help_command=None,
allowed_mentions=am,
owner_ids=[893843824894435370])
allowed_mentions=am)


@bot.event
Expand Down
2 changes: 0 additions & 2 deletions config.yml-template
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ logging:
# true or false determining if logs should be printed to stderr
console: false

auto_updates: false

# set to true for faster loading of player avatars that can be outdated at times
# set to false for non-cached, up-to-date player avatars
cache_avatars: true
39 changes: 14 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
import os
import asyncio
import subprocess
import sys

from dotenv import load_dotenv
from bot.bot import main, config

logging_data = config.get('logging')
load_dotenv()

if logging_data := config.get('logging'):
if __name__ == '__main__':
if logging_level := logging_data.get('level'):
if logging_level and logging_level not in (
'error',
Expand All @@ -18,30 +18,19 @@
):
logging_level = 'info'

if logging_filename := logging_data.get('filename'):
if not logging_filename.endswith('.log'):
logging_filename = logging_filename + '.log'

logging_console: bool = logging_data.get('console', False)

logging.basicConfig(level=getattr(logging, logging_level.upper()), filename=logging_filename)
if logging_console:
logging.getLogger().addHandler(logging.StreamHandler())
if logging_filename := logging_data.get('filename'):
if not logging_filename.endswith('.log'):
logging_filename = logging_filename + '.log'

if not logging_console and not logging_filename:
print('''
You have enabled logging but set left both console and filename blank.
Nothing will be printed to the console or written to a file.
''')
if logging_data.get('console', False):
logging.getLogger().addHandler(logging.StreamHandler())
elif not logging_filename:
print('''
You have enabled logging but set left both console and filename blank.
Nothing will be printed to the console or written to a file.
''')

load_dotenv()

if __name__ == '__main__':
if config.get('auto_updates', False):
pull = str(subprocess.check_output(['git', 'pull']), 'UTF-8')
if 'up to date' not in pull:
logging.info('Auto-update has installed the latest version. Restarting...')
os.execv(sys.executable, ['python'] + sys.argv)
logging.basicConfig(level=getattr(logging, logging_level.upper()), filename=logging_filename)

if TOKEN := os.environ.get('TOKEN'):
logging.info('Starting bot')
Expand Down

0 comments on commit 9daa1a6

Please sign in to comment.