Experimental Discord bot written in Python
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

bot.py 3.3KB

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