Получение пользователем упомянутой "XP". JSON, Discord.py

Я пытаюсь сделать команду, где пользователи могут украсть XP другого пользователя (доллары). я использую random.randintчтобы они были успешными или нет, но я хочу сделать проверку, чтобы убедиться, что у упомянутого пользователя (ограбленного) достаточно XP (долларов), чтобы автор мог украсть. Я пробовал несколько способов, таких как if (get_dollars(message.mentions[0].id) < 25:а также 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)

1 ответ

Извини, у меня был пердеть Я в конечном итоге с помощью

 for user in message.mentions:
                if (get_dollars(user)) < 25:
Другие вопросы по тегам