首页 > 解决方案 > Word limit in an embed description

问题描述

I want to make that when the description of the embed exceeds the word limit it continues in another separate embed but that the previous embed ends with 3 dots (...) eliminating a small part of the message and following it in the other embed, at the moment this is the code I have:

  @commands.command(aliases=["pj"])
  async def personaje(self, ctx, personaje=None, member: discord.Member=None):
    if personaje is None:
      await ctx.send(":x: Debes proporcionar la id del personaje que quieres visualizar")
    else:
     if member is None:
       member = ctx.author
     if os.path.exists("json/Roleplay/Personajes/{member}/{idpersonaje}.json".format(member=member.id, idpersonaje=personaje)): 
      with open("json/Roleplay/Personajes/{member}/{idpersonaje}.json".format(member=member.id, idpersonaje=personaje), 'r') as f:
        data = json.load(f)

        Nombre = data["Nombre"]
        Historia = data["Historia"]
        color = data["Color"]
        personalizado = data["Personalizado"]

      text = ""
      text = "".join(f"\n**{key}:** {value}" for key, value in personalizado.items())
      color = int(color, 16)
      timestamp = datetime.datetime.now()
      prueba = f"**{Nombre} {text}\n\n**Historia:**\n {Historia}"
      if len(prueba) < 2000:
        embed=discord.Embed(description=prueba, color=color)
        embed.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
        embed.timestamp = timestamp
        await ctx.send(embed=embed)
      else:
        embed=discord.Embed(description=prueba[:-3] + "...", color=color)
        embed.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
        embed.timestamp = timestamp
        await ctx.send(embed=embed)
     else:
       await ctx.send(":x: Este personaje no existe")
       return

Example of what I want to do (obviously with a longer text that exceeds 2000 words): https://imgur.com/zytqLKm

标签: discord.py

解决方案


最好的方法是创建一个函数来拆分每 n 个字符的数据。

使用空行名称制作字段可能比使用新嵌入更好。

记住不要在 2000 处分裂,因为你有 ...

def split_length(data: str, n: int):
    '''Splits the data given at each given n'''
    out_data = []
    for i in range(0, len(data), n):
        out_data.append(data[i:i+n])

    return out_data

@bot.command()
async def longtext(ctx):
    embed = discord.Embed(title='Example of long text split')
    long_text = 'qwerty'
    field_values = split_length(data=long_text, n=2)
    
    for value in field_values:
        # "\u200b" is just an empty line
        embed.add_field(name="\u200b", value=f"{value} ...", inline=True)
        
    await ctx.send(embed=embed)

推荐阅读