1
0

main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import logging, os
  2. import discord
  3. from discord.ext import commands
  4. import asyncpg, asyncio
  5. def hint_quit(): # Hint how to edit the settings and quit
  6. logging.info("")
  7. logging.info(" edit local_settings.py")
  8. logging.info("")
  9. quit()
  10. def missing_config(): # Copy or create settings file if missing
  11. logging.basicConfig(level=logging.DEBUG)
  12. if not os.path.exists("local_settings.py"):
  13. logging.error("Settings file not found.")
  14. logging.info("Copying local_settings_example.py to local_settings.py")
  15. try:
  16. os.system("cp local_settings_example.py local_settings.py")
  17. except FileNotFoundError:
  18. logging.info("local_settings_example.py not found, creating local_settings.py")
  19. with open("local_settings.py", "w") as settings_file:
  20. settings_file.writelines(
  21. [
  22. "import logging",
  23. "LOG_LEVEL = logging.INFO # Options: CRITICAL, ERROR, WARNING, INFO, and DEBUG",
  24. "",
  25. "DATABASE_NAME = \"\"",
  26. "DATABASE_USER = \"\"",
  27. "DATABASE_HOST = \"\"",
  28. "DATABASE_PASSWORD = \"\"",
  29. "",
  30. "WEB_HOST = \"\"",
  31. "WEB_SCHEME = \"\"",
  32. "",
  33. "DISCORD_TOKEN = \"\"",
  34. "COMMAND_PREFIX = \"\"",
  35. ]
  36. )
  37. logging.error("Settings undefined.")
  38. logging.info("Configure the settings:")
  39. hint_quit()
  40. def correct_setting(setting): # Hint to correct specific setting and quit
  41. logging.info("Correct the %s in local_settings.py", setting)
  42. hint_quit()
  43. # Attempt to import the local settings and quit gracefully on failure
  44. try:
  45. import local_settings as settings # Environment dependant settings stored in local_settings.py, untracked by .gitinore
  46. except ModuleNotFoundError: # Local settings module import failure
  47. missing_config() # Prepare for configuration and inform operator
  48. async def main():
  49. # Check additional settings
  50. if not settings.WEB_HOST:
  51. logging.error("Web host undefinded.")
  52. correct_setting("WEB_HOST")
  53. if not settings.WEB_SCHEME:
  54. logging.error("Web scheme undefinded.")
  55. correct_setting("WEB_SCHEME")
  56. # Set loglevel
  57. try:
  58. logging.basicConfig(level=settings.LOG_LEVEL)
  59. except AttributeError:
  60. missing_config()
  61. # Define robot
  62. intents = discord.Intents.default()
  63. intents.message_content = True
  64. try:
  65. bot = commands.Bot(
  66. command_prefix=settings.COMMAND_PREFIX,
  67. description="Charlie's Angels bot",
  68. intents=intents,
  69. case_insensitive=True,
  70. )
  71. except AttributeError:
  72. missing_config()
  73. # Load extensions
  74. default_extensions = [
  75. "commands.admin",
  76. "commands.games",
  77. "commands.general",
  78. "events.general",
  79. ]
  80. for ext in default_extensions:
  81. logging.info(f"Loading extension: {ext}")
  82. await bot.load_extension(ext)
  83. async def create_db_pool(): # Connect to database
  84. try:
  85. bot.pg = await asyncpg.create_pool(
  86. database=settings.DATABASE_NAME,
  87. user=settings.DATABASE_USER,
  88. host=settings.DATABASE_HOST,
  89. password=settings.DATABASE_PASSWORD,
  90. )
  91. except AttributeError:
  92. missing_config()
  93. # Create database pool
  94. await create_db_pool()
  95. # Create database tables if they do not exist
  96. from query.initialise_database import init_db
  97. await init_db(bot.pg)
  98. await bot.start(settings.DISCORD_TOKEN)
  99. # Run robot
  100. try:
  101. asyncio.run(main())
  102. except AttributeError:
  103. missing_config()
  104. except discord.errors.LoginFailure:
  105. logging.error("Invalid discord token.")
  106. correct_setting("DISCORD_TOKEN")