'NoneType' 尝试使用 discord 机器人的 googletrans 库进行翻译时,对象没有属性 'group'

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

我在尝试翻译消息内容时遇到错误(请注意,我确实导入了此消息的最新版本:https://pypi.org/project/googletrans/),我已经检查了足够多的时间,我认为我是正确地做事

完整代码:

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)

我遇到的错误:

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'

好的,所以使用这个(固定库)解决了我的问题:https://pypi.org/project/google-trans-new/

还有一个关于官方 googletrans 库的已发布的 alpha 版本已修复此错误,您可以在此线程中找到更多相关信息:

新代码:

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)