diff --git a/fjerkroa_bot/discord_bot.py b/fjerkroa_bot/discord_bot.py index b6c5c5b..915c927 100644 --- a/fjerkroa_bot/discord_bot.py +++ b/fjerkroa_bot/discord_bot.py @@ -92,36 +92,25 @@ class FjerkroaBot(commands.Bot): return channel return fallback_channel - async def respond(self, - message: AIMessage, - channel: Union[TextChannel, DMChannel]) -> None: - if isinstance(channel, DMChannel): - if channel.recipient is not None: - channel_name = str(channel.recipient.name) - else: - channel_name = str(channel.id) - else: - channel_name = str(channel.name) - if channel_name in self.config.get('ignore-channels', []) and not message.direct: - logging.info(f"ignore message {repr(message)} for channel {channel_name}") - return - logging.info(f"handle message {repr(message)} for channel {channel_name}") - if channel_name in self.aichannels: - airesponder = self.aichannels[channel_name] - else: - airesponder = self.airesponder + def get_channel_name(self, channel): + if isinstance(channel, DMChannel) and channel.recipient is not None: + return str(channel.recipient.name) + return str(channel.id) if isinstance(channel, DMChannel) else str(channel.name) + + def ignore_message(self, channel_name, message): + return channel_name in self.config.get("ignore-channels", []) and not message.direct + + def log_message_action(self, action, message, channel_name): + logging.info(f"{action} message {repr(message)} for channel {channel_name}") + + def get_ai_responder(self, channel_name): + return self.aichannels[channel_name] if channel_name in self.aichannels else self.airesponder + + async def send_message_with_typing(self, airesponder, channel, message): async with channel.typing(): - response = await airesponder.send(message) - if response.hack: - logging.warning(f"User {message.user} tried to hack the system.") - if response.staff is None: - response.staff = f"User {message.user} try to hack the AI." - answer_channel = self.channel_by_name(response.channel, channel) - if response.staff is not None and self.staff_channel is not None: - async with self.staff_channel.typing(): - await self.staff_channel.send(response.staff, suppress_embeds=True) - if not response.answer_needed or answer_channel is None: - return + return await airesponder.send(message) + + async def send_answer_with_typing(self, response, answer_channel, airesponder): async with answer_channel.typing(): if response.picture is not None: images = [discord.File(fp=await airesponder.draw(response.picture), filename="image.png")] @@ -129,6 +118,26 @@ class FjerkroaBot(commands.Bot): else: await answer_channel.send(response.answer, suppress_embeds=True) + async def respond(self, + message: AIMessage, + channel: Union[TextChannel, DMChannel] + ) -> None: + channel_name = self.get_channel_name(channel) + if self.ignore_message(channel_name, message): + self.log_message_action("ignore", message, channel_name) + return + self.log_message_action("handle", message, channel_name) + airesponder = self.get_ai_responder(channel_name) + response = await self.send_message_with_typing(airesponder, channel, message) + if response.hack: + logging.warning(f"User {message.user} tried to hack the system.") + if response.staff is None: + response.staff = f"User {message.user} try to hack the AI." + answer_channel = self.channel_by_name(response.channel, channel) + if not (response.answer_needed and answer_channel is not None): + return + await self.send_answer_with_typing(response, answer_channel, airesponder) + async def close(self): self.observer.stop() await super().close()