📜  discord.py 嵌入长度大于 1024 - Python (1)

📅  最后修改于: 2023-12-03 15:00:25.912000             🧑  作者: Mango

Discord.py中如何处理大于1024字符的嵌入?

如果你有过使用discord.py创建一个bot的经验,你就会发现一旦你想要发送一个很长的消息,比如一个嵌入 (embed),你就会遇到"HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body"这样的错误。

这里提供了一种简单的解决方案。

使用文件上传附件

如果你希望在discord上发送一段长文本或嵌入,则可以先将它们写入一个文件中并通过discord.py中的文件上传附件扩展来发送。代码示例如下:

'''python import discord

client = discord.Client()

@client.event async def on_ready(): print('Logged in as {0.user}'.format(client))

@client.event async def on_message(message): if message.content.startswith('!send'): with open('content.txt', 'r') as f: content = f.read() await message.channel.send(content, file=discord.File('content.txt')) client.run('token') '''

这将在当前运行目录中创建名为content.txt的文本文件,并使用其内容创建一个嵌入并将其发送到文本频道中。

使用多个嵌入

如果你不想发送文件附件,则可以将一条消息分为多个嵌入,并使用set_author(name='')方法将它们标记为同一条消息的一部分。示例如下:

'''python import discord

client = discord.Client()

@client.event async def on_ready(): print('Logged in as {0.user}'.format(client))

@client.event async def on_message(message): if message.content.startswith('!send'): content = 'Here is some very long text. ' * 100 embed1 = discord.Embed(description=content[:1024]) embed1.set_author(name='Part 1')

    embed2 = discord.Embed(description=content[1024:])
    embed2.set_author(name='Part 2')

    await message.channel.send(embed=embed1)
    await message.channel.send(embed=embed2)

client.run('token') '''

这将在同一条消息中发送两个嵌入,并将它们标记为一条消息的一部分。

总结

以上是一些常见的处理discord.py中大于1024字符嵌入的方法。如果你使用了其他的方法或有其他的建议,请随时在评论区留言。