如何在discord.py中以表格形式显示数据?

1 python discord discord.py

嗨,我正在创建一个制作积分表/排行榜的机器人,下面是运行非常好的代码。

def check(ctx):
    return lambda m: m.author == ctx.author and m.channel == ctx.channel


async def get_input_of_type(func, ctx):
    while True:
        try:
            msg = await bot.wait_for('message', check=check(ctx))
            return func(msg.content)
        except ValueError:
            continue

@bot.command()
async def start(ctx):
    await ctx.send("How many total teams are there?")
    t = await get_input_of_type(int, ctx)
    embed = discord.Embed(title=f"__**{ctx.guild.name} Results:**__", color=0x03f8fc,timestamp= ctx.message.created_at)
    
    lst = []
    
    for i in range(t):
        await ctx.send(f"Enter team {i+1} name :")
        teamname = await get_input_of_type(str, ctx)
        await ctx.send("How many kills did they get?")
        firstnum = await get_input_of_type(int, ctx)
        await ctx.send("How much Position points did they score?")
        secondnum = await get_input_of_type(int, ctx)
        lst.append((teamname, firstnum, secondnum))  # append 
        
    lstSorted = sorted(lst, key = lambda x: int(x[1]) + int(x[2],),reverse=True) # sort   
    for teamname, firstnum, secondnum in lstSorted:  # process embed
        embed.add_field(name=f'**{teamname}**', value=f'Kills: {firstnum}\nPosition Pt: {secondnum}\nTotal Pt: {firstnum+secondnum}',inline=True)

    await ctx.send(embed=embed)  
Run Code Online (Sandbox Code Playgroud)

结果如下所示:

在此处输入图片说明

但我想知道,我能不能做点什么来得到表格形式的结果,比如团队名称、位置点数、总点数、连续写的击杀点数以及打印在它们下面的结果(我真的不这样做,如果这让你明白我想说什么。)

下面的图片会帮助你理解,

在此处输入图片说明

所以我希望结果采用以下格式。我想不出这样做的方法,如果您能回答这个问题,请这样做,那将是一个非常大的帮助!谢谢。

Den*_*er1 13

使用table2ascii,您可以轻松生成 ascii 表并将其放入 Discord 上的代码块中。

您也可以选择在嵌入中使用它。

from table2ascii import table2ascii as t2a, PresetStyle

# In your command:
output = t2a(
    header=["Rank", "Team", "Kills", "Position Pts", "Total"],
    body=[[1, 'Team A', 2, 4, 6], [2, 'Team B', 3, 3, 6], [3, 'Team C', 4, 2, 6]],
    style=PresetStyle.thin_compact
)

await ctx.send(f"```\n{output}\n```")
Run Code Online (Sandbox Code Playgroud)

表格1

您可以从许多替代样式中进行选择。

from table2ascii import table2ascii as t2a, PresetStyle

# In your command:
output = t2a(
    header=["Rank", "Team", "Kills", "Position Pts", "Total"],
    body=[[1, 'Team A', 2, 4, 6], [2, 'Team B', 3, 3, 6], [3, 'Team C', 4, 2, 6]],
    first_col_heading=True
)

await ctx.send(f"```\n{output}\n```")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


cre*_*eed 5

这可能是你最接近的:

embed.add_field(name=f'**{teamname}**', value=f'> Kills: {firstnum}\n> Position Pt: {secondnum}\n> Total Pt: {firstnum+secondnum}',inline=False)
Run Code Online (Sandbox Code Playgroud)

代码将输出如下内容:

图片

我已经设置inlineFalse并添加>字符到每个统计。