discord.py 机器人中的翻译不起作用

Translation in discord.py bot doesn't work

我一直在尝试使用名为 googletrans 的模块让我的 discord 机器人翻译文本。这看起来相当简单,应该可以毫不费力地工作,或者我是这么认为的。

所以在我的导入语句之后,我有 translator = Translator()。 我的以下 cog 代码是:

@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)

但它显示此错误:discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'group'. 我哪里错了?任何帮助将不胜感激!

编辑:完整回溯:

Ignoring exception in command translate:
Traceback (most recent call last):
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\cogs\translate.py", line 13, in translate
    language = translator.detect(message)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\googletrans\client.py", line 255, in detect
    data = self._translate(text, 'en', 'auto', kwargs)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\googletrans\client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\googletrans\gtoken.py", line 194, in do
    self._update()
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\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'

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

Traceback (most recent call last):
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\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'

我无法添加评论,因为我没有足够的声誉,所以一旦我获得有关您的代码的更多信息,我将编辑此 post。

你能上传完整的回溯吗?另外,如果在对象上调用方法 group 之前,您确保它不是 None 会发生什么? (我不知道这是在你的代码中还是在库中)

您是否尝试过在每一步调试和检查变量是什么,如果它们不是您认为的那样

最后,我还认为也许您的变量 message 始终为空,因为您在 message 之前用 * 捕获了其他参数,因此这使得 message 参数成为仅关键字参数,据我所知,这不能使用机器人命令时被传递

您是否尝试过将函数的签名更改为:

async def translate(self, ctx, message):

?

更新:从表面上看,问题发生在库内部,但是如果提供给库函数的参数格式不正确,则可能会发生这种情况。

如果你模拟 bot 命令(在主文件中使用 ctx=None 和 message="Hello my friend, how are you today? I hope you are fine"),会发生什么?

在“语言检测”部分的pypi page about googletrans中可以看到,大多数时候该函数是用一句话调用的。也许如果句子足够短,检测不起作用?

此外,我认为你应该在你的问题中指出你使用的是什么库,因为还有 Google's official translate API 可以使用

我遇到了同样的问题。安装 google trans 的 alpha 版本有帮助,所以尝试这样做:

pip install googletrans==3.1.0a0

并且:

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)