【问题标题】:Command raised an exception: AttributeError: 'str' object has no attribute 'message'命令引发异常:AttributeError: 'str' object has no attribute 'message'
【发布时间】:2020-08-25 15:40:31
【问题描述】:
@bot.command()
async def vote(reaction, ctx) :
    vote = ctx.message.content[4:].split(" / ")
    await ctx.send(ctx.channel, "[Vote] - " + vote[0])
    for i in range(1, len(vote)) :
        choose = await ctx.send(ctx.channel, "```" + vote[i] + "```")
        await ctx.add_reaction(choose, "⭕")
        await ctx.add_reaction(choose, "❌")

我得到错误:

Command raised an exception: AttributeError: 'str' object has no attribute 'message'

执行代码时出现以下错误。

它是在我输入这些代码之前执行的。

最后,add_reaction 功能不会在 discord 中运行。

我该如何解决这个错误?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您的代码中有多个错误:

    • ctx 必须是任何命令的第一个参数。在您的情况下,您期望的 discord.Context 对象将是 reaction ,这就是您遇到此错误的原因。您的函数定义应该是:
      @bot.command()
      async def vote(ctx, reaction):
      
    • 在您提供的代码中,reaction 变量是无用的,如果您共享了整个函数,则可以将其删除。
    • 您发送消息的方式是错误的,您应该这样做:
      await ctx.send(f"[Vote] - {vote[0]}")
      (...)
      msg = await ctx.send(f"```{vote[i]}```")
      
    • 您添加反应的方式也是错误的。你现在要做的是:
      @bot.command()
      async def vote(ctx) :
          vote = ctx.message.content[4:].split(" / ")
          await ctx.send(ctx.channel, "[Vote] - " + vote[0])
          for i in range(1, len(vote)):
              msg = await ctx.send(f"```{vote[i]}```")
              await msg.add_reaction("⭕")
              await msg.add_reaction("❌")
      

    您似乎混淆了旧的 discord.py(v1.0 以下)和实际的 discord.py 重写(v1.0 之后)。以下链接总结了这两个版本之间的区别:Migrating to v1.0

    【讨论】:

    • 错误已解决!感谢您的回复。
    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2020-04-26
    • 2017-05-10
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    相关资源
    最近更新 更多