Add support for reactions with emojis.

This commit is contained in:
OK 2024-03-17 12:39:15 +01:00
parent 3bdf9d03c6
commit 73d9b9184d
2 changed files with 26 additions and 2 deletions

View File

@ -292,6 +292,15 @@ class AIResponder(AIResponderBase):
response["picture"] = await self.translate(response["picture"]) response["picture"] = await self.translate(response["picture"])
return True return True
async def memoize(self, user: str, message: str, answer: str) -> None:
self.memory = await self.memory_rewrite(self.memory, user, message, answer)
self.update_memory(self.memory)
async def memoize_reaction(self, message_user: str, reaction_user: str, operation: str, reaction: str, message: str) -> None:
quoted_message = message.replace('\n', '\n> ')
await self.memoize(reaction_user, f'{message_user}:\n> {quoted_message}',
f'User {reaction_user} has {operation} this raction: {reaction}')
async def send(self, message: AIMessage) -> AIResponse: async def send(self, message: AIMessage) -> AIResponse:
# Get the history limit from the configuration # Get the history limit from the configuration
limit = self.config["history-limit"] limit = self.config["history-limit"]
@ -343,8 +352,7 @@ class AIResponder(AIResponderBase):
# Update memory # Update memory
if answer_message.answer is not None: if answer_message.answer is not None:
self.memory = await self.memory_rewrite(self.memory, message.user, message.message, answer_message.answer) await self.memoize(message.user, message.message, answer_message.answer)
self.update_memory(self.memory)
# Return the updated answer message # Return the updated answer message
return answer_message return answer_message

View File

@ -108,6 +108,22 @@ class FjerkroaBot(commands.Bot):
return return
await self.handle_message_through_responder(message) await self.handle_message_through_responder(message)
async def on_reaction_add(self, reaction, user):
if user.bot:
return
airesponder = self.get_ai_responder(self.get_channel_name(reaction.message.channel))
message = str(reaction.message.content) if reaction.message.content else ''
if len(message) > 1:
await airesponder.memoize_reaction(reaction.message.user.name, user.name, 'adding', str(reaction.emoji), message)
async def on_reaction_remove(self, reaction, user):
if user.bot:
return
airesponder = self.get_ai_responder(self.get_channel_name(reaction.message.channel))
message = str(reaction.message.content) if reaction.message.content else ''
if len(message) > 1:
await airesponder.memoize_reaction(reaction.message.user.name, user.name, 'removing', str(reaction.emoji), message)
def on_config_file_modified(self, event): def on_config_file_modified(self, event):
if event.src_path == self.config_file: if event.src_path == self.config_file:
new_config = self.load_config(self.config_file) new_config = self.load_config(self.config_file)