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

问题描述 投票:0回答:2

我正在建立一个不和谐的机器人,我有一个属性错误的问题,我希望有人会纠正我。它应该运行,但它显示我这个错误:

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

python python-3.x discord.py
2个回答
3
投票

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

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


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)
© www.soinside.com 2019 - 2024. All rights reserved.