Skip to content
Advertisement

Translation in discord.py bot doesn’t work

I’ve been trying to make my discord bot translate texts using a module called googletrans. It seems fairly simple and it should have worked without any hassle, or so I thought.

So after my import statements, I have translator = Translator(). My following cog code is:

@commands.command(aliases=["tl", "Tl", "Translate"])
    async def translate(self, ctx, *, message):
        language = translator.detect(message)
        translation = translator.translate(message)
        embed = discord.Embed(color=discord.Color.dark_theme())
        embed.add_field(name=f"Language: {language} ", value=f'{translation}')
        await ctx.send(embed=embed)

But it shows this error: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'group'. Where am I going wrong? Any help would be much appreciated!

Edit: the full traceback:

Ignoring exception in command translate:
Traceback (most recent call last):
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:Userswave computerPycharmProjectspythonProjectcogstranslate.py", line 13, in translate
    language = translator.detect(message)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransclient.py", line 255, in detect
    data = self._translate(text, 'en', 'auto', kwargs)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransclient.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransgtoken.py", line 194, in do
    self._update()
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesgoogletransgtoken.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 "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandsbot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.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'

Advertisement

Answer

I had a same problem. Installing alpha version of google trans helped, so try doing this:

pip install googletrans==3.1.0a0

And:

from googletrans import Translator

@client.command()
async def translate(ctx, lang, *, thing):
    translator = Translator()
    translation = translator.translate(thing, dest=lang)
    await ctx.send(translation.text)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement