Experimental Discord bot written in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

bot.py 2.4KB

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