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

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

我能够将我自己的 Discord 机器人代码与编写代码以通过用户输入将数据写入 Google 表格的人进行某种程度的合并。但是,当我尝试向其中引入经济系统时,我不断出错。应该发生的是,当有人输入此命令时:

!bet result[0],result[1],result[2]

我会将结果[2] 的值减去他们的货币,在我的例子中,代币。我是这样写的:

@client.command()
async def bet(ctx,message):
    await open_account(ctx.author)
    users = await token_data()
    user = ctx.author

    if message.content.startswith('!b '):

        msg = message.content[3:]
        result = [x.strip() for x in msg.split(',')]
        SPREADSHEET_ID = 'Sheet ID here'
        RANGE_NAME = result[0]+'!A2'
        FIELDS = 3
        token_bet = int(result[2])
          
    if len(result) == FIELDS:
            print(message.created_at)
            DATA = [ctx.author.name] + [str(ctx.author.id)] + [str(message.created_at)] + result
            sheet.add(SPREADSHEET_ID, RANGE_NAME, DATA)
            await message.channel.send('Thank you! Your bet has been placed :)')
           
    else:
            await message.channel.send('Error: Please try again!'.format(FIELDS,FIELDS-1))

            users[str(user.id)]["token"] -= token_bet

问题是每当我尝试执行命令时,它都会给我这个错误:

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

关于为什么的任何想法?任何帮助将不胜感激。

显然 message 没有定义 class。 如果你的变量是一些字符串输入,我假设,你需要将这个字符串声明为一个(已经给定或self-written)class对象的实例,它继承了属性content.

很遗憾,鉴于这段代码,无法为您提供进一步的建议!

我认为您从使用 on_message 的地方复制了您的代码,并将其放入命令中。在 on_message 中,message 是一个 Message 对象,因此要获取您必须使用 message.content 的内容,但是在命令中,您只会获取原始字符串。我建议将您的命令 def 修改为:

async def bet(ctx, *, message):

这将确保将整个消息内容传递给您的命令。然后将命令中 message.content 的所有实例替换为 message

编辑: 此外,如果您有类似 message.channel.send 的内容,您需要将其更改为 ctx.send,以向上下文

发送消息

如果您还有其他问题,我建议您查看 Commands documentation