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.

149 lines
4.5KB

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