获得用户提到的“XP”。 JSON,Discord.py

问题描述 投票:1回答:1

我正在尝试制作一个用户可以窃取其他用户的XP(美元)的命令。我正在使用random.randintto使它成功或不成功,但我想做一个检查,以确保被提及(抢劫)的用户有足够的XP(美元)供作者窃取。我尝试了一些方法,如if (get_dollars(message.mentions[0].id) < 25:and if (get_dollars(user.mention.id) < 25:没有成功。这是“完整”代码:

client = discord.Client()

try:
    with open("cash.json") as fp:
        cash = json.load(fp)
except Exception:
    cash = {}

def save_cash():
    with open("cash.json", "w+") as fp:
        json.dump(cash, fp, sort_keys=True, indent=4)

def add_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) + dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()

def remove_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) - dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()

def get_dollars(user: discord.User):
    id = user.id
    if id in cash:
        return cash[id].get("dollars", 0)
    return 0

@client.event
async def on_message(message):
    steal = random.randint(1,3)
    stealdollars = random.randint(25, 250)
    if message.content.startswith('!rob'):
        user = message.mentions[0]
        if (get_dollars(user.mention)) < 25:
            await client.send_message(message.channel, "{} is too broke to rob 🤣".format(user.mention))
        elif steal == 1:
            print("{} catches {} slippin and sticks them up for ${} 😨🔫".format(message.author.mention, user.metion, stealdollars))
            await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨🔫".format(message.author.mention, user.mention, stealdollars))
            remove_dollars(user.mention, stealdollars)
            add_dollars(message.author, stealdollars)
        elif steal == 2:
            print("{} kicks {}'s door down but doesn't find any cash 🤦‍".format(message.author.mention, user.mention))
            await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash".format(message.author.mention, user.metion))
        elif steal == 3:
            print("{} gets arrested trying to rob {} and has to post $250 bail 👮🚨".format(message.author.mention, user.metion))
            await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post $250 bail 👮🚨".format(message.author.mention, user.metion))
            remove_dollars(message.author, 250)
python-3.x discord discord.py
1个回答
0
投票

抱歉有一个脑屁。我最终使用了

 for user in message.mentions:
                if (get_dollars(user)) < 25:
© www.soinside.com 2019 - 2024. All rights reserved.