diff --git a/bot/__init__.py b/bot/__init__.py index cbf1a1d..118690e 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -4,4 +4,14 @@ from django.conf import settings BOT_DESCRIPTION = "A simple Discord bot." -hitomi = commands.Bot(command_prefix='!', description=BOT_DESCRIPTION, max_messages=settings.DISCORD_BOT_MAX_MESSAGES) + +class Hitomi(commands.Bot): + """Extend the discord.py Bot, to add more cool stuff.""" + + def __init__(self, *args, **kwargs): + """Initialize bot, and cool stuff.""" + super(Hitomi, self).__init__(*args, **kwargs) + self.on_message_handlers = [] + + +hitomi = Hitomi(command_prefix='!', description=BOT_DESCRIPTION, max_messages=settings.DISCORD_BOT_MAX_MESSAGES) diff --git a/bot/management/commands/starthitomi.py b/bot/management/commands/starthitomi.py index f51e519..a78940e 100644 --- a/bot/management/commands/starthitomi.py +++ b/bot/management/commands/starthitomi.py @@ -17,6 +17,16 @@ async def on_ready(): logger.info("Logged in as {0:s} ({1:s})".format(hitomi.user.name, hitomi.user.id)) +@hitomi.event +async def on_message(message): + """Call all registered on_message handlers.""" + for handler in hitomi.on_message_handlers: + handler(message) + + # let other stuff happen + await hitomi.process_commands(message) + + async def run_bot(): """Initialize and begin the bot in an asyncio context.""" await hitomi.login(settings.DISCORD_BOT_TOKEN) diff --git a/logger/__init__.py b/logger/__init__.py index fc68247..04d252e 100644 --- a/logger/__init__.py +++ b/logger/__init__.py @@ -9,8 +9,7 @@ from bot import hitomi logger = logging.getLogger(__name__) -@hitomi.event -async def on_message(message): +def on_message(message): """Log the seen message.""" try: log_file = log_file_for_message(message) @@ -23,8 +22,8 @@ async def on_message(message): logger.error("error in creating log line") logger.exception(ex) - # let other stuff happen - await hitomi.process_commands(message) + +hitomi.on_message_handlers.append(on_message) @hitomi.event