Experimental Discord bot written in Python
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.

bot.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. Rocketbot Discord bot. Relies on a configured config.py (copy config.py.sample
  3. for a template).
  4. Author: Ian Albert (@rocketsoup)
  5. Date: 2021-11-11
  6. """
  7. import traceback
  8. from discord import Intents
  9. from discord.ext import commands
  10. from config import CONFIG
  11. from rocketbot.cogs.configcog import ConfigCog
  12. from rocketbot.cogs.crosspostcog import CrossPostCog
  13. from rocketbot.cogs.generalcog import GeneralCog
  14. from rocketbot.cogs.joinagecog import JoinAgeCog
  15. from rocketbot.cogs.joinraidcog import JoinRaidCog
  16. from rocketbot.cogs.patterncog import PatternCog
  17. from rocketbot.cogs.urlspamcog import URLSpamCog
  18. from rocketbot.cogs.usernamecog import UsernamePatternCog
  19. CURRENT_CONFIG_VERSION = 3
  20. if (CONFIG.get('__config_version') or 0) < CURRENT_CONFIG_VERSION:
  21. # If you're getting this error, it means something changed in config.py's
  22. # format. Consult config.py.sample and compare it to your own config.py.
  23. # Rename/move any values as needed. When satisfied, update "__config_version"
  24. # to the value in config.py.sample.
  25. raise RuntimeError('config.py format may be outdated. Review ' +
  26. 'config.py.sample, update the "__config_version" field to ' +
  27. f'{CURRENT_CONFIG_VERSION}, and try again.')
  28. class Rocketbot(commands.Bot):
  29. """
  30. Bot subclass
  31. """
  32. def __init__(self, command_prefix, **kwargs):
  33. super().__init__(command_prefix, **kwargs)
  34. async def on_command_error(self, context: commands.Context, exception):
  35. if context.guild is None or \
  36. context.message.channel is None or \
  37. context.message.author.bot:
  38. return
  39. if not context.message.author.permissions_in(context.message.channel).ban_members:
  40. # Don't tell non-mods about errors
  41. return
  42. if isinstance(exception, (commands.errors.CommandError, )):
  43. # Reply with the error message for ordinary command errors
  44. await context.message.reply(
  45. f'{CONFIG["failure_emoji"]} {exception}',
  46. mention_author=False)
  47. return
  48. # Stack trace everything else
  49. traceback.print_exception(type(exception), exception, exception.__traceback__)
  50. intents = Intents.default()
  51. intents.messages = True # pylint: disable=assigning-non-slot
  52. intents.members = True # pylint: disable=assigning-non-slot
  53. bot = Rocketbot(command_prefix=CONFIG['command_prefix'], intents=intents)
  54. # Core
  55. bot.add_cog(GeneralCog(bot))
  56. bot.add_cog(ConfigCog(bot))
  57. # Optional
  58. bot.add_cog(CrossPostCog(bot))
  59. bot.add_cog(JoinAgeCog(bot))
  60. bot.add_cog(JoinRaidCog(bot))
  61. bot.add_cog(PatternCog(bot))
  62. bot.add_cog(URLSpamCog(bot))
  63. bot.add_cog(UsernamePatternCog(bot))
  64. bot.run(CONFIG['client_token'], bot=True, reconnect=True)
  65. print('\nBot aborted')