【问题标题】:discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'author'discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'str'对象没有属性'author'
【发布时间】:2021-12-07 10:24:21
【问题描述】:

我从 Python 开始,我正在制作一个 Discord 机器人几周,现在,当我试图在我的机器人中创建一个“投票系统”时,我遇到了这样的错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'author'

我的代码:

    @client.command()
async def hlasovani(self, ctx, volba1, volba2, *, tema):
    embed = discord.Embed(title = tema,description = f":one: {volba1}\n\n:two: {volba2}",color = ctx.author.color,timestamp = datetime.datetime.utcnow())
    embed.set_footer(text = f"Hlasování zahájil {ctx.author.name}")
    embed.set_thumbnail(url = ctx.author.avatar_url)
    message = await ctx.send(embed = embed)
    await message.add_reaction("1️⃣")
    await message.add_reaction("2️⃣")
    await asyncio.sleep(5)

    newmessage = await ctx.fetch_message(message.id)
    onechoice = await newmessage.reactions[0].users().flatten()
    secchoice = await newmessage.reactions[1].users().flatten()

    vysledek = "REMÍZA!"
    if len(onechoice)>len(secchoice):
        vysledek = volba1
    elif len(secchoice)<len(onechoice):
        vysledek = volba2

    embed = discord.Embed(title = tema, description = f"Výsledek: {vysledek}", color = ctx.author.color, timestamp = datetime.datetime.utcnow())
    embed.set_footer(text = f"{volba1} || {volba2}")

    await newmessage.edit(embed = embed)

我将非常感谢任何形式的帮助...

【问题讨论】:

  • 这个错误发生在哪一行,显示完整的回溯(编辑帖子以包含它)

标签: python discord discord.py bots


【解决方案1】:

由于您使用的是@client.command() 而不是@commands.command(),我会假设您的命令不在 cog 或其他类中。如果是这种情况,您需要从命令中删除 self 参数。

async def hlasovani(ctx, volba1, volba2, *, tema):

【讨论】:

  • 非常感谢,成功了!
猜你喜欢
  • 2021-01-30
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多