为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地10843 2021-06-06 10:11:44
我无法理解如何让用户在命令中下注 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.eventasync 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)
查看完整描述

2 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

如果您使用的是这样的命令,!coinflip (heads/tails) (amount)则可以使用x = message.content.split(" ")将消息拆分为列表。有了这个,你可以做类似outcome = x[1]和的事情betamount = x[2]

我还建议您然后更改if 'tails' in message.content:if outcome == 'tails'. 否则,用户可以做一些类似的事情!coinflip heads (amount) tails,每次都会奖励他们现金。


查看完整回答
反对 回复 2021-06-08
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号