main.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import logging
  2. import discord
  3. from discord.ext import commands
  4. import asyncio
  5. # Attempt to import the local settings and quit gracefully on failure
  6. try:
  7. import local_settings as settings # Environment dependant settings stored in local_settings.py, untracked by .gitinore
  8. except ModuleNotFoundError: # Local settings module import failure
  9. quit()
  10. async def main():
  11. # Set loglevel
  12. logging.basicConfig(level=settings.LOG_LEVEL)
  13. # Define robot
  14. intents = discord.Intents.default()
  15. intents.message_content = True
  16. try:
  17. bot = commands.Bot(
  18. command_prefix="/",
  19. description="Charlie's Angels bot",
  20. intents=intents,
  21. case_insensitive=True,
  22. )
  23. except AttributeError:
  24. quit()
  25. # Load extensions
  26. default_extensions = [
  27. "commands.admin",
  28. "commands.general",
  29. ]
  30. for ext in default_extensions:
  31. logging.info(f"Loading extension: {ext}")
  32. await bot.load_extension(ext)
  33. await bot.start(settings.DISCORD_TOKEN)
  34. # Run robot
  35. try:
  36. asyncio.run(main())
  37. except AttributeError:
  38. quit()
  39. except discord.errors.LoginFailure:
  40. logging.error("Invalid discord token.")
  41. correct_setting("DISCORD_TOKEN")
  42. quit()
  43. except KeyboardInterrupt:
  44. logging.info("Received keyboard interrupt, exiting...")
  45. quit()