【发布时间】: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