python - AttributeError:“协程”对象没有属性“文本”
问题描述
所以,我一直在尝试为我的不和谐机器人创建一个翻译命令。到目前为止,这是我的 cog 代码
import googletrans
from googletrans import Translator, constants
from pprint import pprint
import discord
from discord.ext import commands
class translator(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Translator is loaded")
translator = Translator()
@commands.command()
async def translate(self, ctx, texts, lang):
if lang == "":
lang == "en"
translation = translator.translate(texts, dest = lang)
await ctx.send(await translation.text)
def setup(client):
client.add_cog(translator(client))
但即使在尝试改变这么多事情之后,我也遇到了同样的错误。
Translator is loaded
Ignoring exception in command translate:
Traceback (most recent call last):
File "/home/indo/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/indo/Documents/Python/LoB/cogs/translator.py", line 22, in translate
await ctx.send(await translation.text())
AttributeError: 'coroutine' object has no attribute 'text'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/indo/.local/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/indo/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/indo/.local/lib/python3.9/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: 'coroutine' object has no attribute 'text'
/usr/lib/python3.9/asyncio/events.py:80: RuntimeWarning: coroutine 'Command.__call__' was never awaited
self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
我无法修复这个错误。
到目前为止,我认为这两行有问题->
translation = translator.translate(texts, dest = lang)
await ctx.send(await translation.text)```
Can anyone help on what is the error going on?
解决方案
推荐阅读
- ios - 如何在objective-c中创建自定义导航栏以支持safeareaview?
- virtual-environment - 如何解决此错误“DistributionNotFound:未找到'zipp> = 0.5'分发,并且importlib-metadata需要”?
- apache-kafka - 如何输出kafka消费者属性?
- c# - C# URI 验证
- java - 如何修复关于我的 switch 语句的输出?
- google-cloud-platform - 如何通过 API 获取服务帐户对 GCP 中项目和组织的所有角色/权限
- monitoring - 使用凭据在 netscaler 中进行监控
- django - django-tenant 和 djangorestframework 测试
- python - setMargin() 方法不适用于 Qt Python 中的 QVBoxLayout
- vba - ACCESS VBA 如何将多个表中的行添加到列表框