Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

114 lines
3.4KB

  1. #!/usr/env python3
  2. import time
  3. import logging
  4. import os.path
  5. import datetime
  6. import discord
  7. from discord.ext import commands
  8. from Roxbot import load_config
  9. from Roxbot.settings import guild_settings as gs
  10. # Sets up Logging that discord.py does on its own
  11. logger = logging.getLogger('discord')
  12. logger.setLevel(logging.INFO)
  13. handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
  14. handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
  15. logger.addHandler(handler)
  16. bot = commands.Bot(
  17. command_prefix=load_config.command_prefix,
  18. description=load_config.__description__,
  19. owner_id=load_config.owner,
  20. activity=discord.Game(name="v{}".format(load_config.__version__), type=0)
  21. )
  22. def blacklisted(user):
  23. with open("Roxbot/blacklist.txt", "r") as fp:
  24. for line in fp.readlines():
  25. if user.id+"\n" == line:
  26. return True
  27. return False
  28. @bot.event
  29. async def on_ready():
  30. bot.settings = gs.get_all(bot.guilds)
  31. print("Discord.py version: " + discord.__version__)
  32. print("Client logged in\n")
  33. print("Cogs Loaded:")
  34. for cog in load_config.cogs:
  35. bot.load_extension(cog)
  36. print(cog.split(".")[2])
  37. print("")
  38. print("Servers I am currently in:")
  39. for server in bot.guilds:
  40. print(server)
  41. print("")
  42. # In the next two functions, I was gunna user bot.settings for something but I don't think it's possible.
  43. # So while I don't use it, the function still will do their jobs of adding and removing the settings.
  44. @bot.event
  45. async def on_guild_join(guild):
  46. gs.add_guild(guild)
  47. @bot.event
  48. async def on_guild_remove(guild):
  49. gs.remove_guild(guild)
  50. @bot.event
  51. async def on_message(message):
  52. """
  53. Checks if the user is blacklisted, if not, process the message for commands as usual.
  54. :param message:
  55. :return:
  56. """
  57. if blacklisted(message.author):
  58. return
  59. return await bot.process_commands(message)
  60. @bot.command()
  61. async def about(ctx):
  62. """
  63. Outputs info about RoxBot, showing uptime, how to report issues, what settings where set in prefs.ini and credits.
  64. """
  65. owner = bot.get_user(load_config.owner)
  66. em = discord.Embed(title="About Roxbot", colour=load_config.embedcolour, description=load_config.__description__)
  67. em.set_thumbnail(url=bot.user.avatar_url)
  68. em.add_field(name="Command Prefix", value=load_config.command_prefix)
  69. em.add_field(name="Owner", value=str(owner))
  70. em.add_field(name="Owner ID", value=load_config.owner)
  71. em.add_field(name="Bot Version", value=load_config.__version__)
  72. em.add_field(name="Author", value=load_config.__author__)
  73. em.add_field(name="Discord.py version", value=discord.__version__)
  74. em.set_footer(text="RoxBot is licensed under the MIT License")
  75. # Do time calc late in the command so that the time returned is closest to when the message is received
  76. uptimeflo = time.time() - start_time
  77. uptime = str(datetime.timedelta(seconds=uptimeflo))
  78. em.add_field(name="Current Uptime", value=str(uptime.split(".")[0]))
  79. return await ctx.channel.send(embed=em)
  80. if __name__ == "__main__":
  81. # Pre-Boot checks
  82. if not os.path.isfile("Roxbot/preferences.ini"):
  83. print(
  84. "PREFERENCE FILE MISSING. Something has gone wrong. Please make sure there is a file called 'preferences.ini' in the settings folder")
  85. exit(0)
  86. if not os.path.isfile("Roxbot/settings/servers.json"):
  87. with open("Roxbot/settings/servers.json", "w") as fp:
  88. fp.write("{}")
  89. start_time = time.time()
  90. bot.load_extension("Roxbot.settings.settings")
  91. bot.load_extension("Roxbot.err_handle")
  92. bot.load_extension("Roxbot.logging")
  93. bot.run(load_config.token)