在命令中投注“XP” discord.py

我无法理解如何让用户在命令中下注 X 量的“XP”(我使用美元)。我在下面发布了简单的 coinflip 命令,其中包含我认为应该是逻辑的内容,但我不能 100% 确定我是否在正确的轨道上。我想知道当用户下注随机数量的钱时,如何为用户调用 get_dollars。我猜我将需要创建诸如betamount = enter authors bet amount但我在如何处理他们可能放置的随机数量而不是硬编码强制用户使用的固定数量上画一个空白的东西。


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 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):

    betamount = ???

    if message.content.lower().startswith('!coinflip'):

        if get_dollars(message.author) < 0:

            await client.send_message(message.channel, "{} you don't have enough money to bet.".format(message.author.mention))

        else:

            choice = random.randint(0,1)

            if choice == 0

                await client.add_reaction(message, '⚫')

                await client.send_message(message.channel, "The coin handed on heads!)

                if 'heads' in message.content:

                    await client.send_message(message.channel, "You've won ${}".format(betamount))

                    add_dollars(message.author, betamount)

                else:

                    if 'tails' in message.content:

                        await client.send_message(message.channel, "You've lost ${}".format(betamount))

                        remove_dollars(message.author, betamount)

            elif choice == 1:

                await client.add_reaction(message, '⚪')

                await client.send_message(message.channel, "The coin handed on tails!")

                if 'tails' in message.content:

                    await client.send_message(message.channel, "You've won ${}".format(betamount))

                    add_dollars(message.author, betamount)

慕的地10843
浏览 118回答 2
2回答

慕的地6264312

如果您使用的是这样的命令,!coinflip (heads/tails) (amount)则可以使用x = message.content.split(" ")将消息拆分为列表。有了这个,你可以做类似outcome = x[1]和的事情betamount = x[2]。我还建议您然后更改if 'tails' in message.content:为if outcome == 'tails'.&nbsp;否则,用户可以做一些类似的事情!coinflip heads (amount) tails,每次都会奖励他们现金。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python