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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AttributeError:'str'对象没有属性'author'相关的知识,希望对你有一定的参考价值。

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

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.author更改为context.author

另一答案

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)

以上是关于AttributeError:'str'对象没有属性'author'的主要内容,如果未能解决你的问题,请参考以下文章

解决编码问题:AttributeError: 'str' object has no attribute 'decode'

我遇到错误:AttributeError:'str'对象没有属性'isfloat'

tkinter:AttributeError:'str'对象没有属性'tk'

AttributeError: 'str' object has no attribute 'decode'

sklearn使用FeatureHasher处理字符串特征: AttributeError: ‘str‘ object has no attribute ‘items‘

解决 AttributeError: 'str' object has no attribute 'read'