1
0

main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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. # Hint to correct specific setting and quit
  41. def correct_setting(setting):
  42. logging.info("Correct the %s in local_settings.py", setting)
  43. hint_quit()
  44. # Import settings
  45. try:
  46. import local_settings as settings # Environment dependant settings stored in local_settings.py, untracked by .gitinore
  47. except ModuleNotFoundError:
  48. missing_config()
  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 database pool
  62. import asyncpg
  63. async def create_db_pool():
  64. try:
  65. bot.pg = await asyncpg.create_pool(
  66. database=settings.DATABASE_NAME,
  67. user=settings.DATABASE_USER,
  68. host=settings.DATABASE_HOST,
  69. password=settings.DATABASE_PASSWORD,
  70. )
  71. except AttributeError:
  72. missing_config()
  73. # Create robot
  74. import discord
  75. from discord.ext import commands
  76. try:
  77. bot = commands.Bot(
  78. command_prefix = settings.COMMAND_PREFIX,
  79. description = "Charlie's Angels bot",
  80. intents = discord.Intents.default(), # Required: Guilds
  81. case_insensitive = True,
  82. )
  83. except AttributeError:
  84. print("Attribute error on create.")
  85. missing_config()
  86. # Create database pool
  87. bot.loop.run_until_complete(create_db_pool())
  88. # Create database tables if they do not exist
  89. from query.initialise_database import init_db
  90. bot.loop.run_until_complete(init_db(bot.pg))
  91. # Load extensions
  92. default_extensions = [
  93. "commands.admin",
  94. "commands.games",
  95. "commands.general",
  96. "events.general",
  97. ]
  98. for ext in default_extensions:
  99. bot.load_extension(ext)
  100. # Run robot
  101. try:
  102. bot.run(settings.DISCORD_TOKEN)
  103. except AttributeError:
  104. print("Attribute error on run.")
  105. missing_config()
  106. except discord.errors.LoginFailure:
  107. logging.error("Invalid discord token.")
  108. correct_setting("DISCORD_TOKEN")