Experimental Discord bot written in Python
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

bot.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. intents.presences = True
  55. bot = Rocketbot(command_prefix=CONFIG['command_prefix'], intents=intents)
  56. # Core
  57. bot.add_cog(GeneralCog(bot))
  58. bot.add_cog(ConfigCog(bot))
  59. # Optional
  60. bot.add_cog(AutoKickCog(bot))
  61. bot.add_cog(CrossPostCog(bot))
  62. bot.add_cog(JoinAgeCog(bot))
  63. bot.add_cog(JoinRaidCog(bot))
  64. bot.add_cog(PatternCog(bot))
  65. bot.add_cog(URLSpamCog(bot))
  66. bot.add_cog(UsernamePatternCog(bot))
  67. bot.run(CONFIG['client_token'], bot=True, reconnect=True)
  68. print('\nBot aborted')