首页 > 解决方案 > discord.py 中的翻译命令无法正常工作

问题描述

所以我正在尝试制作一个翻译命令,在该命令中我可以允许其他用户单独将单词(例如 Hello World)翻译成其他语言(例如德语)

但是,事情并没有像他们应该的那样工作......它返回这个错误,例如:

Ignoring exception in command translate:
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await self.prepare(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 789, in prepare
    await self._parse_arguments(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 706, in _parse_arguments
    kwargs[name] = await self.transform(ctx, param)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 542, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: text is a required argument that is missing.
Ignoring exception in command translate:
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "no", line 253, in translate
    translated = translator.translate(text, src=fro, dest=to)
  File "C:\Python39\lib\site-packages\googletrans\client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "C:\Python39\lib\site-packages\googletrans\client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "C:\Python39\lib\site-packages\googletrans\gtoken.py", line 194, in do
    self._update()
  File "C:\Python39\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:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Python39\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'

已经使用的代码是这个:

import googletrans
from googletrans import Translator


@client.command(aliases = ['trans'])
async def translate(ctx, fro, to, *, text):
    translator = Translator(service_urls = ['translate.googleapis.com'])
    translated = translator.translate(text, src=fro, dest=to)
    embed = discord.Embed(title="Translator", color=0xff2200)
    embed.set_footer(text=f"Requested by {ctx.author.name}#{ctx.author.discriminator}", icon_url=f"{ctx.author.avatar_url}")
    embed.add_field(name="From language", value=fro.capitalize(), inline=False)
    embed.add_field(name="What is being Translated", value=text, inline=False)
    embed.add_field(name="To Language", value=to.capitalize(), inline=False)
    embed.add_field(name="Translated Text", value=translated.text, inline=False)

    await ctx.send(embed=embed)

所需的东西(在本例中为 googletrans 4.0.0-rc1)已安装并导入......但它仍然拒绝工作......

标签: pythondiscord.pytranslation

解决方案


推荐阅读