Experimental Discord bot written in Python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

bot.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.cogsetting import CogSetting
  7. from rocketbot.utils import bot_log, dump_stacktrace
  8. class Rocketbot(commands.Bot):
  9. """
  10. Bot subclass
  11. """
  12. def __init__(self, command_prefix, **kwargs):
  13. super().__init__(command_prefix, **kwargs)
  14. self.__commands_set_up = False
  15. async def on_command_error(self, context: commands.Context, exception: BaseException) -> None:
  16. bot_log(None, None, f'Command error')
  17. dump_stacktrace(exception)
  18. if context.guild is None or \
  19. context.message.channel is None or \
  20. context.message.author.bot:
  21. return
  22. if not context.message.channel.permissions_for(context.message.author).ban_members:
  23. # Don't tell non-mods about errors
  24. return
  25. if isinstance(exception, (commands.errors.CommandError, )):
  26. # Reply with the error message for ordinary command errors
  27. await context.message.reply(
  28. f'{CONFIG["failure_emoji"]} {exception}',
  29. mention_author=False)
  30. return
  31. # Stack trace everything else
  32. async def on_error(self, event_method, args=None, kwargs=None):
  33. bot_log(None, None, 'Event caused error\n' + \
  34. f' event method: {event_method}' + \
  35. f' event args: {args}' + \
  36. f' event kwargs: {kwargs}' + \
  37. traceback.format_exc())
  38. async def on_ready(self):
  39. if not self.__commands_set_up:
  40. await self.__set_up_commands()
  41. bot_log(None, None, 'Bot done initializing')
  42. print('----------------------------------------------------------')
  43. async def __set_up_commands(self):
  44. if self.__commands_set_up:
  45. return
  46. self.__commands_set_up = True
  47. for cog in self.cogs.values():
  48. from rocketbot.cogs.basecog import BaseCog
  49. if isinstance(cog, BaseCog):
  50. bcog: BaseCog = cog
  51. if len(bcog.settings) > 0:
  52. CogSetting.set_up_all(bcog, self, bcog.settings)
  53. try:
  54. synced_commands = await self.tree.sync()
  55. for command in synced_commands:
  56. bot_log(None, None, f'Synced command: /{command.name}')
  57. except Exception as e:
  58. dump_stacktrace(e)
  59. # Current active bot instance
  60. rocketbot: Optional[Rocketbot] = None
  61. def __create_bot():
  62. global rocketbot
  63. if rocketbot is not None:
  64. return
  65. bot_log(None, None, 'Creating bot...')
  66. intents = Intents.default()
  67. intents.messages = True # pylint: disable=assigning-non-slot
  68. intents.message_content = True # pylint: disable=assigning-non-slot
  69. intents.members = True # pylint: disable=assigning-non-slot
  70. intents.presences = True
  71. rocketbot = Rocketbot(command_prefix=CONFIG['command_prefix'], intents=intents)
  72. __create_bot()
  73. from rocketbot.cogs.autokickcog import AutoKickCog
  74. from rocketbot.cogs.configcog import ConfigCog
  75. from rocketbot.cogs.crosspostcog import CrossPostCog
  76. from rocketbot.cogs.generalcog import GeneralCog
  77. from rocketbot.cogs.helpcog import HelpCog
  78. from rocketbot.cogs.joinraidcog import JoinRaidCog
  79. from rocketbot.cogs.logcog import LoggingCog
  80. from rocketbot.cogs.patterncog import PatternCog
  81. from rocketbot.cogs.urlspamcog import URLSpamCog
  82. from rocketbot.cogs.usernamecog import UsernamePatternCog
  83. async def start_bot():
  84. bot_log(None, None, 'Bot initializing...')
  85. bot_log(None, None, f"Type {CONFIG['command_prefix']}help in Discord for available commands.")
  86. # Core
  87. await rocketbot.add_cog(GeneralCog(rocketbot))
  88. await rocketbot.add_cog(ConfigCog(rocketbot))
  89. # Optional
  90. await rocketbot.add_cog(AutoKickCog(rocketbot))
  91. await rocketbot.add_cog(CrossPostCog(rocketbot))
  92. await rocketbot.add_cog(HelpCog(rocketbot))
  93. await rocketbot.add_cog(JoinRaidCog(rocketbot))
  94. await rocketbot.add_cog(LoggingCog(rocketbot))
  95. await rocketbot.add_cog(PatternCog(rocketbot))
  96. await rocketbot.add_cog(URLSpamCog(rocketbot))
  97. await rocketbot.add_cog(UsernamePatternCog(rocketbot))
  98. await rocketbot.start(CONFIG['client_token'], reconnect=True)
  99. print('\nBot aborted')