AttributeError:“ str”对象没有属性“作者”

时间:2019-04-21 01:28:41

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

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

  

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)```

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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)