0

I'm trying to use Python to implement a Discord bot, mainly using the Google Translate API. The bot.py source file looks like this:

from dotenv import load_dotenv
from googletrans import Translator

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

bot = commands.Bot(command_prefix=('!', '$', '#', '.'))

@bot.command()
async def translate(ctx, firstlang, secondlang, word):
    translator = Translator()
    result = translator.translate(word, dest=secondlang, src=firstlang)
    await ctx.send(f'```The translation of your word/phrase is: {result}```')

bot.run(TOKEN)

However, when I try running

$translate en es door

on my server I get

Ignoring exception in command translate:
Traceback (most recent call last):
  File "/home/aurinko/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot.py", line 44, in translate
    result = translator.translate(word, dest=secondlang, src=firstlang)
  File "/home/aurinko/lib/python3.8/site-packages/googletrans/client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "/home/aurinko/lib/python3.8/site-packages/googletrans/client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "/home/aurinko/lib/python3.8/site-packages/googletrans/gtoken.py", line 194, in do
    self._update()
  File "/home/aurinko/lib/python3.8/site-packages/googletrans/gtoken.py", line 62, in _update
    code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/aurinko/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/aurinko/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/aurinko/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'group'

how would you go about this? Any help is appreciated

meri
  • 73
  • 3
  • Does this answer your question? [googletrans stopped working with error 'NoneType' object has no attribute 'group'](https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group) – Ture Pålsson Jul 10 '21 at 14:46
  • 1
    I'm not sure if `googletrans` uses official API - so Google may treats it as spamer/hacker and blocks it. You could at least run it in `try/except` to catch error or first get `self.RE_TKK.search(r.text)` and check if it is not `None`. – furas Jul 10 '21 at 16:11

0 Answers0