首页 > 解决方案 > 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?

标签: pythonasync-awaitdiscord.py

解决方案


推荐阅读