1
0

main.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from dotenv import load_dotenv
  2. import os, logging, asyncpg, socket
  3. import discord
  4. def config_missing():
  5. if not os.path.exists(".env"):
  6. logging.error("Environment variable file not found, creating .env file")
  7. with open(".env", "w") as settings_file:
  8. settings_file.writelines(
  9. [
  10. "# LOG_LEVEL options: DEBUG, INFO, WARNING, ERROR, CRITICAL\n",
  11. "LOG_LEVEL = \"WARNING\"\n",
  12. "\n",
  13. "DISCORD_TOKEN = \"\"\n",
  14. "OUTPUT_CHANNEL_ID = \n",
  15. "\n",
  16. "DATABASE_HOST = 127.0.0.1\n",
  17. "DATABASE_NAME = \n",
  18. "DATABASE_USER = \n",
  19. "DATABASE_PASSWORD = \n"
  20. ]
  21. )
  22. logging.critical("Configure the settings by editing the .env file and restart the bot to continue.")
  23. quit()
  24. async def create_db_pool(): # Connect to database
  25. try:
  26. bot.pg = await asyncpg.create_pool(
  27. database=str(os.getenv("DATABASE_NAME")),
  28. user=str(os.getenv("DATABASE_USER")),
  29. host=str(os.getenv("DATABASE_HOST")),
  30. password=str(os.getenv("DATABASE_PASSWORD")),
  31. )
  32. except socket.gaierror:
  33. logging.error("Unable to connect to database - GAI error: PLease verify the DATABASE_HOST.")
  34. config_missing()
  35. except asyncpg.exceptions.InvalidPasswordError:
  36. logging.error("Unable to connect to database - Invalid password: Please verify credentials.")
  37. config_missing()
  38. except asyncpg.exceptions.InvalidCatalogNameError:
  39. logging.error("Unable to connect to database - Invalid catalog name: Please verify the database name.")
  40. config_missing()
  41. await init_db(bot.pg)
  42. # Load variables from .env file
  43. load_dotenv()
  44. # Set loglevel
  45. try:
  46. logging.basicConfig(level=str(os.getenv("LOG_LEVEL").upper()))
  47. except AttributeError:
  48. config_missing()
  49. # Set intent
  50. intents = discord.Intents.none()
  51. intents.bans = True
  52. intents.guilds = True
  53. intents.invites = True
  54. intents.members = True
  55. intents.messages = True
  56. bot = discord.Bot(intents=intents)
  57. bot.OUTPUT_CHANNEL_ID = int(os.getenv("OUTPUT_CHANNEL_ID"))
  58. if not bot.OUTPUT_CHANNEL_ID:
  59. logging.error("OUTPUT_CHANNEL not defined, the bot requires a guild channel to report to.")
  60. config_missing()
  61. # Create database pool
  62. bot.loop.create_task(create_db_pool())
  63. # Create database tables if they do not exist
  64. from query.initialise_database import init_db
  65. # Load cogs
  66. cogs_list = [
  67. 'events',
  68. 'idlerpg',
  69. 'admin'
  70. ]
  71. for cog in cogs_list:
  72. bot.load_extension(f'cogs.{cog}')
  73. # Run bot
  74. try:
  75. bot.run(str(os.getenv("DISCORD_TOKEN")))
  76. except discord.errors.LoginFailure:
  77. logging.error("Login Failure")
  78. config_missing()