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

时间:2020-08-24 17:23:01

标签: 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)  

结果看起来像这样:

enter image description here

但是我想知道,我可以做些什么来获得表格形式的结果,例如“团队名称”,“位置点数”,“总得分”,“杀掉”得分,以及如何将结果打印在下面(我真的不知道)让您了解我要说的话。)

下面的图片将帮助您理解,

enter image description here

所以我希望结果采用以下格式。我想不出什么办法,如果可以回答,请这样做,那将是非常有用的帮助! 谢谢。

2 个答案:

答案 0 :(得分:1)

这可能是您将得到的最接近的东西:

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

代码将输出如下内容:

image

我已将inline设置为False,并将>字符添加到每个统计信息中。

答案 1 :(得分:0)

好的,有一种方法可以做到这一点,但是,您将无法将字段名称用作团队名称! This image shows how it will look like!

@bot.command(name = 'test')
async def test(context: commands.Context):
    # Example dataset here! 
    DATASET = (
        # Rank, team, kills, positions, totals ... 
        (1, 'A', 2, 4, 6),
        (2, 'B', 3, 3, 6),
        (3, 'C', 4, 2, 6)
    ) # Assumes we have sorted this! 
    s = ['Rank     Team   Kills   PosPts.  Total']
    # This needs to be adjusted based on expected range of values or   calculated dynamically
    for data in DATASET:
        s.append('   '.join([str(item).center(5, ' ') for item in data]))
        # Joining up scores into a line
    d = '```'+'\n'.join(s) + '```'
    # Joining all lines togethor! 
    embed = discord.Embed(title = 'Quotient Results', description = d)
    await context.send(embed = embed)

这应该按预期进行。