You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.5KB

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