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.

141 lines
4.4KB

  1. #!/usr/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. MIT License
  5. Copyright (c) 2017-2018 Roxanne Gibson
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. """
  22. import time
  23. import logging
  24. import os.path
  25. import datetime
  26. import discord
  27. from discord.ext import commands
  28. import roxbot
  29. from roxbot import guild_settings as gs
  30. # Sets up Logging that discord.py does on its own
  31. logger = logging.getLogger('discord')
  32. logger.setLevel(logging.INFO)
  33. handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
  34. handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
  35. logger.addHandler(handler)
  36. bot = commands.Bot(
  37. command_prefix=roxbot.command_prefix,
  38. description=roxbot.__description__,
  39. owner_id=roxbot.owner,
  40. activity=discord.Game(name="v{}".format(roxbot.__version__), type=0),
  41. case_insensitive=True
  42. )
  43. @bot.event
  44. async def on_ready():
  45. # Load Roxbots inbuilt cogs and settings
  46. bot.load_extension("roxbot.settings.settings")
  47. bot.load_extension("roxbot.err_handle")
  48. bot.load_extension("roxbot.logging")
  49. bot.load_extension("roxbot.system")
  50. bot.settings = gs.get_all(bot.guilds)
  51. print("Discord.py version: " + discord.__version__)
  52. print("Client logged in\n")
  53. # Load Extension Cogs
  54. print("Cogs Loaded:")
  55. for cog in roxbot.cogs:
  56. bot.load_extension(cog)
  57. print(cog.split(".")[2])
  58. print("")
  59. print("Servers I am currently in:")
  60. for server in bot.guilds:
  61. print(server)
  62. print("")
  63. # In the next two functions, I was gunna user bot.settings for something but I don't think it's possible.
  64. # So while I don't use it, the function still will do their jobs of adding and removing the settings.
  65. @bot.event
  66. async def on_guild_join(guild):
  67. gs.add_guild(guild)
  68. @bot.event
  69. async def on_guild_remove(guild):
  70. gs.remove_guild(guild)
  71. @bot.event
  72. async def on_message(message):
  73. """
  74. Checks if the user is blacklisted, if not, process the message for commands as usual.
  75. :param message:
  76. :return:
  77. """
  78. if roxbot.blacklisted(message.author):
  79. return
  80. return await bot.process_commands(message)
  81. @bot.command()
  82. async def about(ctx):
  83. """
  84. Outputs info about RoxBot, showing uptime, how to report issues, what settings where set in prefs.ini and credits.
  85. """
  86. owner = bot.get_user(roxbot.owner)
  87. em = discord.Embed(title="About Roxbot", colour=roxbot.EmbedColours.pink, description=roxbot.__description__)
  88. em.set_thumbnail(url=bot.user.avatar_url)
  89. em.add_field(name="Command Prefix", value=roxbot.command_prefix)
  90. em.add_field(name="Owner", value=str(owner))
  91. em.add_field(name="Owner ID", value=roxbot.owner)
  92. em.add_field(name="Bot Version", value=roxbot.__version__)
  93. em.add_field(name="Author", value=roxbot.__author__)
  94. em.add_field(name="Discord.py version", value=discord.__version__)
  95. em.set_footer(text="RoxBot is licensed under the MIT License")
  96. # Do time calc late in the command so that the time returned is closest to when the message is received
  97. uptimeflo = time.time() - start_time
  98. uptime = str(datetime.timedelta(seconds=uptimeflo))
  99. em.add_field(name="Current Uptime", value=str(uptime.split(".")[0]))
  100. return await ctx.channel.send(embed=em)
  101. if __name__ == "__main__":
  102. # Pre-Boot checks
  103. if not os.path.isfile("roxbot/settings/preferences.ini"):
  104. print(
  105. "PREFERENCE FILE MISSING. Something has gone wrong. Please make sure there is a file called 'preferences.ini' in the settings folder")
  106. exit(0)
  107. if not os.path.isfile("roxbot/settings/servers.json"):
  108. with open("roxbot/settings/servers.json", "w") as fp:
  109. fp.write("{}")
  110. start_time = time.time()
  111. bot.run(roxbot.token)