Experimental Discord bot written in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bot.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.cogs.basecog import BaseCog
  7. from rocketbot.cogsetting import CogSetting
  8. from rocketbot.utils import bot_log, dump_stacktrace
  9. class Rocketbot(commands.Bot):
  10. """
  11. Bot subclass
  12. """
  13. def __init__(self, command_prefix, **kwargs):
  14. super().__init__(command_prefix, **kwargs)
  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. for cog in self.cogs.values():
  40. if isinstance(cog, BaseCog):
  41. bcog: BaseCog = cog
  42. if len(bcog.settings) > 0:
  43. CogSetting.set_up_all(bcog, self, bcog.settings)
  44. try:
  45. synced_commands = await self.tree.sync()
  46. for command in synced_commands:
  47. bot_log(None, None, f'Synced command: /{command.name}')
  48. except Exception as e:
  49. dump_stacktrace(e)
  50. # Current active bot instance
  51. rocketbot: Optional[Rocketbot] = None
  52. def __create_bot():
  53. global rocketbot
  54. if rocketbot is not None:
  55. return
  56. bot_log(None, None, 'Creating bot...')
  57. intents = Intents.default()
  58. intents.messages = True # pylint: disable=assigning-non-slot
  59. intents.message_content = True # pylint: disable=assigning-non-slot
  60. intents.members = True # pylint: disable=assigning-non-slot
  61. intents.presences = True
  62. rocketbot = Rocketbot(command_prefix=CONFIG['command_prefix'], intents=intents)
  63. __create_bot()
  64. from rocketbot.cogs.autokickcog import AutoKickCog
  65. from rocketbot.cogs.configcog import ConfigCog
  66. from rocketbot.cogs.crosspostcog import CrossPostCog
  67. from rocketbot.cogs.generalcog import GeneralCog
  68. from rocketbot.cogs.joinagecog import JoinAgeCog
  69. from rocketbot.cogs.joinraidcog import JoinRaidCog
  70. from rocketbot.cogs.logcog import LoggingCog
  71. from rocketbot.cogs.patterncog import PatternCog
  72. from rocketbot.cogs.urlspamcog import URLSpamCog
  73. from rocketbot.cogs.usernamecog import UsernamePatternCog
  74. async def start_bot():
  75. bot_log(None, None, 'Bot initializing...')
  76. bot_log(None, None, f"Type {CONFIG['command_prefix']}help in Discord for available commands.")
  77. # Core
  78. await rocketbot.add_cog(GeneralCog(rocketbot))
  79. await rocketbot.add_cog(ConfigCog(rocketbot))
  80. # Optional
  81. await rocketbot.add_cog(AutoKickCog(rocketbot))
  82. await rocketbot.add_cog(CrossPostCog(rocketbot))
  83. await rocketbot.add_cog(JoinAgeCog(rocketbot))
  84. await rocketbot.add_cog(JoinRaidCog(rocketbot))
  85. await rocketbot.add_cog(LoggingCog(rocketbot))
  86. await rocketbot.add_cog(PatternCog(rocketbot))
  87. await rocketbot.add_cog(URLSpamCog(rocketbot))
  88. await rocketbot.add_cog(UsernamePatternCog(rocketbot))
  89. await rocketbot.start(CONFIG['client_token'], reconnect=True)
  90. print('\nBot aborted')