spacepaste

  1.  
  2. # disbot.py
  3. import yaml
  4. import discord
  5. from discord.ext import commands
  6. # Create empty config variable. Will be filled later.
  7. config = ""
  8. try:
  9. config_file = open("config.yml")
  10. except IOError:
  11. print("File config.yml not found. Please create one in an empty directory. Documentation here: https://gitlab.nextgamers.eu/weStart/DisBot/wikis/config.yml")
  12. exit(0)
  13. # Load config_file into variable config
  14. config = yaml.load(config_file)
  15. # Close config file, content is in variable config.
  16. config_file.close()
  17. print("Config was found and loaded. Good job :).")
  18. if(config['token'] == "TOKEN_HERE"):
  19. print("Please set a proper Discord token.")
  20. exit(1)
  21. print("Now starting the bot with configured token.")
  22. # Initialize the bot with prefix and a description from the config.
  23. bot = commands.Bot(command_prefix=config['prefix'], description=config['desc'])
  24. @bot.event
  25. async def on_ready():
  26. """"When the bot is successfully connected to Discord."""
  27. print(f"Logged in as: {bot.user.name}")
  28. print(f"UserID: {bot.user.id}")
  29. await bot.change_presence(game=discord.Game(name=config['game']))
  30. @bot.command()
  31. async def load(extension_name: str):
  32. """Loads an extension."""
  33. try:
  34. bot.load_extension(extension_name)
  35. except (AttributeError, ImportError) as e:
  36. await bot.say("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
  37. return
  38. await bot.say("{} loaded.".format(extension_name))
  39. @bot.command()
  40. async def unload(extension_name: str):
  41. """Unloads an extension."""
  42. bot.unload_extension(extension_name)
  43. await(bot.say("{} unloaded.".format(extension_name)))
  44. @bot.command()
  45. async def test():
  46. """Just replies - testing the bot."""
  47. await bot.say("confirmed.")
  48. bot.run(config['token'])
  49.