Archived
1
0
Fork 0
This commit is contained in:
Ethanell 2020-03-27 16:00:12 +01:00
parent ddb3994fbf
commit f4f77d82f8
7 changed files with 87 additions and 0 deletions

3
.gitignore vendored
View file

@ -127,3 +127,6 @@ dmypy.json
# Pyre type checker
.pyre/
# Bot configuration
config.json

8
backup_bot/__init__.py Normal file
View file

@ -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"))

20
backup_bot/config.py Normal file
View file

@ -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")

20
backup_bot/logger.py Normal file
View file

@ -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)

1
config_exemple.json Normal file
View file

@ -0,0 +1 @@
{"prefix": "!", "token": "GOOD_BOT_TOKEN", "admin_id": "GOOD_USER_ID"}

3
extensions/__init__.py Normal file
View file

@ -0,0 +1,3 @@
from backup_bot import bot
bot.load_extension("extensions.help")

32
extensions/help.py Normal file
View file

@ -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")