繁体   English   中英

如何将 json 文件传输到 discord.py 上的嵌入中?

[英]How can I transfer an json file into an embed on discord.py?

我试过这个:

@bot.command()
@commands.has_any_role('KING')
async def list(ctx):

    with open(r'F:\DiscordBot\PyCharm\Knastwärter\knastusers.json', 'r')as read_file:
        users = json.load(read_file)

    embedlist = discord.Embed(title='__**Liste aller Häftlinge**__', description=
                              f'{users}')
    await ctx.send(embed=embedlist)

但我得到了这个:

{'369910055665008652': 'Scuux#0001', '605948244282834944': 'test12312424#3354'}

但我希望用户名和 ID 排列在另一个下方,没有字符串和尾括号。

我认为您不会自己传输 json 文件,否则您的代码可能会如下所示:

@bot.command(name='file')
async def cmd_file(ctx):
    await ctx.send(file=discord.File('./a.json'))

您的问题是如何传输 json 文件中的信息,所以我认为您需要Embed.add_field来实现这一点:

@bot.command(name='list')
async def cmd_list(ctx):
    with open('./a.json', 'r') as read_file:
        users = json.load(read_file)

    embedlist = discord.Embed(title='Title', description='User List')

    join = lambda x: '\n'.join(x)
    embedlist.add_field(name='User Name', value=join(users.values()))
    embedlist.add_field(name='User ID', value=join(users.keys()))

    await ctx.send(embed=embedlist)

请注意, list是一种内置数据类型,应避免将 function 命名为list 我建议你给你的 function 名字加上一个前缀,并在bot.command中使用name参数。

结果:

结果

您可以根据自己的喜好进行调整,discord.py 的文档中有更多详细信息,包括许多美化嵌入的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM