简体   繁体   English

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

[英]Discord.py - check balance of other users

the bot im making has a economy system and u can check ur balance by doing !bal .我制作的机器人有一个经济系统,你可以通过!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?我如何更改代码,以便您可以通过输入他们的 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

i tried to change the ctx.author to ctx.user but it doesnt work我试图将ctx.author更改为ctx.user但它不起作用

You can add member argument to your command: async def balance(ctx, member: discord.Member = None):您可以在命令中添加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)

Then users will be able to use the command by typing something like [your_prefix]bal @User#1234 .然后用户将能够通过键入类似[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM