Discord.py - check balance of other users

Viewed 248

the bot im making has a economy system and u can check ur balance by doing !bal. however i want it such that other users can check your balance and you can check other users balance as well. but right now i only know how to code it that you can check your own balance only. how can i change the code so that you can check other users balance by entering their id or mentioning them?

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

i tried to change the ctx.author to ctx.user but it doesnt work

2 Answers

You can add member argument to your command: 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)

Then users will be able to use the command by typing something like [your_prefix]bal @User#1234.

if anyone needs it, here's the code that im using now, it works fine!

@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
Related