Skip to content

Commit

Permalink
Merge pull request #2160 from sopel-irc/translate-mangle-None
Browse files Browse the repository at this point in the history
translate: handle failure/error better in `.mangle`
  • Loading branch information
dgw authored Aug 6, 2021
2 parents 6ac286b + 4d43ec0 commit 3d07382
Showing 1 changed file with 10 additions and 0 deletions.
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

0 comments on commit 3d07382

Please sign in to comment.