Experimental Discord bot written in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bot.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, **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, f'Command 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, 'Event 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, f"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 synced_commands:
  65. bot_log(None, None, f'Synced command: /{command.name}')
  66. except Exception as e:
  67. dump_stacktrace(e)
  68. # Current active bot instance
  69. rocketbot: Optional[Rocketbot] = 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. intents.messages = True
  77. intents.message_content = True
  78. intents.members = True
  79. intents.presences = True
  80. rocketbot = Rocketbot(intents=intents)
  81. __create_bot()
  82. from rocketbot.cogs.autokickcog import AutoKickCog
  83. from rocketbot.cogs.bangcommandcog import BangCommandCog
  84. from rocketbot.cogs.configcog import ConfigCog
  85. from rocketbot.cogs.crosspostcog import CrossPostCog
  86. from rocketbot.cogs.gamescog import GamesCog
  87. from rocketbot.cogs.generalcog import GeneralCog
  88. from rocketbot.cogs.helpcog import HelpCog
  89. from rocketbot.cogs.joinraidcog import JoinRaidCog
  90. from rocketbot.cogs.logcog import LoggingCog
  91. from rocketbot.cogs.patterncog import PatternCog
  92. from rocketbot.cogs.urlspamcog import URLSpamCog
  93. from rocketbot.cogs.usernamecog import UsernamePatternCog
  94. async def start_bot():
  95. bot_log(None, None, 'Bot initializing...')
  96. # Core
  97. await rocketbot.add_cog(GeneralCog(rocketbot))
  98. await rocketbot.add_cog(ConfigCog(rocketbot))
  99. # Optional
  100. await rocketbot.add_cog(AutoKickCog(rocketbot))
  101. await rocketbot.add_cog(BangCommandCog(rocketbot))
  102. await rocketbot.add_cog(CrossPostCog(rocketbot))
  103. await rocketbot.add_cog(GamesCog(rocketbot))
  104. await rocketbot.add_cog(HelpCog(rocketbot))
  105. await rocketbot.add_cog(JoinRaidCog(rocketbot))
  106. await rocketbot.add_cog(LoggingCog(rocketbot))
  107. await rocketbot.add_cog(PatternCog(rocketbot))
  108. await rocketbot.add_cog(URLSpamCog(rocketbot))
  109. await rocketbot.add_cog(UsernamePatternCog(rocketbot))
  110. await rocketbot.start(CONFIG['client_token'], reconnect=True)
  111. print('\nBot aborted')