Experimental Discord bot written in Python
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

bot.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import traceback
  2. from discord import Intents
  3. from discord.errors import DiscordException
  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, **kwargs):
  13. # Bot requires command_prefix, even though we're only using slash commands,
  14. # not commands in message content. Giving an unlikely prefix of private-use
  15. # Unicode characters. This isn't a security thing, just avoiding showing a
  16. # "command not found" error if someone happens to start a message with
  17. # something more common.
  18. super().__init__(command_prefix='\uED1E\uEA75\uF00D', **kwargs)
  19. self.__commands_set_up = False
  20. self.__first_ready = True
  21. async def on_command_error(self, context: commands.Context, exception: BaseException) -> None:
  22. bot_log(None, None, '\u0007Command error')
  23. dump_stacktrace(exception)
  24. if context.guild is None or \
  25. context.message.channel is None or \
  26. context.message.author.bot:
  27. return
  28. if not context.message.channel.permissions_for(context.message.author).ban_members:
  29. # Don't tell non-mods about errors
  30. return
  31. if isinstance(exception, (commands.errors.CommandError, )):
  32. # Reply with the error message for ordinary command errors
  33. await context.message.reply(
  34. f'{CONFIG["failure_emoji"]} {exception}',
  35. mention_author=False)
  36. return
  37. # Stack trace everything else
  38. async def on_error(self, event_method, args=None, kwargs=None):
  39. bot_log(None, None, '\u0007Event caused error\n' + \
  40. f' event method: {event_method}' + \
  41. f' event args: {args}' + \
  42. f' event kwargs: {kwargs}' + \
  43. traceback.format_exc())
  44. async def on_ready(self):
  45. if self.__first_ready:
  46. self.__first_ready = False
  47. if not self.__commands_set_up:
  48. await self.__set_up_commands()
  49. bot_log(None, None, 'Bot done initializing')
  50. bot_log(None, None, "Type /help in Discord for available commands.")
  51. print('----------------------------------------------------------')
  52. async def __set_up_commands(self):
  53. if self.__commands_set_up:
  54. return
  55. self.__commands_set_up = True
  56. for cog in self.cogs.values():
  57. from rocketbot.cogs.basecog import BaseCog
  58. if isinstance(cog, BaseCog):
  59. bcog: BaseCog = cog
  60. if len(bcog.settings) > 0:
  61. CogSetting.set_up_all(bcog, self, bcog.settings)
  62. try:
  63. synced_commands = await self.tree.sync()
  64. for command in sorted(synced_commands, key=lambda c: c.name):
  65. bot_log(None, None, f'Synced command: /{command.name}')
  66. except DiscordException as e:
  67. dump_stacktrace(e)
  68. # Current active bot instance
  69. rocketbot: Rocketbot | None = None
  70. def __create_bot():
  71. global rocketbot
  72. if rocketbot is not None:
  73. return
  74. bot_log(None, None, 'Creating bot...')
  75. intents = Intents.default()
  76. # Set privileged intents. Must agree with configuration in Discord app.
  77. intents.message_content = CONFIG.get('has_message_content_intent', False)
  78. intents.members = CONFIG.get('has_members_intent', False)
  79. intents.presences = False
  80. rocketbot = Rocketbot(intents=intents)
  81. __create_bot()
  82. async def start_bot():
  83. from rocketbot.cogs.autokickcog import AutoKickCog
  84. from rocketbot.cogs.bangcommandcog import BangCommandCog
  85. from rocketbot.cogs.configcog import ConfigCog
  86. from rocketbot.cogs.crosspostcog import CrossPostCog
  87. from rocketbot.cogs.gamescog import GamesCog
  88. from rocketbot.cogs.generalcog import GeneralCog
  89. from rocketbot.cogs.helpcog import HelpCog
  90. from rocketbot.cogs.joinraidcog import JoinRaidCog
  91. from rocketbot.cogs.kickstartercog import KickstarterCog
  92. from rocketbot.cogs.logcog import LoggingCog
  93. from rocketbot.cogs.patterncog import PatternCog
  94. from rocketbot.cogs.urlspamcog import URLSpamCog
  95. from rocketbot.cogs.usernamecog import UsernamePatternCog
  96. from rocketbot.cogs.videopreviewcog import VideoPreviewCog
  97. bot_log(None, None, 'Bot initializing...')
  98. # Core
  99. await rocketbot.add_cog(GeneralCog(rocketbot))
  100. await rocketbot.add_cog(ConfigCog(rocketbot))
  101. # Optional
  102. if AutoKickCog.supports_intents(rocketbot.intents):
  103. await rocketbot.add_cog(AutoKickCog(rocketbot))
  104. if BangCommandCog.supports_intents(rocketbot.intents):
  105. await rocketbot.add_cog(BangCommandCog(rocketbot))
  106. if CrossPostCog.supports_intents(rocketbot.intents):
  107. await rocketbot.add_cog(CrossPostCog(rocketbot))
  108. if GamesCog.supports_intents(rocketbot.intents):
  109. await rocketbot.add_cog(GamesCog(rocketbot))
  110. if HelpCog.supports_intents(rocketbot.intents):
  111. await rocketbot.add_cog(HelpCog(rocketbot))
  112. if JoinRaidCog.supports_intents(rocketbot.intents):
  113. await rocketbot.add_cog(JoinRaidCog(rocketbot))
  114. if LoggingCog.supports_intents(rocketbot.intents):
  115. await rocketbot.add_cog(LoggingCog(rocketbot))
  116. if PatternCog.supports_intents(rocketbot.intents):
  117. await rocketbot.add_cog(PatternCog(rocketbot))
  118. if URLSpamCog.supports_intents(rocketbot.intents):
  119. await rocketbot.add_cog(URLSpamCog(rocketbot))
  120. if UsernamePatternCog.supports_intents(rocketbot.intents):
  121. await rocketbot.add_cog(UsernamePatternCog(rocketbot))
  122. if VideoPreviewCog.supports_intents(rocketbot.intents):
  123. await rocketbot.add_cog(VideoPreviewCog(rocketbot))
  124. if KickstarterCog.supports_intents(rocketbot.intents):
  125. await rocketbot.add_cog(KickstarterCog(rocketbot))
  126. await rocketbot.start(CONFIG['client_token'], reconnect=True)
  127. print('\nBot aborted')