【问题标题】:Translation in discord.py bot doesn't workdiscord.py 机器人中的翻译不起作用
【发布时间】:2021-02-17 08:49:22
【问题描述】:

我一直在尝试使用一个名为 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'

【问题讨论】:

  • 看起来 commands.group() 某处有问题。如果您确实使用 commands.group,您能否提供更多代码?根据回溯,代码块本身似乎没有问题。

标签: python discord.py bots google-translate


【解决方案1】:

我遇到了同样的问题。安装 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)

【讨论】:

    【解决方案2】:

    由于我没有足够的声誉,我无法添加 cmets,所以一旦我获得有关您的代码的更多信息,我将编辑这篇文章。

    能否请您上传完整的回溯?另外,如果在对象上调用方法 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 可以使用

    【讨论】:

    • 我确实将我的函数签名更改为async def translate(self, ctx, message):,但它仍然显示相同的错误。我现在已经添加了完整的回溯,请查看。
    • 您是否尝试过在调用方法后打印变量message?它是否与您要传递的论点相对应?该参数是否对应于函数translator.detect 所期望的?您也可以尝试使用python typing(至少对于变量消息),使其看起来像这样 async def translate(self, ctx: discord.ext.commands.Context, message: str):`。这允许在调用命令时自动转换,还可以帮助您的 IDE 自动完成
    猜你喜欢
    • 2020-11-21
    • 2019-02-27
    • 2021-08-05
    • 2021-11-11
    • 2023-03-20
    • 1970-01-01
    • 2021-04-20
    • 2022-01-16
    • 2019-07-10
    相关资源
    最近更新 更多