【问题标题】:Discord.py sending an embed without the use of add fieldsDiscord.py 在不使用添加字段的情况下发送嵌入
【发布时间】:2021-01-13 08:44:53
【问题描述】:

我正在尝试从用户那里获取消息,然后将其发送到特定的文本频道。消息应嵌入。但是我不喜欢当你有一堆字段时它的样子,我希望discord.Embed() 中的描述来保存文本内容。但这给了我一个错误 TypeError: Object of type Message is not JSON serializable

这是我的代码:

class Changelog(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print('Changelog is loaded')

    @commands.command()
    async def changelog(self, ctx):
        changelog_channel = self.client.get_channel(759547196433104956)
        await ctx.send("`Message: `")
        message = await self.client.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
        embed = discord.Embed(title="Changelog", description=message, color=0)
        await changelog_channel.send(embed=embed)


def setup(client):
    client.add_cog(Changelog(client))

【问题讨论】:

  • 您确定在显示的代码中出现此错误吗?

标签: discord.py discord.py-rewrite


【解决方案1】:

您没有收到此错误,因为您没有添加字段,而是因为您尝试在描述中放置 message 实例,而您应该将该消息的 content 放在那里。描述只能以strings为值。

embed = discord.Embed(title="Changelog", description=message.content, color=0)

wait_for("message") 返回一个discord.Message 实例。 Message 包含该消息的内容、ID、作者、频道以及更多内容。如果只想获取用户发送的文本,则需要content 属性。

有关Message 可以做什么的更多信息可以在API docs 中找到。

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 2022-08-06
    • 2013-10-25
    • 2021-02-13
    • 2021-04-14
    • 1970-01-01
    • 2021-02-05
    • 2018-09-24
    • 1970-01-01
    相关资源
    最近更新 更多