Skip to content

Commit e31f14f

Browse files
committed
Update general_trans.py
Made a static method of getting the pronunciation so the code wasn't repeated for both the listening command and the slash command. Also improved the pronunciation getting method after noticing some odd behaviour during testing. Added Crambor's suggestion of not outputting a translation if the original is the same as the translated text, to avoid unwanted spam translations. Finally made uniform the variable name for the translated data: It was "translated" in one command and "translation" in another. I just made both "translation"
1 parent 8ad941f commit e31f14f

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/zorak/cogs/general/general_trans.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ def __init__(self, bot):
3333
self.translator = Translator()
3434
self.threshold = 0.75
3535

36+
@staticmethod
37+
def pronunciation(message):
38+
"""
39+
Static Method. Call with GoogleTranslate.pronunciation(message)
40+
Looks up the pronunciation from the extra data
41+
"""
42+
if message:
43+
message = [*filter(None, message[0])]
44+
return f"\npronunciation: ({message[0].lower()})"
45+
return ""
46+
3647
@commands.Cog.listener()
3748
async def on_message(self, message):
3849
"""
@@ -75,15 +86,20 @@ async def on_message(self, message):
7586
# translate message
7687
translation = self.translator.translate(message.content, dest='en')
7788

89+
if translation.text.strip().lower() == message.content.strip().lower():
90+
"""
91+
Check to see if the result is the same as the original. If so, do not print
92+
This is to avoid spamming random haha's and hehehe's. (Thanks Crambor!)
93+
"""
94+
return
95+
7896
# send results of translation as embed
7997
embed = discord.Embed(
8098
title=f"{message.content}:",
8199
description=f"{translation.text}")
82100

83101
footer = f"translated from {detected_language}\nconfidence: {confidence * 100:0.2f}%"
84-
pronunciation = translation.extra_data["translation"][1:]
85-
if pronunciation:
86-
footer += f"\npronunciation: ({pronunciation[0][-1].lower()})"
102+
footer += GoogleTranslate.pronunciation(translation.extra_data["translation"][1:2])
87103

88104
embed.set_footer(text=footer)
89105
await message.channel.send(embed=embed)
@@ -95,15 +111,13 @@ async def translate(self, ctx, destination_language, text):
95111
"""
96112
logger.info("%s used the %s command.", ctx.author.name, ctx.command)
97113
try:
98-
translated = self.translator.translate(text, dest=destination_language)
114+
translation = self.translator.translate(text, dest=destination_language)
99115
embed = discord.Embed(
100116
title=f"{text}:",
101-
description=translated.text
117+
description=translation.text
102118
)
103-
footer = f"translated from {LANGUAGES[translated.src]}"
104-
pronunciation = translated.extra_data["translation"][1:]
105-
if pronunciation:
106-
footer += f"\npronunciation: ({pronunciation[0][-1].lower()})"
119+
footer = f"translated from {LANGUAGES[translation.src]}"
120+
footer += GoogleTranslate.pronunciation(translation.extra_data["translation"][1:2])
107121

108122
embed.set_footer(text=footer)
109123
await ctx.respond(embed=embed)

0 commit comments

Comments
 (0)