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.

114 lines
3.3KB

  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. import Roxbot
  9. from Roxbot 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=Roxbot.command_prefix,
  18. description=Roxbot.__description__,
  19. owner_id=Roxbot.owner,
  20. activity=discord.Game(name="v{}".format(Roxbot.__version__), type=0),
  21. case_insensitive=True
  22. )
  23. @bot.event
  24. async def on_ready():
  25. # Load Roxbots inbuilt cogs and settings
  26. bot.load_extension("Roxbot.settings.settings")
  27. bot.load_extension("Roxbot.err_handle")
  28. bot.load_extension("Roxbot.logging")
  29. bot.settings = gs.get_all(bot.guilds)
  30. print("Discord.py version: " + discord.__version__)
  31. print("Client logged in\n")
  32. # Load Extension Cogs
  33. print("Cogs Loaded:")
  34. for cog in Roxbot.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 Roxbot.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(Roxbot.owner)
  66. em = discord.Embed(title="About Roxbot", colour=Roxbot.EmbedColours.pink, description=Roxbot.__description__)
  67. em.set_thumbnail(url=bot.user.avatar_url)
  68. em.add_field(name="Command Prefix", value=Roxbot.command_prefix)
  69. em.add_field(name="Owner", value=str(owner))
  70. em.add_field(name="Owner ID", value=Roxbot.owner)
  71. em.add_field(name="Bot Version", value=Roxbot.__version__)
  72. em.add_field(name="Author", value=Roxbot.__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/settings/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.run(Roxbot.token)