Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate: handle failure/error better in .mangle #2160

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sopel/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ def mangle(bot, trigger):
"""Repeatedly translate the input until it makes absolutely no sense."""
long_lang_list = ['fr', 'de', 'es', 'it', 'no', 'he', 'la', 'ja', 'cy', 'ar', 'yi', 'zh', 'nl', 'ru', 'fi', 'hi', 'af', 'jw', 'mr', 'ceb', 'cs', 'ga', 'sv', 'eo', 'el', 'ms', 'lv']
lang_list = []

for __ in range(0, 8):
lang_list = get_random_lang(long_lang_list, lang_list)
random.shuffle(lang_list)

if trigger.group(2) is None:
try:
phrase = (bot.memory['mangle_lines'][trigger.sender], '')
Expand All @@ -241,9 +243,11 @@ def mangle(bot, trigger):
return
else:
phrase = (trigger.group(2).strip(), '')

if phrase[0] == '':
bot.reply("What do you want me to mangle?")
return

for lang in lang_list:
backup = phrase
try:
Expand All @@ -264,6 +268,12 @@ def mangle(bot, trigger):
phrase = backup
break

if phrase[0] is None:
# translate() returns (None, None) if an error happens,
# usually because the bot has exceeded a rate limit
bot.reply("Translation rate limit reached. Try again later.")
return

bot.say(phrase[0])


Expand Down