From b03dc30c6ce5ce007b96008892853bcb99f05175 Mon Sep 17 00:00:00 2001 From: Romain J Date: Sun, 1 Sep 2019 23:01:43 +0200 Subject: [PATCH] add(launcher): start launcher development --- .gitignore | 5 ++- config.py.example | 20 ------------ first_run/__init__.py | 5 +++ first_run/config_generator.py | 61 +++++++++++++++++++++++++++++++++++ first_run/langs.py | 41 +++++++++++++++++++++++ launcher.py | 43 ++++++++++++++++++++++++ logs/tuxbot.log | 0 requirements.txt | 13 +++----- 8 files changed, 158 insertions(+), 30 deletions(-) delete mode 100644 config.py.example create mode 100644 first_run/__init__.py create mode 100644 first_run/config_generator.py create mode 100644 first_run/langs.py create mode 100644 launcher.py create mode 100644 logs/tuxbot.log diff --git a/.gitignore b/.gitignore index 3663aba..7d9f536 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ config.py private.py #jetbrains -.idea/ \ No newline at end of file +.idea/ + +#other +.logs/* \ No newline at end of file diff --git a/config.py.example b/config.py.example deleted file mode 100644 index c394242..0000000 --- a/config.py.example +++ /dev/null @@ -1,20 +0,0 @@ -token = "INSERT TOKEN HERE" -client_id = -log_channel_id = -main_server_id = - -game = "PLAYING_GAME_HERE" -prefix = ["."] -description = """ -Je suis TuxBot, le bot qui vit de l'OpenSource ! ;) -""" - -mysql = { - "host": "localhost", - "username": "msqlusername", - "password": "msqlpasswd", - "dbname": "mysqldb" -} - -authorized_id = ['admin ids here'] -unkickable_id = ['unkickable ids here'] diff --git a/first_run/__init__.py b/first_run/__init__.py new file mode 100644 index 0000000..669c547 --- /dev/null +++ b/first_run/__init__.py @@ -0,0 +1,5 @@ +from .config import Config + +setup = Config() +setup.ask() +setup.save() diff --git a/first_run/config_generator.py b/first_run/config_generator.py new file mode 100644 index 0000000..506c176 --- /dev/null +++ b/first_run/config_generator.py @@ -0,0 +1,61 @@ +from .langs import locales, texts + + +class Config: + def __init__(self): + self.config = { + 'log_channel_id': '', + 'main_server_id': '', + 'authorized_id': '[admin ids here (in int)]', + 'unkickable_id': '[unkickable ids here (in int)]' + } + + def input(self, key, **kwargs): + lang = self.config.get('lang', 'multiple') + + print('\n\033[4m' + texts.get(lang).get(key) + '\033[0m') + response = input('> ') + + if kwargs.get('valid'): + while response not in kwargs.get('valid'): + print('\033[36m' + '/'.join(kwargs.get('valid')) + '\033[0m') + response = input('> ') + + if not kwargs.get('empty', True): + while len(response) == 0: + print('\033[41m' + texts.get(lang).get('not_empty') + + '\033[0m') + response = input('> ') + else: + response = kwargs.get('default', None) if len(response) == 0 \ + else response + + self.config[key] = response + + def ask(self): + self.input('lang', valid=locales) + self.input('token', empty=False) + self.input('postgresql_username', empty=False) + self.input('postgresql_password', empty=False) + self.input('postgresql_dbname', empty=False) + + print('\n\n\033[4;36m' + texts.get(self.config.get('lang')).get('misc') + + '\033[0m\n') + + self.input('activity', empty=True) + self.input('prefix', empty=True) + + def save(self): + with open('config.py', 'w') as file: + postgresql = f"postgresql://" \ + f"{self.config.get('postgresql_username')}:" \ + f"{self.config.get('postgresql_password')}@host/" \ + f"{self.config.get('postgresql_dbname')}" + file.write(f"postgresql = '{postgresql}'\n") + + for key, value in self.config.items(): + if not key.startswith('postgresql_'): + value = f"'{value}'" if type(value) is str else value + file.write(f"{key} = {value}\n") + print('\n\n\033[4;36m' + texts.get(self.config.get('lang')).get('end') + + '\033[0m\n') diff --git a/first_run/langs.py b/first_run/langs.py new file mode 100644 index 0000000..4d1ae7b --- /dev/null +++ b/first_run/langs.py @@ -0,0 +1,41 @@ +locales = ['fr', 'en'] +texts = { + 'fr': { + 'token': "Veuillez entrer le token", + 'not_empty': "Cette valeur ne doit pas être vide", + + 'postgresql_username': "Veuillez entrer le nom d'utilisateur de postgresql", + 'postgresql_password': "Veuillez entrer le mot de passe de postgresql", + 'postgresql_dbname': "Veuillez entrer le nom de la base de donnée", + + 'misc': 'Autre', + + 'activity': "Joue à ...", + 'prefix': "Prefixe (par defaut : @tuxbot)", + + "end": "Configuration terminée, vous pouvez à tout moment la rectifier en modifiant le fichier config.py" + }, + + 'en': { + 'token': "Please enter the token", + 'not_empty': "This value must not be empty", + + 'postgresql_username': "Please enter the postgresql username", + 'postgresql_password': "Please enter the postgresql password", + 'postgresql_dbname': "Please enter the database name", + + 'misc': 'Misc', + + 'activity': "Playing ...", + 'prefix': "Prefix (default is @tuxbot)", + + "end": "Configuration completed, you can fix it at any time by modifying the config.py file" + }, + + 'multiple': { + 'lang': "Veuillez choisir une langue | Please choose a language " + "[fr/en]", + 'not_empty': "Cette valeur ne doit pas être vide |" + " This value must not be empty" + } +} diff --git a/launcher.py b/launcher.py new file mode 100644 index 0000000..8cc5584 --- /dev/null +++ b/launcher.py @@ -0,0 +1,43 @@ +import logging +import contextlib + + +try: + import config +except ModuleNotFoundError: + import first_run + + +@contextlib.contextmanager +def setup_logging(): + try: + logging.getLogger('discord').setLevel(logging.INFO) + logging.getLogger('discord.http').setLevel(logging.WARNING) + + log = logging.getLogger() + log.setLevel(logging.INFO) + + handler = logging.FileHandler(filename='logs/tuxbot.log', + encoding='utf-8', mode='w') + fmt = logging.Formatter('[{asctime}] [{levelname:<7}]' + ' {name}: {message}', + '%Y-%m-%d %H:%M:%S', style='{') + + handler.setFormatter(fmt) + log.addHandler(handler) + + yield + finally: + handlers = log.handlers[:] + for hdlr in handlers: + hdlr.close() + log.removeHandler(hdlr) + + +def run_bot(): + pass # Todo: initialize bot, postgresql,... + + +if __name__ == '__main__': + with setup_logging(): + run_bot() diff --git a/logs/tuxbot.log b/logs/tuxbot.log new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt index 77cbbcd..68f77bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,4 @@ -pymysql -beautifulsoup4 -lxml==4.2.4 -bs4 -pytz -requests -wikipedia -pillow -gtts \ No newline at end of file +discord.py +lxml +click +asyncpg>=0.12.0 \ No newline at end of file