[英]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.