【问题标题】:Tracking Guild Names and Guild Ids In Embeds在嵌入中跟踪公会名称和公会 ID
【发布时间】:2021-02-25 23:13:22
【问题描述】:

我有这个代码:

  logging_channel=client.get_channel(776924690291097621)
  embed = discord.Embed(
    title = 'Command Used',
    description = f"""
    User's Id: {ctx.author.id}
    User's Name: {ctx.author.name}
    Command: !help
    Guild Name: {discord.Message.guild}
    Guild Id: {discord.Message.guild_id} """,
    colour = discord.Colour.purple()
    )
  embed.set_footer(text = "Support Server")
  embed.set_author(name = "") 
  await logging_channel.send(embed=embed)

它应该发送一个带有作者 ID、作者姓名、命令、公会名称和公会 ID 的嵌入。但是,当我运行它时,我得到了这个错误:

Command raised an exception: AttributeError: type object 'Message' has no attribute 'guild_id'

当我删除“discord.Message.guild_id”部分时,它只会发送:

Guild: 作为嵌入的一部分。

我该如何解决这个问题,错误是什么意思?

【问题讨论】:

  • 如果您删除了 guild_id,它将返回与消息相关的信息。 Message 下存在哪些键?在查找 api discordpy.readthedocs.io/en/latest/api.html#message 时,您可以看到您想要 sauy:discord.Message.guild.id。我认为公会对象可能会利用 str 函数让它显示名称?

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

您可以使用您的Context 对象访问公会nameid,因为它具有guild 属性:

logging_channel=client.get_channel(776924690291097621)
embed = discord.Embed(
    title = 'Command Used',
    description = f"""
    User's Id: {ctx.author.id}
    User's Name: {ctx.author.name}
    Command: !help
    Guild Name: {ctx.guild.name}
    Guild Id: {ctx.guild.id} """,
    colour = discord.Colour.purple()
)
embed.set_footer(text = "Support Server")
embed.set_author(name = "") 
await logging_channel.send(embed=embed)

您所做的是创建一个空白的discord.Message,并且由于discord.Message 对象没有任何guild_id 属性,因此您会遇到此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2022-01-15
    相关资源
    最近更新 更多