Discord.py - 查看其他用户的余额

Discord.py - check balance of other users

我制作的机器人有一个经济系统,你可以通过 !bal 查看你的余额。但是我希望其他用户可以检查您的余额,您也可以检查其他用户的余额。但现在我只知道如何编码,你只能检查自己的余额。我怎样才能更改代码,以便您可以通过输入他们的 ID 或提及他们来检查其他用户的余额?

async def balance(ctx):
    await open_account(ctx.author)
    user = ctx.author

    users = await get_bank_data()

    lvsAmt = users[str(user.id)]["leaves"]
    tckAmt = users[str(user.id)]["tickets"]
    gemAmt = users[str(user.id)]["gems"]

    em = discord.Embed(
        title=f"{ctx.author.mention}'s Balance",
        description=
        f"<:zoa_leaves:918778938707279883> {lvsAmt}\n<:zoa_ticket:918774141400801330> {tckAmt}\n<:zoa_gems:918777349946888212> {gemAmt}",
        color=0xe9a9a9)
    await ctx.send(embed=em)

async def open_account(user):

    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["leaves"] = 0
        users[str(user.id)]["tickets"] = 0
        users[str(user.id)]["gems"] = 0

    with open('mainbank.json', 'w') as f:
        json.dump(users, f)

    return True


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

    return users

我试图将 ctx.author 更改为 ctx.user 但它不起作用

您可以在命令中添加 member 参数:async def balance(ctx, member: discord.Member = None):

async def balance(ctx, member: discord.Member):
    user = member or ctx.author
    await open_account(user)
    users = await get_bank_data()
    lvsAmt = users[str(user.id)]["leaves"]
    tckAmt = users[str(user.id)]["tickets"]
    gemAmt = users[str(user.id)]["gems"]

    em = discord.Embed(
        title=f"{user.mention}'s Balance",
        description=
        f"<:zoa_leaves:918778938707279883> {lvsAmt}\n<:zoa_ticket:918774141400801330> {tckAmt}\n<:zoa_gems:918777349946888212> {gemAmt}",
        color=0xe9a9a9)
    await ctx.send(embed=em)

然后用户将能够通过键入类似 [your_prefix]bal @User#1234.

的内容来使用该命令

如果有人需要,这是我现在使用的代码,它工作正常!

@client.command(aliases=['bal'])
async def balance(ctx, member: discord.Member=None):
    if member is None:
      member = ctx.author

    user = member or ctx.author
    await open_account(user)
    users = await get_bank_data()
    lvsAmt = users[str(user.id)]["leaves"]
    tckAmt = users[str(user.id)]["tickets"]
    gemAmt = users[str(user.id)]["gems"]

    em = discord.Embed(
        title=f"{user.mention}'s Balance",
        description=
        f"<:zoa_leaves:918778938707279883> {lvsAmt}\n<:zoa_ticket:918774141400801330> {tckAmt}\n<:zoa_gems:918777349946888212> {gemAmt}",
        color=0xe9a9a9)
    await ctx.send(embed=em)

async def open_account(user):

    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["leaves"] = 0
        users[str(user.id)]["tickets"] = 0
        users[str(user.id)]["gems"] = 0

    with open('mainbank.json', 'w') as f:
        json.dump(users, f)

    return True


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

    return users