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.

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