Search code examples
pythongoogle-translation-api

'NoneType' object has no attribute 'group' when trying to translate with the googletrans lib for a discord bot


I'm getting an error when trying to translate a messages' content (note that I did import the latest version of this: https://pypi.org/project/googletrans/), I've checked enough times and I think I'm doing it correctly

Full code:

import discord, googletrans, os, dotenv
from discord.ext import commands
from dotenv import load_dotenv
from googletrans import Translator

load_dotenv()
translate = Translator()
prefix = 'tb!'
bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")
client = discord.Client()
TOKEN = os.getenv('TOKEN')
#translationch = 822024643468460042

@bot.event
async def on_ready():
    print(f'{bot.user.name} connected to Discord!')
    await bot.change_presence(activity=discord.Game(name=f"Translating..."))

@bot.listen()
async def on_message(msg):
    print(msg.content)
    if msg.content == "": return
    text_ = translate.translate(msg.content)
    if text_.text == msg.content: return
    else:
        embed=discord.Embed(title="Translating...", description="Just a second...", color=0x545454)
        msg = await msg.channel.send(embed=embed)
        try:
            emb=discord.Embed(title="Translation", color=0x00ff08)
            emb.add_field(name=f"Sent by {msg.author}", value=f"`{msg.content}`\n`{text_.text}`", inline=False)
            emb.set_footer(text=f"Sent from channel #{msg.channel.name}")
            await msg.edit(embed=emb)
        except:
            i = msg.content
            if i > 1800: i = "The message was too long to display!"
            error=discord.Embed(color=0xff0000)
            error.add_field(name="Error!", value=f"I couldn't translate **{msg.author}**'s message: `{i}`", inline=False) 
            await msg.edit(embed=error)
            return

bot.run(TOKEN)

The error I'm getting:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\borny\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "c:/Users/borny/Desktop/stuff/vscode_projects/translate-bot/main.py", line 24, in on_message
    translate.translate(msg.content)
  File "C:\Users\borny\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "C:\Users\borny\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "C:\Users\borny\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\gtoken.py", line 194, in do
    self._update()
  File "C:\Users\borny\AppData\Local\Programs\Python\Python38\lib\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'

Solution

  • Ok so using this (the fixed lib) solved my problem: https://pypi.org/project/google-trans-new/

    There's also an released alpha about the official googletrans lib that has this bug patched, you can find more about it in this thread: googletrans stopped working with error 'NoneType' object has no attribute 'group'

    New code:

    import discord, google_trans_new, os, dotenv
    from discord.ext import commands
    from dotenv import load_dotenv
    from google_trans_new import google_translator  
    
    load_dotenv()
    translate = google_translator()
    prefix = 'tb!'
    bot = commands.Bot(command_prefix=prefix)
    bot.remove_command("help")
    client = discord.Client()
    TOKEN = os.getenv('TOKEN')
    translationch = 822024643468460042
    
    @bot.event
    async def on_ready():
        print(f'{bot.user.name} connected to Discord!')
        await bot.change_presence(activity=discord.Game(name=f"Translating..."))
    
    @bot.listen()
    async def on_message(msg):
        if msg.content == "": return
        text_ = translate.translate(msg.content)[:-1]
        if text_ == msg.content: return
        else:
            embed=discord.Embed(title="Translating...", description="Just a second...", color=0x545454)
            msg_ = await msg.channel.send(embed=embed)
            try:
                emb=discord.Embed(title="Translated!", color=0x00ff08)
                emb.add_field(name=f"Sent by {msg.author}", value=f"`Text: {msg.content}`\n`Translation: {text_}`", inline=False)
                await msg_.edit(embed=emb)
            except:
                i = msg.content
                if len(i) > 1800: i = "The message was too long to display!"
                error=discord.Embed(color=0xff0000)
                error.add_field(name="Error!", value=f"I couldn't translate **{msg.author}**'s message: `{i}`", inline=False) 
                await msg_.edit(embed=error)
                return
    
    bot.run(TOKEN)