Discord.py-将输出拆分为多个消息或嵌入



现在我有一个shell命令,可以在我的AWS实例上执行命令。唯一的问题是,有时输出太大,无法在嵌入中显示,有没有办法将内容拆分,使其出现在多个嵌入中,或者使用反应显示输出的不同部分?

@client.command()
@commands.has_role('Bot Manager')
async def shell(ctx, * , command):
author = ctx.message.author
guild = ctx.message.guild
output = ""
p = subprocess.run(command, shell=True, text=True, capture_output=True, check=True)
output += p.stdout
embed = discord.Embed(title = "Shell Process", description = f"Shell Process started by {author.mention}", color = 0x4c594b)
embed.add_field(name = "Output", value = f"```bashn{output}n```")
timestamp = datetime.now()
embed.set_footer(text=guild.name + " | Date: " + str(timestamp.strftime(r"%x")))
await ctx.send(embed = embed)

如有任何帮助,我们将不胜感激!

整个嵌入最多只能有6000个字符,如下所示(不包括标题和字段名(:

  • 最多25个字段
  • 每个字段值1024个字符
  • 主描述中有2048个字符

在您的情况下,您似乎只使用字段,所以这是我们需要担心的一种情况。

话虽如此,您可以相应地拼接output字符串并添加字段:

num_of_fields = len(output)//1024 + 1
for i in range(num_of_fields):
embed.add_field(name="Output" if i == 0 else "u200b",  # You can't have an empty name
value=output[i*1024:i+1*1024])

我添加的u200b是为了不让Output作为添加的每个新字段的字段名称。不幸的是,名称不能留空,所以u200b通常用作占位符。这是一个零宽度空间的unicode。


参考文献:

  • 嵌入字符限制
  • Embed.add_field()

最新更新