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

问题描述 投票:0回答:2

我无法理解如何让用户在命令中下注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)
                else:
                    if 'heads' in message.content:
                        await client.send_message(message.channel, "You've lost ${}".format(betamount))
                        remove_dollars(message.author, betamount)
python python-3.x discord discord.py
2个回答
1
投票

如果您使用的命令如!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的事情,每次都会奖励他们现金。


1
投票

如果您正在使用参数进入命令,(特别是一旦这些参数成为像这样的discord.Member类型),我会强烈建议切换到使用discord.ext.commands扩展。它使编写命令变得更加简单,这意味着您可以将代码移出on_message事件。以下是使用commands执行命令的方式:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def coinflip(ctx, guess: str, amount: float):
    guesses = ('heads', 'tails')
    guess = guess.lower()
    if guess not in guesses:
        await bot.say("Invalid guess.")
        return
    author = ctx.message.author
    balance = get_dollars(author)
    if balance < amount:
        await bot.say(f"You don't have that much money.  Your balance is ${balance:.2f}")
        return
    result = random.sample(guesses)
    if result == guess:
        await bot.say("You won!")
        add_dollars(author, amount)
    else:
        await bot.say("You lost!")
        remove_dollars(author, amount)

bot.run("TOKEN")

另请注意,如果您开始使用命令,则必须将await bot.process_commands(message)添加到on_message事件的底部

© www.soinside.com 2019 - 2024. All rights reserved.