【问题标题】:why my discord leaderboard is not working ? , It is not even showing error and I have tried everything to make it work为什么我的不和谐排行榜不起作用? ,它甚至没有显示错误,我已经尝试了一切让它工作
【发布时间】:2021-09-13 13:49:28
【问题描述】:
@client.command(aliases = ['lb'])
async def leaderboard(ctx , x = 0):
    
    await ctx.send('this command is alive but dead for some reason idk')
    users = await get_bank_data()
    
    leader_board = {}
    total = []
    for user in users:
        name = int(user)
        total_amount = users[user]["wallet"] + users[user]["bank"]
        leader_board[total_amount] = name
        total.append(total_amount)
    total = sorted(total,reverse=True)    
    
    em = discord.Embed(title = f"Top {x} Richest People" , description = "This is decided on the basis of raw money in the bank and wallet",color = discord.Color(0xfa43ee))
    index = 1
    await ctx.send(embed = em)
    for amt in total:
        id_ = leader_board[amt]
        member = client.get_user(id_)
        name = member.name
        em.add_field(name = f"{index}. {name}" , value = f"{amt}",  inline = False)
        if index == x:
            break
        else:
            index += 1
            
    await ctx.send(embed = em)

我不知道为什么我的排行榜命令不起作用,如果我在 for 循环之前使用 await ctx.send(embed = em) 它正在发送 top 0 richest people 但它在 for 循环之后没有发送消息,所以我认为问题是在循环中

这里是 get_bank_data 函数

async def get_bank_data():
    with open('mainbank.json','r') as f:
        users = json.load(f)
    return users

这是json文件

{"555463650358329354": {"wallet": 545, "bank": 7194, "bag": [{"item": "watch", "amount": 0}]}, "886271151729414154": {"wallet": 0, "bank": 0}, "525961784297914378": {"wallet": 0, "bank": 0}, "680988467270123522": {"wallet": 901, "bank": 0}, "725722170206060624": {"wallet": 0, "bank": 507}, "517879304562933780": {"wallet": 137, "bank": 388}, "706831502859698226": {"wallet": 189, "bank": 2124, "bag": [{"item": "watch", "amount": 1}]}, "781446116955914260": {"wallet": 0, "bank": 5511, "bag": [{"item": "watch", "amount": 1}]}, "875681501696643102": {"wallet": 0, "bank": 1664}, "599611694746042380": {"wallet": 0, "bank": 1206}, "786899180240502804": {"wallet": 0, "bank": 104}, "537275315530104832": {"wallet": 0, "bank": 228}, "632848175992012825": {"wallet": 235, "bank": 0}, "704319968183058492": {"wallet": 241, "bank": 0}, "864813571501195315": {"wallet": 0, "bank": 203}}

请帮助p

【问题讨论】:

  • 什么不起作用?
  • 它没有显示任何输出,在for循环之后没有发送任何消息
  • 发送的是嵌入文本还是初始文本?
  • @SuvenPandey ,是的,但在 for 循环之前
  • 我不能在我的测试中使用名称字段,但除此之外它似乎工作正常。这是 mainbank.json 中的完整数据吗?除了尝试发送用户以查看它是否通过整个文件之外,我想不出其他任何东西。

标签: python json dictionary discord.py


【解决方案1】:

代码没有发送任何输出,因为它没有分解

if index == x:
    break

当您更改 index = 1 并在 else 语句中增加它时

index = 0 #start by declaring index = 0
    await ctx.send(embed = em)
    for amt in total:
        id_ = leader_board[amt]
        member = client.get_user(id_)
        name = member.name
        em.add_field(name = f"{index+1}. {name}" , value = f"{amt}",  inline = False)# add 1 to index here
        if index == x: #now if index is 0 it'll break the loop and send the message
            break
        else:
            index += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2022-06-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多