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

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