| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import logging, os
- import discord
- from discord.ext import commands
- import asyncpg, asyncio
- def hint_quit(): # Hint how to edit the settings and quit
- logging.info("")
- logging.info(" edit local_settings.py")
- logging.info("")
- quit()
- def missing_config(): # Copy or create settings file if missing
- logging.basicConfig(level=logging.DEBUG)
- if not os.path.exists("local_settings.py"):
- logging.error("Settings file not found.")
- logging.info("Copying local_settings_example.py to local_settings.py")
- try:
- os.system("cp local_settings_example.py local_settings.py")
- except FileNotFoundError:
- logging.info("local_settings_example.py not found, creating local_settings.py")
- with open("local_settings.py", "w") as settings_file:
- settings_file.writelines(
- [
- "import logging",
- "LOG_LEVEL = logging.INFO # Options: CRITICAL, ERROR, WARNING, INFO, and DEBUG",
- "",
- "DATABASE_NAME = \"\"",
- "DATABASE_USER = \"\"",
- "DATABASE_HOST = \"\"",
- "DATABASE_PASSWORD = \"\"",
- "",
- "WEB_HOST = \"\"",
- "WEB_SCHEME = \"\"",
- "",
- "DISCORD_TOKEN = \"\"",
- "COMMAND_PREFIX = \"\"",
- ]
- )
- logging.error("Settings undefined.")
- logging.info("Configure the settings:")
- hint_quit()
- def correct_setting(setting): # Hint to correct specific setting and quit
- logging.info("Correct the %s in local_settings.py", setting)
- hint_quit()
- # Attempt to import the local settings and quit gracefully on failure
- try:
- import local_settings as settings # Environment dependant settings stored in local_settings.py, untracked by .gitinore
- except ModuleNotFoundError: # Local settings module import failure
- missing_config() # Prepare for configuration and inform operator
- async def main():
- # Check additional settings
- if not settings.WEB_HOST:
- logging.error("Web host undefinded.")
- correct_setting("WEB_HOST")
- if not settings.WEB_SCHEME:
- logging.error("Web scheme undefinded.")
- correct_setting("WEB_SCHEME")
- # Set loglevel
- try:
- logging.basicConfig(level=settings.LOG_LEVEL)
- except AttributeError:
- missing_config()
- # Define robot
- intents = discord.Intents.default()
- intents.message_content = True
- try:
- bot = commands.Bot(
- command_prefix=settings.COMMAND_PREFIX,
- description="Charlie's Angels bot",
- intents=intents,
- case_insensitive=True,
- )
- except AttributeError:
- missing_config()
- # Load extensions
- default_extensions = [
- "commands.admin",
- "commands.games",
- "commands.general",
- "events.general",
- ]
- for ext in default_extensions:
- logging.info(f"Loading extension {ext}")
- await bot.load_extension(ext)
- async def create_db_pool(): # Connect to database
- try:
- bot.pg = await asyncpg.create_pool(
- database=settings.DATABASE_NAME,
- user=settings.DATABASE_USER,
- host=settings.DATABASE_HOST,
- password=settings.DATABASE_PASSWORD,
- )
- except AttributeError:
- missing_config()
- # Create database pool
- await create_db_pool()
- # Create database tables if they do not exist
- from query.initialise_database import init_db
- await init_db(bot.pg)
- await bot.start(settings.DISCORD_TOKEN)
- # Run robot
- try:
- asyncio.run(main())
- except AttributeError:
- missing_config()
- except discord.errors.LoginFailure:
- logging.error("Invalid discord token.")
- correct_setting("DISCORD_TOKEN")
|