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

问题描述 投票:-4回答:1

我想提到变量(保存用户名),这是我的代码

@bot.command(pass_context=True)
async def book(ctx,stage,low,high):
    channel = bot.get_channel(642826606858993707)
    author = '{0}'.format(ctx.message.author)
    await ctx.send(f'{author.mention}')

将得到错误:AttributeError:'str'对象没有属性'mention'。

所以我该如何解决?感谢

python discord discord.py
1个回答
0
投票

我很快想摆脱的两件事,以稍微改进您的代码,

不再需要

pass_context,因为discord.py不再具有可重写和不可重写的版本。 commanddocumentation中有更详细的说明。

您在代码中同时使用了.formatf字符串。坚持一个会更容易理解。

您分配了可变通道,但似乎从未真正使用过它,除非您提交了一些代码。

错误,要提及消息的作者,您将作者转换为字符串,因此为什么会出现此错误。要提及用户,您可以简单地使用.send发送消息,然后直接使用.mention提及用户。

async def book(ctx):
    await ctx.send(ctx.author.mention)

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.