From f4f77d82f8c0cf640b7f0c9c0091056135e4d3ae Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 27 Mar 2020 16:00:12 +0100 Subject: [PATCH] Base bot --- .gitignore | 3 +++ backup_bot/__init__.py | 8 ++++++++ backup_bot/config.py | 20 ++++++++++++++++++++ backup_bot/logger.py | 20 ++++++++++++++++++++ config_exemple.json | 1 + extensions/__init__.py | 3 +++ extensions/help.py | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 87 insertions(+) create mode 100644 backup_bot/__init__.py create mode 100644 backup_bot/config.py create mode 100644 backup_bot/logger.py create mode 100644 config_exemple.json create mode 100644 extensions/__init__.py create mode 100644 extensions/help.py diff --git a/.gitignore b/.gitignore index b6e4761..c94c690 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# Bot configuration +config.json diff --git a/backup_bot/__init__.py b/backup_bot/__init__.py new file mode 100644 index 0000000..9de86cb --- /dev/null +++ b/backup_bot/__init__.py @@ -0,0 +1,8 @@ +from backup_bot.config import config +from discord.ext import commands + +bot = commands.Bot(command_prefix=config.get("prefix")) + +import extensions + +bot.run(config.get("token")) diff --git a/backup_bot/config.py b/backup_bot/config.py new file mode 100644 index 0000000..7885361 --- /dev/null +++ b/backup_bot/config.py @@ -0,0 +1,20 @@ +from backup_bot.logger import logger +from os.path import isfile +from json import load + +logger = logger.getChild("Config") + +if not isfile("config.json"): + logger.critical("Config file not found !") + exit(1) + +config = {} +with open("config.json") as conf: + logger.info("Loading configuration") + try: + config.update(load(conf)) + except Exception as e: + logger.critical(f"Fail to load configuration: {e}") + exit(1) + else: + logger.info("Configuration load successful") diff --git a/backup_bot/logger.py b/backup_bot/logger.py new file mode 100644 index 0000000..b30383d --- /dev/null +++ b/backup_bot/logger.py @@ -0,0 +1,20 @@ +import logging +from logging import handlers +from os import mkdir +from os.path import isdir + +if not isdir("../logs"): + mkdir("../logs") + +log_format = "{%(levelname)s}[%(asctime)s]: %(name)s | %(message)s" + +logging.basicConfig( + format=log_format, + level=logging.INFO +) +logger = logging.getLogger("BackupBot") +handler = handlers.TimedRotatingFileHandler("../logs/current.log", when="d", interval=1) +handler.suffix = "%Y-%m-%d" +handler.style = log_format +handler.setFormatter(logging.Formatter(log_format)) +logger.addHandler(handler) diff --git a/config_exemple.json b/config_exemple.json new file mode 100644 index 0000000..b49a4a0 --- /dev/null +++ b/config_exemple.json @@ -0,0 +1 @@ +{"prefix": "!", "token": "GOOD_BOT_TOKEN", "admin_id": "GOOD_USER_ID"} \ No newline at end of file diff --git a/extensions/__init__.py b/extensions/__init__.py new file mode 100644 index 0000000..34f902b --- /dev/null +++ b/extensions/__init__.py @@ -0,0 +1,3 @@ +from backup_bot import bot + +bot.load_extension("extensions.help") \ No newline at end of file diff --git a/extensions/help.py b/extensions/help.py new file mode 100644 index 0000000..2ff28e5 --- /dev/null +++ b/extensions/help.py @@ -0,0 +1,32 @@ +from discord.ext import commands +from backup_bot.logger import logger + + +extension_name = "help" +logger = logger.getChild(extension_name) + + +@commands.command("help") +async def help_cmd(ctx): + await ctx.send("Help !") + + +def setup(bot): + logger.info(f"Loading of {extension_name} extension") + try: + bot.help_command = None + bot.add_command(help_cmd) + except Exception as e: + logger.error(f"Error loading extension {extension_name}: {e}") + else: + logger.info(f"Extension {extension_name} load successful") + + +def teardown(bot): + logger.info(f"Unloading of {extension_name} extension") + try: + bot.remove_command("help") + except Exception as e: + logger.error(f"Error unloading extension {extension_name}: {e}") + else: + logger.info(f"Extension {extension_name} unload successful")