From 7e8e9a69cd6163fe670d81e51e42225bfc664a09 Mon Sep 17 00:00:00 2001 From: phixxy Date: Wed, 17 Jul 2024 23:13:13 -0700 Subject: [PATCH] added a simple translate command for text files --- cogs/chatgpt.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cogs/chatgpt.py b/cogs/chatgpt.py index 3bf0ed8..da7be40 100644 --- a/cogs/chatgpt.py +++ b/cogs/chatgpt.py @@ -312,6 +312,25 @@ class ChatGPT(commands.Cog): self.logger.exception("Error occurred in answer_question") return "Error occurred in answer_question" + @commands.command() + async def translate(self, ctx): + if ctx.message.attachments: + attachment = ctx.message.attachments[0] # assuming only one attachment + await attachment.save(self.working_dir + '/' + attachment.filename) + + with open(self.working_dir + '/' + attachment.filename, 'r') as file: + text = file.read() + question = f"Translate the following text to english: {text}" + # Now text contains the content of the downloaded file + translated_text = await self.answer_question(question) + # Save the translated text to a new file + with open(f'{self.working_dir}/translated_text.txt', 'w') as new_file: + new_file.write(translated_text) + + # Send the text file as an attachment + file = discord.File(f'{self.working_dir}/translated_text.txt') + await ctx.send(file=file) + @commands.command( description="Personality",