我一直在嘗試在嵌入中顯示我的 json 檔案中的資料。但是,我似乎找不到顯示它的方法,需要一些幫助來找出方法。這是json檔案資料:
{
"Slims Mod Bot": {
"Felix\u2122": 2,
"DustinFoes": 16,
"Slim Beatbox": 26,
},
換句話說,我不知道如何在沒有所有符號的情況下整齊地顯示這些資料。
這是發送資料并計算訊息的代碼:
@bot.listen()
async def on_message(message):
if not message.author.bot:
with open('message_count.json','r') as f:
global message_count
message_count = json.load(f)
if message.guild.name not in message_count:
message_count[message.guild.name] = {}
try:
message_count[message.guild.name][message.author.name] = 1
except KeyError:
message_count[message.guild.name][message.author.name] = 1
with open('message_count.json','w') as f:
json.dump(message_count,f,indent=4)
uj5u.com熱心網友回復:
對于遇到同樣問題的任何人,我的朋友和他的團隊找到了答案:
@bot.command()
async def leaderboard(ctx):
with open('messages.json') as file:
data = json.load(file)
for key, value in data["Slims Mod Bot"].items():
embed = discord.Embed(
title="Leadboard",
description="Test",
color=0xFF000
)
embed.add_field(name = key, value = value)
await ctx.respond(embed=embed)
請注意第一組和第二組代碼的區別
@bot.command()
async def leaderboard(ctx):
embed = discord.Embed(
title="Leadboard",
description="Test",
color=0xFF000
)
with open('messages.json') as file:
data = json.load(file)
for key, value in data["Slims Mod Bot"].items():
try:
embed.add_field(name = key, value = value)
except: return
await ctx.respond(embed=embed)
而不是“embed = discord.Embed”在“json.load(file)”下面,它應該在上面。但是,嵌入欄位仍應位于“ json.load (file)”下方,如果我們不這樣做,它將導致機器人一遍又一遍地發送嵌入內容,本質上會產生違反 ToS的不需要的垃圾郵件,希望這會有所幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529637.html
