Discord.py等级命令

问题描述 投票:1回答:1
{
    "341522422836625408-679072765583556720": {
        "experience": 111157,
        "level": 34,
        "last_message": 1590382008.6055543
    },
    "692075200690520184-679072765583556720": {
        "experience": 51,
        "level": 3,
        "last_message": 1590373878.9466648
    },
    "465887722721116162-679072765583556720": {
        "experience": 34,
        "level": 2,
        "last_message": 1590380700.2158434
    },
    "477106559676317717-679072765583556720": {
        "experience": 350,
        "level": 5,
        "last_message": 1590331947.6997736
    },
    "377116068994482176-679072765583556720": {
        "experience": 20,
        "level": 2,
        "last_message": 1590329384.397424
    },
}

上面是我要用来制作一个命令的输入数据,在这个命令中会显示出在服务器上经验最多的前三名用户。我已经做了一个命令,给用户打字的经验。现在,代码是这样的。

@client.command()
async def leaderboard(ctx):
    with open('users.json', 'r') as f:
        user = json.load(f)
    guild = str(ctx.guild.id)
    users = str(ctx.author.id)
    order = {k: v for k, v in sorted(user.items(), key = lambda x: x[1]['experience'], reverse=True)}
    await ctx.send(order[str(users + "-" + guild): "experience"])

目前的输出是

('', {'experience': experience_count, 'level': level_count, 'last_message': last_message_id})

user.id, guild.id, experience_count, level_count 和 last_message_id 是不同的数字。

如果有谁能帮我完成这个命令,那就太好了!我是python的初学者,所以代码可能会有点乱或愚蠢。我是一个python初学者,所以代码可能会有点乱或愚蠢。如果你有任何问题,请问我

python command discord discord.py
1个回答
0
投票

试试这个。


data = {
    "341522422836625408": {
        "experience": 111157,
        "level": 34,
        "last_message": 1590382008.6055543
    },
    "692075200690520184": {
        "experience": 51,
        "level": 3,
        "last_message": 1590373878.9466648
    },
    "465887722721116162": {
        "experience": 34,
        "level": 2,
        "last_message": 1590380700.2158434
    },
    "477106559676317717": {
        "experience": 350,
        "level": 5,
        "last_message": 1590331947.6997736
    },
    "377116068994482176": {
        "experience": 20,
        "level": 2,
        "last_message": 1590329384.397424
    },
}


class ExperienceCount:

    def __init__(self, user, experience):
        self.user = user
        self.experience = experience

    def __repr__(self):
        return f'<@{self.user}> has {self.experience} experience'


def get_top_experience(data):
    users = []
    for k, v in data.items():
        users.append(ExperienceCount(k, v['experience']))
    return sorted(users, key=lambda x: x.experience, reverse=True)

a = get_top_experience(data)

(<@USER_ID> @提到用户)

你可以将其纳入到一个命令中。

@client.command
async def leaderboard(ctx):
    leaderboard = get_top_experience(data)
    embed = discord.Embed(title='Top Experience', description='Top experience all time including others not in this server', color=0xff5555)
    embed.add_field(name='**1st Place**', value=leaderboard[0], inline=False)
    embed.add_field(name='**2nd Place**', value=leaderboard[1], inline=False)
    embed.add_field(name='**3rd Place**', value=leaderboard[2], inline=False)
    await ctx.send(embed=embed)

如果你想只在命令被调用的服务器中体验。

@client.command
async def leaderboard(ctx):
    leaderboard1 = get_top_experience(data)
    leaderboard = leaderboard1.copy()
    for i, user in enumerate(leaderboard1):
        if ctx.guild.get_member(user.user) is None:
            del leaderboard[i]
    embed = discord.Embed(title='Top Experience', description='Top experience all time in this server', color=0xff5555)
    embed.add_field(name='**1st Place**', value=leaderboard[0], inline=False)
    embed.add_field(name='**2nd Place**', value=leaderboard[1], inline=False)
    embed.add_field(name='**3rd Place**', value=leaderboard[2], inline=False)
    await ctx.send(embed=embed)
© www.soinside.com 2019 - 2024. All rights reserved.