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`

Cherry-picked from master: 3d07382
  • Loading branch information
dgw committed Aug 6, 2021
1 parent d6cd69b commit 44d587e
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 @@ -240,9 +240,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 @@ -251,9 +253,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 @@ -274,6 +278,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 44d587e

Please sign in to comment.