main.py 3.1 KB

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