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.

162 lines
4.8KB

  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 traceback
  27. import discord
  28. from discord.ext import commands
  29. import roxbot
  30. from roxbot import guild_settings as gs
  31. # Sets up Logging
  32. logger = logging.getLogger('discord')
  33. logger.setLevel(logging.INFO)
  34. handler = logging.FileHandler(filename='roxbot.log', encoding='utf-8', mode='w')
  35. handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
  36. logger.addHandler(handler)
  37. bot = commands.Bot(
  38. command_prefix=roxbot.command_prefix,
  39. description=roxbot.__description__,
  40. owner_id=roxbot.owner,
  41. activity=discord.Game(name="v{}".format(roxbot.__version__), type=0),
  42. case_insensitive=True
  43. )
  44. @bot.event
  45. async def on_ready():
  46. # Load Roxbots inbuilt cogs and settings
  47. print("Loading Bot internals...")
  48. bot.load_extension("roxbot.base")
  49. print("base.py Loaded")
  50. bot.load_extension("roxbot.err_handle")
  51. print("err_handle.py Loaded")
  52. bot.load_extension("roxbot.logging")
  53. print("logging.py Loaded")
  54. print("")
  55. print("Discord.py version: " + discord.__version__)
  56. print("Client logged in\n")
  57. # Load Extension Cogs
  58. print("Cogs Loaded:")
  59. for cog in roxbot.cogs:
  60. try:
  61. bot.load_extension(cog)
  62. print(cog.split(".")[2])
  63. except ImportError:
  64. print("{} FAILED TO LOAD. MISSING REQUIREMENTS".format(cog.split(".")[2]))
  65. print("")
  66. # this is so if we're added to a server while we're offline we deal with it
  67. roxbot.guild_settings.error_check(bot.guilds, bot.cogs)
  68. print("Guilds I'm currently in:")
  69. for guild in bot.guilds:
  70. print(guild)
  71. print("")
  72. @bot.event
  73. async def on_guild_join(guild):
  74. gs.add_guild(guild, bot.cogs)
  75. @bot.event
  76. async def on_guild_remove(guild):
  77. gs.remove_guild(guild)
  78. @bot.event
  79. async def on_error(event, *args, **kwargs):
  80. if roxbot.dev_mode:
  81. traceback.print_exc()
  82. else:
  83. logging.exception(event)
  84. @bot.event
  85. async def on_command_error(self, ctx, error):
  86. pass # This needs to overwrite the discord.py version and allow roxbot's errpr handling to kick in
  87. @bot.check
  88. def check_blacklist(ctx):
  89. """Adds global check to the bot to check for a user being blacklisted."""
  90. return not roxbot.blacklisted(ctx.author)
  91. @bot.command()
  92. async def about(ctx):
  93. """
  94. Outputs info about RoxBot, showing up time, how to report issues, what settings where set in prefs.ini and credits.
  95. """
  96. owner = bot.get_user(roxbot.owner)
  97. em = discord.Embed(title="About Roxbot", colour=roxbot.EmbedColours.pink, description=roxbot.__description__)
  98. em.set_thumbnail(url=bot.user.avatar_url)
  99. em.add_field(name="Command Prefix", value=roxbot.command_prefix)
  100. em.add_field(name="Owner", value=str(owner))
  101. em.add_field(name="Owner ID", value=roxbot.owner)
  102. em.add_field(name="Bot Version", value=roxbot.__version__)
  103. em.add_field(name="Author", value=roxbot.__author__)
  104. em.add_field(name="Discord.py version", value=discord.__version__)
  105. em.set_footer(text="RoxBot is licensed under the MIT License")
  106. # Do time calc late in the command so that the time returned is closest to when the message is received
  107. uptimeflo = time.time() - start_time
  108. uptime = str(datetime.timedelta(seconds=uptimeflo))
  109. em.add_field(name="Current Uptime", value=str(uptime.split(".")[0]))
  110. return await ctx.channel.send(embed=em)
  111. @commands.command(pass_context=False, hidden=True)
  112. async def settings():
  113. # This is to block any customcommand or command from being made with the same name.
  114. # This is to avoid conflicts with the internal settings system.
  115. raise commands.CommandNotFound()
  116. if __name__ == "__main__":
  117. # Pre-Boot checks
  118. if not os.path.isfile("roxbot/settings/preferences.ini"):
  119. print("PREFERENCE FILE MISSING. Please make sure there is a file called 'preferences.ini' in the settings folder")
  120. exit(0)
  121. start_time = time.time()
  122. bot.run(roxbot.token)