Experimental Discord bot written in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

bot.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import traceback
  2. from typing import Optional
  3. from discord import Intents
  4. from discord.ext import commands
  5. from config import CONFIG
  6. from rocketbot.utils import bot_log
  7. class Rocketbot(commands.Bot):
  8. """
  9. Bot subclass
  10. """
  11. def __init__(self, command_prefix, **kwargs):
  12. super().__init__(command_prefix, **kwargs)
  13. async def on_command_error(self, context: commands.Context, exception: BaseException) -> None:
  14. bot_log(None, None, f'Command error')
  15. ex = exception
  16. while ex is not None:
  17. print(f'Caused by {ex}')
  18. traceback.print_exception(type(ex), ex, ex.__traceback__)
  19. ex = ex.__cause__ if ex.__cause__ != ex else None
  20. if context.guild is None or \
  21. context.message.channel is None or \
  22. context.message.author.bot:
  23. return
  24. if not context.message.channel.permissions_for(context.message.author).ban_members:
  25. # Don't tell non-mods about errors
  26. return
  27. if isinstance(exception, (commands.errors.CommandError, )):
  28. # Reply with the error message for ordinary command errors
  29. await context.message.reply(
  30. f'{CONFIG["failure_emoji"]} {exception}',
  31. mention_author=False)
  32. return
  33. # Stack trace everything else
  34. async def on_error(self, event_method, args=None, kwargs=None):
  35. bot_log(None, None, 'Event caused error\n' + \
  36. f' event method: {event_method}' + \
  37. f' event args: {args}' + \
  38. f' event kwargs: {kwargs}' + \
  39. traceback.format_exc())
  40. # Current active bot instance
  41. rocketbot: Optional[Rocketbot] = None
  42. def __create_bot():
  43. global rocketbot
  44. if rocketbot is not None:
  45. return
  46. bot_log(None, None, 'Creating bot...')
  47. intents = Intents.default()
  48. intents.messages = True # pylint: disable=assigning-non-slot
  49. intents.message_content = True # pylint: disable=assigning-non-slot
  50. intents.members = True # pylint: disable=assigning-non-slot
  51. intents.presences = True
  52. rocketbot = Rocketbot(command_prefix=CONFIG['command_prefix'], intents=intents)
  53. __create_bot()
  54. from rocketbot.cogs.autokickcog import AutoKickCog
  55. from rocketbot.cogs.configcog import ConfigCog
  56. from rocketbot.cogs.crosspostcog import CrossPostCog
  57. from rocketbot.cogs.generalcog import GeneralCog
  58. from rocketbot.cogs.joinagecog import JoinAgeCog
  59. from rocketbot.cogs.joinraidcog import JoinRaidCog
  60. from rocketbot.cogs.logcog import LoggingCog
  61. from rocketbot.cogs.patterncog import PatternCog
  62. from rocketbot.cogs.urlspamcog import URLSpamCog
  63. from rocketbot.cogs.usernamecog import UsernamePatternCog
  64. async def start_bot():
  65. bot_log(None, None, 'Bot initializing...')
  66. bot_log(None, None, f"Type {CONFIG['command_prefix']}help in Discord for available commands.")
  67. # Core
  68. await rocketbot.add_cog(GeneralCog(rocketbot))
  69. await rocketbot.add_cog(ConfigCog(rocketbot))
  70. # Optional
  71. await rocketbot.add_cog(AutoKickCog(rocketbot))
  72. await rocketbot.add_cog(CrossPostCog(rocketbot))
  73. await rocketbot.add_cog(JoinAgeCog(rocketbot))
  74. await rocketbot.add_cog(JoinRaidCog(rocketbot))
  75. await rocketbot.add_cog(LoggingCog(rocketbot))
  76. await rocketbot.add_cog(PatternCog(rocketbot))
  77. await rocketbot.add_cog(URLSpamCog(rocketbot))
  78. await rocketbot.add_cog(UsernamePatternCog(rocketbot))
  79. await rocketbot.start(CONFIG['client_token'], reconnect=True)
  80. print('\nBot aborted')