【问题标题】:AttributeError: 'str' object has no attribute 'author'AttributeError:“str”对象没有属性“author”
【发布时间】:2019-04-21 01:28:41
【问题描述】:

我正在构建一个不和谐的机器人,但我遇到了属性错误的问题,希望有人能纠正我。它应该运行,但它向我显示此错误:

AttributeError: 'str' 对象没有属性 'author'

import asyncio
import aiohttp
import json
from discord import Game
from discord.ext.commands import Bot


BOT_PREFIX = ('?', '!')
TOKEN = ""

client = Bot(command_prefix=BOT_PREFIX)

@client.command(name='8ball',
                description="Answers a yes/no question.",
                brief="Answers from the beyond.",
                aliases=['eight_ball', 'eightball', '8-ball'],
                pass_context=True)
async def eight_ball(context):
    possible_responses = [
        'That is a resounding no',
        'It is not looking likely',
        'Too hard to tell',
        'It is quite possible',
        'Definitely',
    ]
    await client.process_commands(random.choice(possible_responses) + ", " + context.message.author.mention)


client.run(TOKEN)```

【问题讨论】:

  • context.message 是一个字符串,不是你想的那样。
  • 我该如何解决这个问题

标签: python python-3.x discord.py


【解决方案1】:

首先,尽快重置您的令牌。您的机器人现已被入侵,互联网上的每个人都可以访问它。

现在关于您的问题:您只需将context.message.author 更改为context.author

【讨论】:

    【解决方案2】:

    Bot.process_commands 需要一个 Message 对象,但您传递给它的是一个字符串。

    process_commands 的目的是让您控制何时在您的on_message 事件中处理命令。如果您不提供on_message 事件,默认的会为您调用process_commands

    您似乎正试图将消息发送回调用命令的位置。您可以通过使用Context.send 将消息直接发送到调用上下文来做到这一点(这实际上只是ctx.channel.send 的简写)

    @client.command(name='8ball',
                    description="Answers a yes/no question.",
                    brief="Answers from the beyond.",
                    aliases=['eight_ball', 'eightball', '8-ball'],
                    pass_context=True)
    async def eight_ball(context):
        possible_responses = [
            'That is a resounding no',
            'It is not looking likely',
            'Too hard to tell',
            'It is quite possible',
            'Definitely',
        ]
        await context.send(random.choice(possible_responses) + ", " + context.message.author.mention)
    

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 2021-12-07
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 2021-10-04
      • 2019-12-02
      相关资源
      最近更新 更多