Refactor respond function.

This commit is contained in:
OK 2023-04-12 19:56:25 +02:00
parent 4cfb6c162e
commit b1a38d5e86

View File

@ -92,36 +92,25 @@ class FjerkroaBot(commands.Bot):
return channel return channel
return fallback_channel return fallback_channel
async def respond(self, def get_channel_name(self, channel):
message: AIMessage, if isinstance(channel, DMChannel) and channel.recipient is not None:
channel: Union[TextChannel, DMChannel]) -> None: return str(channel.recipient.name)
if isinstance(channel, DMChannel): return str(channel.id) if isinstance(channel, DMChannel) else str(channel.name)
if channel.recipient is not None:
channel_name = str(channel.recipient.name) def ignore_message(self, channel_name, message):
else: return channel_name in self.config.get("ignore-channels", []) and not message.direct
channel_name = str(channel.id)
else: def log_message_action(self, action, message, channel_name):
channel_name = str(channel.name) logging.info(f"{action} message {repr(message)} for channel {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}") def get_ai_responder(self, channel_name):
return return self.aichannels[channel_name] if channel_name in self.aichannels else self.airesponder
logging.info(f"handle message {repr(message)} for channel {channel_name}")
if channel_name in self.aichannels: async def send_message_with_typing(self, airesponder, channel, message):
airesponder = self.aichannels[channel_name]
else:
airesponder = self.airesponder
async with channel.typing(): async with channel.typing():
response = await airesponder.send(message) return await airesponder.send(message)
if response.hack:
logging.warning(f"User {message.user} tried to hack the system.") async def send_answer_with_typing(self, response, answer_channel, airesponder):
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
async with answer_channel.typing(): async with answer_channel.typing():
if response.picture is not None: if response.picture is not None:
images = [discord.File(fp=await airesponder.draw(response.picture), filename="image.png")] images = [discord.File(fp=await airesponder.draw(response.picture), filename="image.png")]
@ -129,6 +118,26 @@ class FjerkroaBot(commands.Bot):
else: else:
await answer_channel.send(response.answer, suppress_embeds=True) 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): async def close(self):
self.observer.stop() self.observer.stop()
await super().close() await super().close()