【问题标题】:Why is my mention not working in discordpy?为什么我的提法在 discord py 中不起作用?
【发布时间】:2021-12-13 23:55:09
【问题描述】:

@“我的用户名” 无法显示在嵌入中。

@client.command()
async def user(ctx, subcommand, name):
    player = osu.fetch_user(username=name)
    if subcommand == "stats":
        embed = discord.Embed(title=f"osu! stats of {name}")
        id = 416651818760929291
        embed.set_footer(text=f"Shuka (osu! bot) by <@{id}>", icon_url="https://i.pinimg.com/originals/92/f7/cb/92f7cbba6e1f629f619ad878066ededc.png")
        await ctx.send(embed=embed)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    正如我在 cmets 中所述 - 嵌入页脚不支持提及(嵌入标题、作者和字段名称也是如此)。因此,在这种情况下,最好的解决方案是只获取您的姓名:

    user = await client.fetch_user(416651818760929291)
    embed.set_footer(text=f"Shuka (osu! bot) by {user}")
    

    或在支持的地方使用提及(例如描述):

    &lt;@!{id}&gt; 而不是&lt;@{id}&gt; - 你忘了在@ 之后使用!

    embed.description = f"Shuka (osu! bot) by <@!{id}>"
    

    或者你也可以fetch_user然后使用mention

    user = await client.fetch_user(416651818760929291)
    embed.description = f"Shuka (osu! bot) by {user.mention}"
    

    【讨论】:

    • 还是不行(codeshare.io/YLyVAE)
    • 哦。抱歉,我忘了你想在embed footer 中使用它,不幸的是它不支持embed footer 中的提及。检查this answer 了解哪些嵌入字段不允许提及。
    • 因此,我建议使用没有.mention 的用户。将 {user.mention} 更改为 {user}。您不会得到提及,但至少您的用户名将与标签一起出现。您的另一个选择是将其移至支持提及的嵌入描述。
    【解决方案2】:

    您可以获取User,然后使用mention 属性

    #somewhere in your globals
    bot.creator = client.get_user(416651818760929291)
    
    ...
    
    @client.command()
    async def user(ctx, subcommand, name):
        player = osu.fetch_user(username=name)
        if subcommand == "stats":
            embed = discord.Embed(title=f"osu! stats of {name}")
            embed.set_footer(text=f"Shuka (osu! bot) by {self.bot.creator.mention}>", icon_url="https://i.pinimg.com/originals/92/f7/cb/92f7cbba6e1f629f619ad878066ededc.png")
            await ctx.send(embed=embed)
    

    【讨论】:

    • 我收到一条错误消息:Traceback(最近一次调用最后一次):文件“h:\py\Projects\osuBot\main.py”,第 7 行,在 bot.creator = bot .get_user(416651818760929291) AttributeError: module 'discord.ext.commands.bot' has no attribute 'get_user'
    • 我的错,我编辑了。应该是client.get_user
    【解决方案3】:

    您不能在嵌入的页脚中提及用户,因此您会得到显示。检索您所做的用户名没有问题。 (并且在页脚中也禁用了所有降价)

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2019-01-06
      • 2021-07-18
      • 2022-01-04
      • 2021-04-21
      • 2021-04-24
      • 2021-11-22
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多